From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.3 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,NICE_REPLY_A,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED, USER_AGENT_SANE_1 autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id B78B0C43381 for ; Thu, 18 Mar 2021 19:06:52 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 8F13764F62 for ; Thu, 18 Mar 2021 19:06:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232824AbhCRTGX (ORCPT ); Thu, 18 Mar 2021 15:06:23 -0400 Received: from mga14.intel.com ([192.55.52.115]:40804 "EHLO mga14.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231590AbhCRTGA (ORCPT ); Thu, 18 Mar 2021 15:06:00 -0400 IronPort-SDR: xKOc06sQKVp6/FUGF8fGeXIx5jd4M5nHvgocyvCnyt5yP/Hf/oIGqfRgh4M0Iy98GPknM62ctr dEJvILz/Iv2A== X-IronPort-AV: E=McAfee;i="6000,8403,9927"; a="189128303" X-IronPort-AV: E=Sophos;i="5.81,259,1610438400"; d="scan'208";a="189128303" Received: from orsmga002.jf.intel.com ([10.7.209.21]) by fmsmga103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Mar 2021 12:06:00 -0700 IronPort-SDR: hqYeyuc91+tiTRuC7ikztwLhEgKS2tALLRwJSbZ64YO5K2tylKZBX7tsRUFeWimmlOPHqcL6yO P5Jcq/OKHeVg== X-IronPort-AV: E=Sophos;i="5.81,259,1610438400"; d="scan'208";a="389358155" Received: from yyu32-mobl1.amr.corp.intel.com (HELO [10.209.36.121]) ([10.209.36.121]) by orsmga002-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Mar 2021 12:05:58 -0700 Subject: Re: [PATCH v23 22/28] x86/cet/shstk: User-mode shadow stack support To: Borislav Petkov Cc: x86@kernel.org, "H. Peter Anvin" , Thomas Gleixner , Ingo Molnar , linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-api@vger.kernel.org, Arnd Bergmann , Andy Lutomirski , Balbir Singh , Cyrill Gorcunov , Dave Hansen , Eugene Syromiatnikov , Florian Weimer , "H.J. Lu" , Jann Horn , Jonathan Corbet , Kees Cook , Mike Kravetz , Nadav Amit , Oleg Nesterov , Pavel Machek , Peter Zijlstra , Randy Dunlap , "Ravi V. Shankar" , Vedvyas Shanbhogue , Dave Martin , Weijiang Yang , Pengfei Xu , Haitao Huang References: <20210316151054.5405-1-yu-cheng.yu@intel.com> <20210316151054.5405-23-yu-cheng.yu@intel.com> <20210318123215.GE19570@zn.tnic> From: "Yu, Yu-cheng" Message-ID: Date: Thu, 18 Mar 2021 12:05:58 -0700 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.8.0 MIME-Version: 1.0 In-Reply-To: <20210318123215.GE19570@zn.tnic> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 3/18/2021 5:32 AM, Borislav Petkov wrote: >> Subject: Re: [PATCH v23 22/28] x86/cet/shstk: User-mode shadow stack support > ^ > Add > > On Tue, Mar 16, 2021 at 08:10:48AM -0700, Yu-cheng Yu wrote: >> Introduce basic shadow stack enabling/disabling/allocation routines. >> A task's shadow stack is allocated from memory with VM_SHSTK flag and has >> a fixed size of min(RLIMIT_STACK, 4GB). >> >> Signed-off-by: Yu-cheng Yu >> Reviewed-by: Kees Cook >> --- >> arch/x86/include/asm/cet.h | 28 ++++++ >> arch/x86/include/asm/processor.h | 5 ++ >> arch/x86/kernel/Makefile | 2 + >> arch/x86/kernel/cet.c | 147 +++++++++++++++++++++++++++++++ [...] >> +void cet_free_shstk(struct task_struct *tsk) >> +{ >> + struct cet_status *cet = &tsk->thread.cet; >> + >> + if (!static_cpu_has(X86_FEATURE_SHSTK) || > > cpu_feature_enabled and as above. > >> + !cet->shstk_size || !cet->shstk_base) >> + return; >> + >> + if (!tsk->mm || tsk->mm != current->mm) >> + return; > > You're operating on current here merrily but what's protecting all those > paths operating on current from getting current changed underneath them > due to scheduling? IOW, is preemption safely disabled in all those > paths ending up here? Good thought. Indeed, this looks like scheduling would bring some trouble. However, when this instance is running, the current task must be current, context switch or not. The purpose of this check is described below. When fork() fails, it calls exit_thread(), then cet_free_shstk(). Normally the child tsk->mm != current->mm (parent). There is no need to free shadow stack. For CLONE_VM, however, the kernel has already allocated a shadow stack for the child and needs to free it because fork() failed. Maybe I would add comments here. > >> + >> + while (1) { > > Uuh, an endless loop. What guarantees we'll exit it relatively timely... > >> + int r; >> + >> + r = vm_munmap(cet->shstk_base, cet->shstk_size); >> + >> + /* >> + * Retry if mmap_lock is not available. >> + */ >> + if (r == -EINTR) { >> + cond_resched(); > > ... that thing? If vm_munmap() returns -EINTR, mmap_lock is held by something else. That lock should not be held forever. For other types of error, the loop stops. > >> + continue; >> + } >> + >> + WARN_ON_ONCE(r); >> + break; >> + } >> + >> + cet->shstk_base = 0; >> + cet->shstk_size = 0; >> +} >> -- >> 2.21.0 >> >