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 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id F05A7C3A5A7 for ; Tue, 6 Dec 2022 23:24:11 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6438F8E0003; Tue, 6 Dec 2022 18:24:11 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 5F40F8E0001; Tue, 6 Dec 2022 18:24:11 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4BBCD8E0003; Tue, 6 Dec 2022 18:24:11 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 3D2338E0001 for ; Tue, 6 Dec 2022 18:24:11 -0500 (EST) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 9B92EC0547 for ; Tue, 6 Dec 2022 23:24:10 +0000 (UTC) X-FDA: 80213461860.26.456EF9E Received: from mail-pf1-f180.google.com (mail-pf1-f180.google.com [209.85.210.180]) by imf10.hostedemail.com (Postfix) with ESMTP id 39130C0005 for ; Tue, 6 Dec 2022 23:24:10 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=HVrEOzqv; spf=pass (imf10.hostedemail.com: domain of keescook@chromium.org designates 209.85.210.180 as permitted sender) smtp.mailfrom=keescook@chromium.org; dmarc=pass (policy=none) header.from=chromium.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1670369050; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=ovJCxHQc8+rS89BUXUtqUrFqrY//aBiCOIFL+tZQbfw=; b=rKMQG6KaV20ANbETpCbBSt96F/nKCnd1c7iTfoRCMYVViiJqKtWuVuMALAJbIbROh8n+y1 +FsOgdnk6UpBUCP/BGTrpuIL96dnjb1ltmlWrkmEA6PsPhqoyPbVDz4Igslwo4xrTr1q/a RmXSOA8txLtq8PxvxPQDWCqxRz3B6Y4= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=HVrEOzqv; spf=pass (imf10.hostedemail.com: domain of keescook@chromium.org designates 209.85.210.180 as permitted sender) smtp.mailfrom=keescook@chromium.org; dmarc=pass (policy=none) header.from=chromium.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1670369050; a=rsa-sha256; cv=none; b=5ajG4ZVnmb8TK39OJShf3s4YQBL6cLVLciX9JM7AgdR7qsc0hSpBdyquST/rLNn3kqLWVo gZZTPuHLMzbd0+D2vaZ3jtl4QH2W/GQP2zBq8jbDvB2CTaEdwseRyXsxqlxOydHeMYNea8 Kyl7MZGb5DL/Isg3op85TQwNxIWUJY8= Received: by mail-pf1-f180.google.com with SMTP id x66so15988529pfx.3 for ; Tue, 06 Dec 2022 15:24:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=ovJCxHQc8+rS89BUXUtqUrFqrY//aBiCOIFL+tZQbfw=; b=HVrEOzqv7iDT/JOxfMs/WBt+oKr/cbFXvM++HKNyT6dOPN3cugGpkJ3yQyWQnmZ4ag 4Kbdw/HvCA0qjI13q9bOhLnNtrZKFx0haHQ6tnR+eDSVRdh0sPSLVD3teiZYQEHyERMT A/r7biG5o6krK9DJyeHPh4P2YS8Mc6eBMytkQ= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=ovJCxHQc8+rS89BUXUtqUrFqrY//aBiCOIFL+tZQbfw=; b=0Fmv22wuBhHlpX9YT0etQ6vZFSuWzAF4RUaONHXdSmFyBWZSd+ckkxqkJ2/ZqlsZWx KHtGdjUG0uiMmJsC+NkGJYryZvZny7ZhGeE5omnSETTiWdmPD0iU43adluEhUSzbu46y GhFm8AtfZDCGK6f94U7kRdiPdUJgiiicE88tHF7ih4zai5mMn2rMPgDCyJGwtxMo1Xi+ wSgTHZeylPyeoTpQSCxFpBjPzmVDetp+Vb/piRb6Fee4iFxky//kp5xmdoPdC/HZw9ym d/sNRXynG8zG1mySxK3EzofFVLg5evNKCys5Y2MvVTzzJLYthu93+aTT+p8xfymEAg2Q +h+A== X-Gm-Message-State: ANoB5plDnAsPzAXj7yDEoIuFh76wLimA7cICqvxdYBYlt3zgoyWJaTn2 xMqwO14SvMx8C8pD1SCRveYgPg== X-Google-Smtp-Source: AA0mqf6Is1y/2rFNhFTvRk7tj3Kc77p/2qwRcRoY9gMN/fuB3B9m27qlvCroh6NLXyJ3BULuoBcP6g== X-Received: by 2002:aa7:938b:0:b0:576:9b24:ca60 with SMTP id t11-20020aa7938b000000b005769b24ca60mr17599406pfe.7.1670369049127; Tue, 06 Dec 2022 15:24:09 -0800 (PST) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id q13-20020a17090311cd00b00189929219acsm13212238plh.183.2022.12.06.15.24.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Dec 2022 15:24:08 -0800 (PST) Date: Tue, 6 Dec 2022 15:24:07 -0800 From: Kees Cook To: Jeff Xu Cc: Greg KH , jeffxu@chromium.org, skhan@linuxfoundation.org, akpm@linux-foundation.org, dmitry.torokhov@gmail.com, dverkamp@chromium.org, hughd@google.com, jorgelo@chromium.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-mm@kvack.org, jannh@google.com, linux-hardening@vger.kernel.org, kernel test robot Subject: Re: [PATCH v5 3/6] mm/memfd: add MFD_NOEXEC_SEAL and MFD_EXEC Message-ID: <202212061521.2BD762D@keescook> References: <20221206152358.1966099-1-jeffxu@google.com> <20221206152358.1966099-4-jeffxu@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Rspamd-Queue-Id: 39130C0005 X-Stat-Signature: bo4i5whf38y8resixywup5hp56mqy6hy X-Rspam-User: X-Spamd-Result: default: False [5.78 / 9.00]; SORBS_IRL_BL(3.00)[209.85.210.180:from]; SUSPICIOUS_RECIPS(1.50)[]; SUBJECT_HAS_UNDERSCORES(1.00)[]; MID_RHS_NOT_FQDN(0.50)[]; BAYES_HAM(-0.32)[69.27%]; RCVD_NO_TLS_LAST(0.10)[]; MIME_GOOD(-0.10)[text/plain]; BAD_REP_POLICIES(0.10)[]; PREVIOUSLY_DELIVERED(0.00)[linux-mm@kvack.org]; FROM_EQ_ENVFROM(0.00)[]; FROM_HAS_DN(0.00)[]; RCPT_COUNT_TWELVE(0.00)[15]; ARC_NA(0.00)[]; R_DKIM_ALLOW(0.00)[chromium.org:s=google]; MIME_TRACE(0.00)[0:+]; TAGGED_RCPT(0.00)[]; DMARC_POLICY_ALLOW(0.00)[chromium.org,none]; TO_MATCH_ENVRCPT_SOME(0.00)[]; ARC_SIGNED(0.00)[hostedemail.com:s=arc-20220608:i=1]; DKIM_TRACE(0.00)[chromium.org:+]; R_SPF_ALLOW(0.00)[+ip4:209.85.128.0/17]; RCVD_COUNT_THREE(0.00)[3]; TO_DN_SOME(0.00)[]; RCVD_VIA_SMTP_AUTH(0.00)[] X-Rspamd-Server: rspam08 X-HE-Tag: 1670369050-897702 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On Tue, Dec 06, 2022 at 09:48:55AM -0800, Jeff Xu wrote: > On Tue, Dec 6, 2022 at 8:35 AM Greg KH wrote: > > > > On Tue, Dec 06, 2022 at 08:26:30AM -0800, Jeff Xu wrote: > > > On Tue, Dec 6, 2022 at 8:04 AM Greg KH wrote: > > > > > > > > On Tue, Dec 06, 2022 at 03:23:55PM +0000, jeffxu@chromium.org wrote: > > > > > --- a/kernel/pid_namespace.c > > > > > +++ b/kernel/pid_namespace.c > > > > > @@ -110,6 +110,11 @@ static struct pid_namespace *create_pid_namespace(struct user_namespace *user_ns > > > > > ns->ucounts = ucounts; > > > > > ns->pid_allocated = PIDNS_ADDING; > > > > > > > > > > +#if defined(CONFIG_SYSCTL) && defined(CONFIG_MEMFD_CREATE) > > > > > + ns->memfd_noexec_scope = > > > > > + task_active_pid_ns(current)->memfd_noexec_scope; > > > > > +#endif > > > > > > > > .c files should never have #if in them. Can't you put this in a .h file > > > > properly so that this does not get really messy over time? > > > > > > > > > > > Thanks for reviewing. > > > It seems to me that checking for CONFIG_XXX is common in c code in > > > kernel/ path. > > > > Maybe, but please don't make it any worse if at all possible. It's > > tough to maintain code like that. > > > > > Do you have a sample code pattern (link/function) that I can follow? > > > > Any of the zillions of #if statements in .h files :) > > > Thanks. > I will take the approach of having real/stub implementation in the h > file, and the c file using it without a compile flag. > Please kindly let me know if this is not right. Right; for example: in .h: #if defined(CONFIG_SYSCTL) && defined(CONFIG_MEMFD_CREATE) static inline void ns_copy_memfd_scope(... dst, ... src) { dst->memfd_noexec_scope = src->memfd_noexec_scope; } #else static inline void ns_set_memfd_scope(... ns, ... scope) { } #endif in .c: ns_copy_memfd_scope(ns, task_active_pid_ns(current)); -- Kees Cook