From nobody Mon Dec 29 09:46:32 2025 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 72D96C4167B for ; Wed, 29 Nov 2023 05:35:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231326AbjK2FfL (ORCPT ); Wed, 29 Nov 2023 00:35:11 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60534 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231129AbjK2FfH (ORCPT ); Wed, 29 Nov 2023 00:35:07 -0500 Received: from mail-pj1-x1032.google.com (mail-pj1-x1032.google.com [IPv6:2607:f8b0:4864:20::1032]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 177BE120 for ; Tue, 28 Nov 2023 21:35:14 -0800 (PST) Received: by mail-pj1-x1032.google.com with SMTP id 98e67ed59e1d1-28555b0c7afso5718919a91.1 for ; Tue, 28 Nov 2023 21:35:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1701236113; x=1701840913; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=/dIBqx2oS+xIFOoQxR3EPS6VRyKX+8khQ60y5SxvTWQ=; b=m1F/Bc+NpdBq8SaSJ3wz3GV43JKRlssdUI7RhF6QCt7mMjls7NDUN6kTVyFELiiEVL kM+pco0XdtyKaneMq4Jssq2WWtW0e+OQUaSSMKScy8hkD8ptq+wWHxo2fMxgGLLJzxgY MlBEzu14FnMOwHoU7r6qI4FKo5zqQE/n2ux0E69ogsBXWnN0edUMHtoB2AcXtXQvSYRF mmppREmdxMfpU/PjDT1VR2jgtWfGz36FrZSmUc+aRKlUEgJLnNZkGFMPVTqJ1orqyiEZ i9PEwXXCHpVfmwgkykHI6TCFh3pzVIRo1xsrwd2AiWczSwFOLh3Z0QN9xbzPfYv6sD4G r/jw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701236113; x=1701840913; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=/dIBqx2oS+xIFOoQxR3EPS6VRyKX+8khQ60y5SxvTWQ=; b=HkQu/yulqt5QxFRKJKs+URMOW+3i/RyenO567K/8cWaHLBWk6DK4+5cYjv0HG2NScl RbVqLC3Zgl6CuT5syYAgwGa8LEjnK58nufLq8m0dR4mPPh5zHv/ZpSEHDnQ1Nk9t8eg3 cSuj+0UhB/wtuZV/90PHpK2oWt34EgSKEocgoXG9KycomAOVBCrIszSby4VECvlE8jIT 96keyTEU4g92OjXMBLp12Z+lBJ1RrCqZMICG8ddCK9zPGmgEPUf4VBKADd3iYvY9UQJZ k7vrarLNe+5uo68pzD7WBMD7cTRxdaIg/h5mgyovICv2Qdwy4IfRpkbQmJB1B49oeqoz D0Pg== X-Gm-Message-State: AOJu0Yz5C2d9dm3ldOxawHs2Ql5vYVyXv5PtRGH/G2ZxtbThnwa1kW87 GFmwIo5V5vPC/QbkrFQkiuHbnkXtxtCOxA== X-Google-Smtp-Source: AGHT+IFGkS+AD6FjcjgeUjG28W7da06oZdikUTQYr6wr1Y6dcP+elvAnT1GQmRIPMuPBQ+fmh4Cvug== X-Received: by 2002:a17:90b:1c85:b0:285:8d5c:9114 with SMTP id oo5-20020a17090b1c8500b002858d5c9114mr17821966pjb.19.1701236113315; Tue, 28 Nov 2023 21:35:13 -0800 (PST) Received: from ubuntu.. ([43.132.98.44]) by smtp.googlemail.com with ESMTPSA id gj6-20020a17090b108600b00285a2d14184sm660717pjb.0.2023.11.28.21.35.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Nov 2023 21:35:12 -0800 (PST) From: Hengqi Chen To: linux-kernel@vger.kernel.org Cc: keescook@chromium.org, luto@amacapital.net, wad@chromium.org, alexyonghe@tencent.com, hengqi.chen@gmail.com Subject: [PATCH v3 1/3] seccomp: Introduce SECCOMP_LOAD_FILTER operation Date: Wed, 29 Nov 2023 05:34:38 +0000 Message-Id: <20231129053440.41522-2-hengqi.chen@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231129053440.41522-1-hengqi.chen@gmail.com> References: <20231129053440.41522-1-hengqi.chen@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" This patch adds a new operation named SECCOMP_LOAD_FILTER. It accepts a sock_fprog the same as SECCOMP_SET_MODE_FILTER but only performs the loading process. If succeed, returns a new fd associated with the seccomp filter. The seccomp filter thus can be reused to attach to different processes. Signed-off-by: Hengqi Chen --- include/uapi/linux/seccomp.h | 1 + kernel/seccomp.c | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/include/uapi/linux/seccomp.h b/include/uapi/linux/seccomp.h index dbfc9b37fcae..ee2c83697810 100644 --- a/include/uapi/linux/seccomp.h +++ b/include/uapi/linux/seccomp.h @@ -16,6 +16,7 @@ #define SECCOMP_SET_MODE_FILTER 1 #define SECCOMP_GET_ACTION_AVAIL 2 #define SECCOMP_GET_NOTIF_SIZES 3 +#define SECCOMP_LOAD_FILTER 4 =20 /* 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 255999ba9190..ef956c3d15c7 100644 --- a/kernel/seccomp.c +++ b/kernel/seccomp.c @@ -1885,6 +1885,18 @@ static bool has_duplicate_listener(struct seccomp_fi= lter *new_child) return false; } =20 +static int seccomp_filter_put(struct inode *inode, struct file *file) +{ + struct seccomp_filter *filter =3D file->private_data; + + __put_seccomp_filter(filter); + return 0; +} + +static const struct file_operations seccomp_filter_fops =3D { + .release =3D seccomp_filter_put, +}; + /** * seccomp_set_mode_filter: internal function for setting seccomp filter * @flags: flags to change filter behavior @@ -1996,12 +2008,29 @@ static long seccomp_set_mode_filter(unsigned int fl= ags, seccomp_filter_free(prepared); return ret; } + +static long seccomp_load_filter(const char __user *filter) +{ + struct seccomp_filter *sfilter; + + sfilter =3D seccomp_prepare_user_filter(filter); + if (IS_ERR(sfilter)) + return PTR_ERR(sfilter); + + return anon_inode_getfd("seccomp-filter", &seccomp_filter_fops, + sfilter, O_CLOEXEC); +} #else static inline long seccomp_set_mode_filter(unsigned int flags, const char __user *filter) { return -EINVAL; } + +static inline long seccomp_load_filter(const char __user *filter) +{ + return -EINVAL; +} #endif =20 static long seccomp_get_action_avail(const char __user *uaction) @@ -2063,6 +2092,11 @@ static long do_seccomp(unsigned int op, unsigned int= flags, return -EINVAL; =20 return seccomp_get_notif_sizes(uargs); + case SECCOMP_LOAD_FILTER: + if (flags !=3D 0) + return -EINVAL; + + return seccomp_load_filter(uargs); default: return -EINVAL; } --=20 2.34.1 From nobody Mon Dec 29 09:46:32 2025 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0C04BC4167B for ; Wed, 29 Nov 2023 05:35:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231129AbjK2FfW (ORCPT ); Wed, 29 Nov 2023 00:35:22 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38500 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231243AbjK2FfS (ORCPT ); Wed, 29 Nov 2023 00:35:18 -0500 Received: from mail-pf1-x430.google.com (mail-pf1-x430.google.com [IPv6:2607:f8b0:4864:20::430]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 424CC19AD for ; Tue, 28 Nov 2023 21:35:25 -0800 (PST) Received: by mail-pf1-x430.google.com with SMTP id d2e1a72fcca58-6cbe716b511so4827950b3a.3 for ; Tue, 28 Nov 2023 21:35:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1701236124; x=1701840924; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=YwvK48qFUm0jCa9wEfVcXn45D/cRCqvvkYtg9bxD7CQ=; b=bp0oQe9UlkFRjkIx9QI9KDBxM8CKm9e0RQ9yt/Qix0KYIn2DttzSxYxNOgLL863MlG D4PqGO+ZxG7y1RleoHR4MnAU7BekyB5nBiDuyJkB0VEkN/7/LipqTKOvIRc/eqS6CFBk xEzaDXTCNR202R6Tlpf89IoqcWmyr8QOSZBS9J03v8e1ZMIbThMkW5umWhK71C4lfmei /3aM57zK5Bwf2YVzwreRmojucpMpNGT9Er1uJivsj+i/9cpMvtayjRdqWMCUqsABkwsp RCzgL4qcIeS0S2cU5LETLRwrvHDo2QV4+L4Er8M9uCICFNNf5I+jeBfWMRMa3oL+W5cM Bh+g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701236124; x=1701840924; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=YwvK48qFUm0jCa9wEfVcXn45D/cRCqvvkYtg9bxD7CQ=; b=ciRyhVqFjpLv1fHI5k0Je+Nx56ORIKB36WicoAlEXQTxbNBhVKXl7jH7yTDGSWTZJt OSNiQUCaqn1mpvfjfiHv8ND9MUd+bGP6QAX1KKnVNEDGzGpUYcsBGkwJ9YI7zedTf8QI I0p7v4LdySw3i74MiUfrkLT97VjmqomZcflJuL8+29t68OOOLpAOWdPir4hFdqCdvQiA IVIhkt+Q7ARmtpfSnGopUxiV3EsvfptkvqgClFAa8gDaHHAD69qf6H/+7IYjoz2g8LnM 6YoDTpR0OkkTW0NnFcDq6e0Ks+5DWrKqn9LiiJ30fP4hNKk6P1i9en5/lpnQB2a3l5pr ANwQ== X-Gm-Message-State: AOJu0Yw810l3UZ61MAmwI6NehTnp1hljIurz8pZIyB+0P7EplA0GXV1Z tQC2DxEBob4kc1ukN8aAy6JxyHlZtOCf3Q== X-Google-Smtp-Source: AGHT+IGqw9zpcmv9S8ayL/b0Kmr/cPkw5pqGB8FtB1beqRSMUAvhJmfMDLi0YT1zEZ6KrwMRF/bgpw== X-Received: by 2002:a05:6a20:8e1e:b0:18c:6c2:55e0 with SMTP id y30-20020a056a208e1e00b0018c06c255e0mr16921486pzj.22.1701236124324; Tue, 28 Nov 2023 21:35:24 -0800 (PST) Received: from ubuntu.. ([43.132.98.44]) by smtp.googlemail.com with ESMTPSA id gj6-20020a17090b108600b00285a2d14184sm660717pjb.0.2023.11.28.21.35.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Nov 2023 21:35:23 -0800 (PST) From: Hengqi Chen To: linux-kernel@vger.kernel.org Cc: keescook@chromium.org, luto@amacapital.net, wad@chromium.org, alexyonghe@tencent.com, hengqi.chen@gmail.com Subject: [PATCH v3 2/3] seccomp: Introduce new flag SECCOMP_FILTER_FLAG_FILTER_FD Date: Wed, 29 Nov 2023 05:34:39 +0000 Message-Id: <20231129053440.41522-3-hengqi.chen@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231129053440.41522-1-hengqi.chen@gmail.com> References: <20231129053440.41522-1-hengqi.chen@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Add a new flag SECCOMP_FILTER_FLAG_FILTER_FD for SECCOMP_SET_MODE_FILTER. This indicates user supply a seccomp filter fd, not a sock_fprog. It allows us to attach the seccomp filter that is previously loaded via SECCOMP_LOAD_FILTER. Signed-off-by: Hengqi Chen --- include/linux/seccomp.h | 3 ++- include/uapi/linux/seccomp.h | 2 ++ kernel/seccomp.c | 44 +++++++++++++++++++++++++++++++++--- 3 files changed, 45 insertions(+), 4 deletions(-) diff --git a/include/linux/seccomp.h b/include/linux/seccomp.h index 175079552f68..3257042c35fc 100644 --- a/include/linux/seccomp.h +++ b/include/linux/seccomp.h @@ -9,7 +9,8 @@ SECCOMP_FILTER_FLAG_SPEC_ALLOW | \ SECCOMP_FILTER_FLAG_NEW_LISTENER | \ SECCOMP_FILTER_FLAG_TSYNC_ESRCH | \ - SECCOMP_FILTER_FLAG_WAIT_KILLABLE_RECV) + SECCOMP_FILTER_FLAG_WAIT_KILLABLE_RECV | \ + SECCOMP_FILTER_FLAG_FILTER_FD) =20 /* sizeof() the first published struct seccomp_notif_addfd */ #define SECCOMP_NOTIFY_ADDFD_SIZE_VER0 24 diff --git a/include/uapi/linux/seccomp.h b/include/uapi/linux/seccomp.h index ee2c83697810..41e0ca56ef1c 100644 --- a/include/uapi/linux/seccomp.h +++ b/include/uapi/linux/seccomp.h @@ -26,6 +26,8 @@ #define SECCOMP_FILTER_FLAG_TSYNC_ESRCH (1UL << 4) /* Received notifications wait in killable state (only respond to fatal si= gnals) */ #define SECCOMP_FILTER_FLAG_WAIT_KILLABLE_RECV (1UL << 5) +/* Indicates that the filter is a fd obtained from SECCOMP_LOAD_FILTER */ +#define SECCOMP_FILTER_FLAG_FILTER_FD (1UL << 6) =20 /* * All BPF programs must return a 32-bit value. diff --git a/kernel/seccomp.c b/kernel/seccomp.c index ef956c3d15c7..a43a6a6b6b77 100644 --- a/kernel/seccomp.c +++ b/kernel/seccomp.c @@ -883,8 +883,11 @@ static long seccomp_attach_filter(unsigned int flags, =20 /* Validate resulting filter length. */ total_insns =3D filter->prog->len; - for (walker =3D current->seccomp.filter; walker; walker =3D walker->prev) + for (walker =3D current->seccomp.filter; walker; walker =3D walker->prev)= { total_insns +=3D walker->prog->len + 4; /* 4 instr penalty */ + if (walker =3D=3D filter) + return -EEXIST; + } if (total_insns > MAX_INSNS_PER_PATH) return -ENOMEM; =20 @@ -1897,6 +1900,38 @@ static const struct file_operations seccomp_filter_f= ops =3D { .release =3D seccomp_filter_put, }; =20 +/** + * seccomp_prepare_filter_from_fd - prepares filter from a user-supplied fd + * @ufd: pointer to fd that refers to a seccomp filter. + * + * Returns filter on success or an ERR_PTR on failure. + */ +static struct seccomp_filter * +seccomp_prepare_filter_from_fd(const char __user *ufd) +{ + struct seccomp_filter *sfilter; + struct fd f; + int fd; + + if (copy_from_user(&fd, ufd, sizeof(fd))) + return ERR_PTR(-EFAULT); + + f =3D fdget(fd); + if (!f.file) + return ERR_PTR(-EBADF); + + if (f.file->f_op !=3D &seccomp_filter_fops) { + fdput(f); + return ERR_PTR(-EINVAL); + } + + sfilter =3D f.file->private_data; + __get_seccomp_filter(sfilter); + + fdput(f); + return sfilter; +} + /** * seccomp_set_mode_filter: internal function for setting seccomp filter * @flags: flags to change filter behavior @@ -1944,7 +1979,10 @@ static long seccomp_set_mode_filter(unsigned int fla= gs, return -EINVAL; =20 /* Prepare the new filter before holding any locks. */ - prepared =3D seccomp_prepare_user_filter(filter); + if (flags & SECCOMP_FILTER_FLAG_FILTER_FD) + prepared =3D seccomp_prepare_filter_from_fd(filter); + else + prepared =3D seccomp_prepare_user_filter(filter); if (IS_ERR(prepared)) return PTR_ERR(prepared); =20 @@ -2005,7 +2043,7 @@ static long seccomp_set_mode_filter(unsigned int flag= s, } } out_free: - seccomp_filter_free(prepared); + __put_seccomp_filter(prepared); return ret; } =20 --=20 2.34.1 From nobody Mon Dec 29 09:46:32 2025 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 87B30C4167B for ; Wed, 29 Nov 2023 05:35:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1376948AbjK2Ff1 (ORCPT ); Wed, 29 Nov 2023 00:35:27 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38546 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1376940AbjK2FfV (ORCPT ); Wed, 29 Nov 2023 00:35:21 -0500 Received: from mail-pg1-x529.google.com (mail-pg1-x529.google.com [IPv6:2607:f8b0:4864:20::529]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CA38919B7 for ; Tue, 28 Nov 2023 21:35:27 -0800 (PST) Received: by mail-pg1-x529.google.com with SMTP id 41be03b00d2f7-5c604b092a6so281324a12.0 for ; Tue, 28 Nov 2023 21:35:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1701236127; x=1701840927; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=HgQkKxfbfB1dQHkl0UdeF9VBFXfuQafglyd43X0xZ5I=; b=Q9HJgTB+Qa/OGdbAEZsVhwmNvI14FI/T9fVPN41e/7gGqf7yKeczk8BukkVYr7wUp6 RKVNsKy6d9qIpHTKbooiYxonlv5mV5X5r/lx9zNUG7qeT2vE9Y+frRy65/S/cBD62e8O kQ2uOx+Zx0EmTI2yTA2RV15aVgIeYigI3hohnt89ieaor5YY6hk+jOnktfbDJl0hwcvk SYlVU7oHaDHI19DC66kGxZ82yI4QDjdS4VVqt6qe1eLsuEnhu4Aa0tR+blr7CmX1qe9F 0lPcD4TZmtTT0HJueIWczp0qwVdmTDlkmssIKEPKp6JNj2rUFsFKeXqKdmFh3fJPQgCz ldtw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701236127; x=1701840927; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=HgQkKxfbfB1dQHkl0UdeF9VBFXfuQafglyd43X0xZ5I=; b=EYVrl/ANCZu7TE4+QERgHQ1Rz12sZqs/58YDyrQu3gYTRZbd0+K7ioPGhKkVc6X1m7 eNovZ1m4WvqOWO4rpWVAn4deEyGRj8cwKVSWJxd2pvMklfK91zNp2F1NfBvBpDs1FGxP HJn+A5d4A5Wyt22Hzt4RypuaPzTE6UCQKdRC60sjzYs4s34mscA4uoPLeiWZIZKp6BAb 2+Vwl9SdoaRXLpY8rRJ9w+pSCovIAHI8CW9OvcVRsg9PftCQIdwveLzqEDqvE69OuEw0 FxemoNUjI8pCc7Vnr7JRte4PqXXnpT+/Szgv8r7+d0g2LVm64R8ZqxegkH1hn+drqAz+ /xNA== X-Gm-Message-State: AOJu0YyPAnoRjcIHKkVw9z8kfzELgTqtGgoBQfREFhCdiEC3LkAzZvl9 Ck71Y1jMOiHo8KFdrSdIDTAbfR2kYTEbcg== X-Google-Smtp-Source: AGHT+IHqVK+ATOxjmy6GWlz6Ri5iiOxE7mucUIAdlzFeplutaG34BDnZ2MJ4pcBj87Fcy3mZca+Dqw== X-Received: by 2002:a17:90a:d482:b0:280:4af4:1a41 with SMTP id s2-20020a17090ad48200b002804af41a41mr30356151pju.15.1701236127124; Tue, 28 Nov 2023 21:35:27 -0800 (PST) Received: from ubuntu.. ([43.132.98.44]) by smtp.googlemail.com with ESMTPSA id gj6-20020a17090b108600b00285a2d14184sm660717pjb.0.2023.11.28.21.35.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Nov 2023 21:35:26 -0800 (PST) From: Hengqi Chen To: linux-kernel@vger.kernel.org Cc: keescook@chromium.org, luto@amacapital.net, wad@chromium.org, alexyonghe@tencent.com, hengqi.chen@gmail.com Subject: [PATCH v3 3/3] selftests/seccomp: Test seccomp filter load and attach Date: Wed, 29 Nov 2023 05:34:40 +0000 Message-Id: <20231129053440.41522-4-hengqi.chen@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231129053440.41522-1-hengqi.chen@gmail.com> References: <20231129053440.41522-1-hengqi.chen@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Add testcases to exercise the newly added seccomp filter load and attach functionalities. Signed-off-by: Hengqi Chen --- tools/testing/selftests/seccomp/seccomp_bpf.c | 71 +++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/tools/testing/selftests/seccomp/seccomp_bpf.c b/tools/testing/= selftests/seccomp/seccomp_bpf.c index 38f651469968..66eb72e6c1a3 100644 --- a/tools/testing/selftests/seccomp/seccomp_bpf.c +++ b/tools/testing/selftests/seccomp/seccomp_bpf.c @@ -4735,6 +4735,77 @@ TEST(user_notification_wait_killable_fatal) EXPECT_EQ(SIGTERM, WTERMSIG(status)); } =20 +TEST(seccomp_filter_load_and_attach) +{ + struct sock_filter filter[] =3D { + BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW), + }; + struct sock_fprog prog =3D { + .len =3D (unsigned short)ARRAY_SIZE(filter), + .filter =3D filter, + }; + int fd, ret, flags; + + ret =3D prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); + ASSERT_EQ(0, ret) + { + TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!"); + } + + flags =3D 0; + fd =3D seccomp(SECCOMP_LOAD_FILTER, flags, &prog); + ASSERT_GT(fd, -1); + + flags =3D SECCOMP_FILTER_FLAG_FILTER_FD; + ret =3D seccomp(SECCOMP_SET_MODE_FILTER, flags, &fd); + ASSERT_EQ(ret, 0); + + flags =3D SECCOMP_FILTER_FLAG_FILTER_FD; + ret =3D seccomp(SECCOMP_SET_MODE_FILTER, flags, &fd); + ASSERT_EQ(ret, -1); + ASSERT_EQ(errno, EEXIST); + + flags =3D 0; + ret =3D seccomp(SECCOMP_SET_MODE_FILTER, flags, &prog); + ASSERT_EQ(ret, 0); + + close(fd); +} + +TEST(seccomp_attach_fd_failed) +{ + int fd, ret, flags; + + ret =3D prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); + ASSERT_EQ(0, ret) + { + TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!"); + } + + fd =3D socket(AF_UNIX, SOCK_STREAM, 0); + ASSERT_GT(fd, -1); + + /* copy a sock_fprog from a fd */ + flags =3D 0; + ret =3D seccomp(SECCOMP_SET_MODE_FILTER, flags, &fd); + ASSERT_EQ(ret, -1); + ASSERT_EQ(errno, EFAULT); + + /* pass a non seccomp filter fd */ + flags =3D SECCOMP_FILTER_FLAG_FILTER_FD; + ret =3D seccomp(SECCOMP_SET_MODE_FILTER, flags, &fd); + ASSERT_EQ(ret, -1); + ASSERT_EQ(errno, EINVAL); + close(fd); + + /* pass a invalid fd */ + fd =3D -1; + flags =3D SECCOMP_FILTER_FLAG_FILTER_FD; + ret =3D seccomp(SECCOMP_SET_MODE_FILTER, flags, &fd); + ASSERT_EQ(ret, -1); + ASSERT_EQ(errno, EBADF); +} + /* * TODO: * - expand NNP testing --=20 2.34.1