Received: by 2002:a05:7412:d8a:b0:e2:908c:2ebd with SMTP id b10csp14025rdg; Wed, 11 Oct 2023 18:50:20 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFgOB6728E8M/pXjMEECcz6t7kOsjGEalB5gh+JqQQ+Dc4M64z9YYCSEI6EeDSYRsUjNkIw X-Received: by 2002:a17:902:e849:b0:1b8:6cae:3570 with SMTP id t9-20020a170902e84900b001b86cae3570mr27785147plg.11.1697075420602; Wed, 11 Oct 2023 18:50:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697075420; cv=none; d=google.com; s=arc-20160816; b=ZjrENS/FIC6V4Ec8I5slZODLuhMocEgm+5MbnNLrw+csi+MTFfuoHrAGW7N0UuU8js +tmyA7LFGUQFT+w7+e3T8CbD+9WxDiiLXOcbDNkiMlIQG1mBNIShXP2s2MBxxYteLIeH hSf5zzPhlhEiYJheTeIvAhUxZeOcccRQjdDBloRSA2TSaXSKnuItu8R7aZePSEp7nhtV zHva+ph2wlWh+ji6U5UKA+9cZKod6BE4PiOem2vSbvBcgZJWdhbfdzidAeY4MYZnsnUE nc8skZqdboEM4XXDrp03Mg/n7fDWK0IVBDvqNNNvx8dC8sb35kVOTRO7qHUBy7fRzjeu 6JFQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=ZXFwUyTy0U6yArCYy69m9SsGkAP6XEKpYScGP5jhuhs=; fh=tJ2UZ0tcKCt35O21cSZih4D2BIwoe8mtH3s2s3GlJM0=; b=KBDOhZKJV/G3hVPnw/gIOrHyrNqn4YbynXMTygCU0caMn38Xpt57aiWtZLihxXXF2l JC4JVV0LCaaHIbJiL9nbc3noTaAMA9i0BYjzAXMp1xAuHEPTyQFEJe/BG6XTJL3BXSml i72UPpOZaeo1rFAcqxR/81x3tCAglsFzSGE+eNYAQWP2ArAhYOHsiCI2HMZBPcDp9tyS B7C+8e8BujlG9RwG1Zia2gM/UdV41FDzXm9I470Zq3qMlg8OYMI78QHQ/vIX0jSucmJD rQtUv1J3LMtFCysVARkZmQZKWt37I/LZswjsyrpMT2Ov7vM3m2/AQd3bxpqJfB3PzFJq 4nvA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=jWOkzlQG; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id x8-20020a170902ec8800b001c4401a7e18si1057151plg.382.2023.10.11.18.50.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 Oct 2023 18:50:20 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=jWOkzlQG; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id AD70981E64BD; Wed, 11 Oct 2023 18:50:19 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234072AbjJLBuO (ORCPT + 99 others); Wed, 11 Oct 2023 21:50:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40256 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234024AbjJLBuN (ORCPT ); Wed, 11 Oct 2023 21:50:13 -0400 Received: from mail-oa1-x33.google.com (mail-oa1-x33.google.com [IPv6:2001:4860:4864:20::33]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7AC70B8; Wed, 11 Oct 2023 18:50:11 -0700 (PDT) Received: by mail-oa1-x33.google.com with SMTP id 586e51a60fabf-1dcead29b3eso253967fac.3; Wed, 11 Oct 2023 18:50:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1697075411; x=1697680211; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=ZXFwUyTy0U6yArCYy69m9SsGkAP6XEKpYScGP5jhuhs=; b=jWOkzlQGLWZ+a8QJ6UcAQRMXcv/d8GvTAt/g0zdWnXPeLd4El7QgVUhvRPt4cfXFrb n/ck7qFTy3RH+WuE9M8YITKFpOM89aPgSH2wy/2NE0x7/Cig6Nxbut9dpU2dS+Nk5G5j nLGE0k9OGJFebKp132y6tIQ7yMIwnepx/FN4o1r0jSpza6DJedDkdur2omcK8Nb0bIQe eOtBLgWo3vIR4eSVgU3mBkTYKDvhKhukNSzzzZu7oTKwzHhZ0P1Dnh65kMbufottL4ux NJdjuEl4PzVGnqGl0jiRctunG0yvvJr30QCu7p7gxI7piF0LIXfWevoSw+2nArmTp+Oe mcoQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697075411; x=1697680211; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ZXFwUyTy0U6yArCYy69m9SsGkAP6XEKpYScGP5jhuhs=; b=W6/HEkjn3HedA4v2x57sdbZA/78teSkTKJJczaMOpOBj+1f9N1oQXX7yTZ7DZt25nV TCklmAkAzAZMcEvTKaLhJhGZciyHFQU0D8SDPJkGQqLlBdGDkV8NBTMAqnjIMlci37Yz wMZYvcj6ilhUronQvWsfVU6EMpcguJjWDMzWYQCDXT+0FkJvLtVGOJhYDZtOW1MwXVpU NF2/ZCAc5oeUJf77tkCDzvK1s3DvFqCOqt+YUDwJfyUwAItMZLYyosmTsvRBTucbEfC9 bBQs0opAxjQJf0mJWPdUFriwGt9gFZkBW9NknJWIdF7ybH9sSMxxwfhBQzV3YxZp6eHI 2yEQ== X-Gm-Message-State: AOJu0Yydl/Y6wYeHzwVlRzUz8zidIug7X1NqYOYzog2UiylBdJuZhZIj 7PjUeNrhsmHtveKQ/pkYT4DOxrgp57SOnqcELjy04uD9cZA= X-Received: by 2002:a05:6871:209:b0:1e9:b0be:d006 with SMTP id t9-20020a056871020900b001e9b0bed006mr1015538oad.2.1697075410743; Wed, 11 Oct 2023 18:50:10 -0700 (PDT) MIME-Version: 1.0 References: <20231009124046.74710-1-hengqi.chen@gmail.com> <20231009124046.74710-4-hengqi.chen@gmail.com> <202310101719.2E6AA3E@keescook> In-Reply-To: <202310101719.2E6AA3E@keescook> From: Hengqi Chen Date: Thu, 12 Oct 2023 09:49:59 +0800 Message-ID: Subject: Re: [PATCH 3/4] seccomp: Introduce SECCOMP_ATTACH_FILTER operation To: Kees Cook Cc: linux-kernel@vger.kernel.org, bpf@vger.kernel.org, ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, luto@amacapital.net, wad@chromium.org, alexyonghe@tencent.com Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Wed, 11 Oct 2023 18:50:19 -0700 (PDT) On Wed, Oct 11, 2023 at 8:22=E2=80=AFAM Kees Cook w= rote: > > On Mon, Oct 09, 2023 at 12:40:45PM +0000, Hengqi Chen wrote: > > The SECCOMP_ATTACH_FILTER operation is used to attach > > a loaded filter to the current process. The loaded filter > > is represented by a fd which is either returned by the > > SECCOMP_LOAD_FILTER operation or obtained from bpffs using > > bpf syscall. > > > > Signed-off-by: Hengqi Chen > > --- > > include/uapi/linux/seccomp.h | 1 + > > kernel/seccomp.c | 68 +++++++++++++++++++++++++++++++++--- > > 2 files changed, 64 insertions(+), 5 deletions(-) > > > > diff --git a/include/uapi/linux/seccomp.h b/include/uapi/linux/seccomp.= h > > index ee2c83697810..fbe30262fdfc 100644 > > --- a/include/uapi/linux/seccomp.h > > +++ b/include/uapi/linux/seccomp.h > > @@ -17,6 +17,7 @@ > > #define SECCOMP_GET_ACTION_AVAIL 2 > > #define SECCOMP_GET_NOTIF_SIZES 3 > > #define SECCOMP_LOAD_FILTER 4 > > +#define SECCOMP_ATTACH_FILTER 5 > > > > /* Valid flags for SECCOMP_SET_MODE_FILTER */ > > #define SECCOMP_FILTER_FLAG_TSYNC (1UL << 0) > > diff --git a/kernel/seccomp.c b/kernel/seccomp.c > > index 3ae43db3b642..9f9d8a7a1d6e 100644 > > --- a/kernel/seccomp.c > > +++ b/kernel/seccomp.c > > @@ -523,7 +523,10 @@ static inline pid_t seccomp_can_sync_threads(void) > > static inline void seccomp_filter_free(struct seccomp_filter *filter) > > { > > if (filter) { > > - bpf_prog_destroy(filter->prog); > > + if (filter->prog->type =3D=3D BPF_PROG_TYPE_SECCOMP) > > + bpf_prog_put(filter->prog); > > + else > > + bpf_prog_destroy(filter->prog); > > kfree(filter); > > } > > } > > @@ -894,7 +897,7 @@ static void seccomp_cache_prepare(struct seccomp_fi= lter *sfilter) > > #endif /* SECCOMP_ARCH_NATIVE */ > > > > /** > > - * seccomp_attach_filter: validate and attach filter > > + * seccomp_do_attach_filter: validate and attach filter > > * @flags: flags to change filter behavior > > * @filter: seccomp filter to add to the current process > > * > > @@ -905,8 +908,8 @@ static void seccomp_cache_prepare(struct seccomp_fi= lter *sfilter) > > * seccomp mode or did not have an ancestral seccomp filter > > * - in NEW_LISTENER mode: the fd of the new listener > > */ > > -static long seccomp_attach_filter(unsigned int flags, > > - struct seccomp_filter *filter) > > +static long seccomp_do_attach_filter(unsigned int flags, > > + struct seccomp_filter *filter) > > { > > unsigned long total_insns; > > struct seccomp_filter *walker; > > @@ -2001,7 +2004,7 @@ static long seccomp_set_mode_filter(unsigned int = flags, > > goto out; > > } > > > > - ret =3D seccomp_attach_filter(flags, prepared); > > + ret =3D seccomp_do_attach_filter(flags, prepared); > > if (ret) > > goto out; > > /* Do not free the successfully attached filter. */ > > @@ -2058,6 +2061,51 @@ static long seccomp_load_filter(const char __use= r *filter) > > bpf_prog_put(prog); > > return ret; > > } > > + > > +static long seccomp_attach_filter(const char __user *ufd) > > +{ > > + const unsigned long seccomp_mode =3D SECCOMP_MODE_FILTER; > > + struct seccomp_filter *sfilter; > > + struct bpf_prog *prog; > > + int flags =3D 0; > > + int fd, ret; > > + > > + if (copy_from_user(&fd, ufd, sizeof(fd))) > > + return -EFAULT; > > + > > + prog =3D bpf_prog_get_type(fd, BPF_PROG_TYPE_SECCOMP); > > + if (IS_ERR(prog)) > > + return PTR_ERR(prog); > > + > > + sfilter =3D kzalloc(sizeof(*sfilter), GFP_KERNEL | __GFP_NOWARN); > > + if (!sfilter) { > > + bpf_prog_put(prog); > > + return -ENOMEM; > > + } > > + > > + sfilter->prog =3D prog; > > + refcount_set(&sfilter->refs, 1); > > + refcount_set(&sfilter->users, 1); > > + mutex_init(&sfilter->notify_lock); > > + init_waitqueue_head(&sfilter->wqh); > > + > > + spin_lock_irq(¤t->sighand->siglock); > > + > > + ret =3D -EINVAL; > > + if (!seccomp_may_assign_mode(seccomp_mode)) > > + goto out; > > + > > + ret =3D seccomp_do_attach_filter(flags, sfilter); > > + if (ret) > > + goto out; > > + > > + sfilter =3D NULL; > > + seccomp_assign_mode(current, seccomp_mode, flags); > > +out: > > + spin_unlock_irq(¤t->sighand->siglock); > > + seccomp_filter_free(sfilter); > > + return ret; > > +} > > This is duplicating part of seccomp_set_mode_filter() but without > handling flags at all. This isn't really workable, since we need things > like TSYNC, etc. I think it would be better to adjust > SECCOMP_SET_MODE_FILTER to take a new flag that indicates that the user > arg is an fd, not a filter. Then the middle of seccomp_set_mode_filter() > can choosen between seccomp_prepare_user_filter() and a wrapped call to > bpf_prog_get_type() on the fd, etc. > Great, that would make things easier. Thanks. > -- > Kees Cook