Received: by 2002:a05:7412:d8a:b0:e2:908c:2ebd with SMTP id b10csp2236679rdg; Sun, 15 Oct 2023 18:48:04 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEfvcyi6lzQz5UX62q9/MqrMpu7E1ECfmCvJfD68TVHBeN+KyrWJD5wA+Bi4rYL5VVlhwH3 X-Received: by 2002:a17:902:da8e:b0:1c6:2655:625d with SMTP id j14-20020a170902da8e00b001c62655625dmr9700640plx.15.1697420884477; Sun, 15 Oct 2023 18:48:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697420884; cv=none; d=google.com; s=arc-20160816; b=IINF/ApcOQJ5RLwSt2/thyZjm0pY2Wb2Ye/iKDIn3ryaznjZgEnyWmCzXv+bg3T2fK TQ/ZJnaVe4hAbBNCfgvy6HPuTj/sWATo6Y0PgSSqSzd0iC1XBCV0hm0KqDzKMkikElSg b8a/yFc6Y9l1SxOW8148JJ+Z0Ph0u+yZyTfQeT2idqcst1ErpJ1Ta/DzajW7Y7HgMbZd FSYxEfJJKHIArEb8Xr9TxkQv0Oz8FSpDrYx/JEuH3dtk2pKDHohYgfvMt/BlUXVrhGsY 1OwOi2vQZTIaqQ3ak3I6dA5KzYitMknsfQAXdK60ghBe55rSiLrjPHEfxKLSMUwSSTzb /LLw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=sG+9X4nVV+pHbrjt1Lng7ohViJzwy8aDHAaR38B9wtI=; fh=UUVBB6hdZtgkdevQaSWXrPPcR6FiQNJo3IQxWCli3s0=; b=Q+81eQahvA4cgwB01OXXiIlP5MsHLu0f7EeqsMbFfXZ2s4D00WEmoeH3G5eMKooC1Y sdOqcevHVrVw/4ZkCidVt/rAYcJ3WH7jUgtDhT4J8T+oXOeW7bFBLy0CkpPxNmNuPx0B 7hzd2FvKjo2vq5c/sUsjtr6Oe3BLEnGxmzviPnjIKypudsTww3pYQQnRhBPfG/XoqelA aME9+gz0ANGzcIp5gMgrJk17WtVWvQfX5joJ8I0xsuvaeWU+iA1PLCM6OoXpBPjHB82I lNoroD78abIrlQMKkV8SOke6TvykMGWTKxfb5Jr1TWCAhtDFw8QX8Ja2R28oYP+0Al+D BMEg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=MIp7kBIS; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 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 agentk.vger.email (agentk.vger.email. [2620:137:e000::3:2]) by mx.google.com with ESMTPS id w16-20020a170902e89000b001ca87d0eea8si210693plg.475.2023.10.15.18.48.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 15 Oct 2023 18:48:04 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) client-ip=2620:137:e000::3:2; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=MIp7kBIS; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 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 agentk.vger.email (Postfix) with ESMTP id D6EF58058B50; Sun, 15 Oct 2023 18:48:01 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231250AbjJPBrt (ORCPT + 99 others); Sun, 15 Oct 2023 21:47:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33704 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231328AbjJPBro (ORCPT ); Sun, 15 Oct 2023 21:47:44 -0400 Received: from mail-pj1-x1029.google.com (mail-pj1-x1029.google.com [IPv6:2607:f8b0:4864:20::1029]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D50D3E1; Sun, 15 Oct 2023 18:47:42 -0700 (PDT) Received: by mail-pj1-x1029.google.com with SMTP id 98e67ed59e1d1-27d17f5457fso3049568a91.0; Sun, 15 Oct 2023 18:47:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1697420861; x=1698025661; 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=sG+9X4nVV+pHbrjt1Lng7ohViJzwy8aDHAaR38B9wtI=; b=MIp7kBIS/yoSBdepUlPZldhNS4fP9f2bcdz/X3aSp88nbnKW4JY+QrGkLhYnIN6i9T J9ZKQSnrU9kA/bYDQXruwMwnSJC0L7WZlAJ5R16fk4qSTQmhtaLGu5yRrMMix0WaLZr/ nphYUbcb9DLzHDdouR4KcQBWCvXNM1Qjvhcjk0Fx/+DLMCb2CSCTfA+vVi8Rb2ERKuOz wS4csYMY0AXmkVEvMWm0W1iPFa0coOc1TuEIg8kKZm0WLCLJR8Re15qYyj7aXGBZZGxF 9iU4YtG1CA5fW6pnpkBhy0WKl+BhZHPYa8m9Ykzh+rWuNZ98CuTz0vTl0yeXQBDz4yrB wIEg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697420861; x=1698025661; 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=sG+9X4nVV+pHbrjt1Lng7ohViJzwy8aDHAaR38B9wtI=; b=ljhJPdCidai855p1YaLQOaxK6UTiLdlOMm6kc1HSgvOLx/pCUF7Ypg4dZm0pIvPKco UFBZ9Bso/fdxf3/j22silrvR/Boa3iLCwyhRNd1YKknqmEeyCAH6EpuedllyDDXbw1qd Qciy5Z58DvTPWDLaIjLhuF7Z9ROh2PuU4Pesc/5tj4iOjcj6wYX9VjeJ4YB5g9pZ/3Ig wUsJH0naLvsqAyk3uKFterJFhBiOVQYRCUD9f+IITIpZHgNJSHzMdlDl8AOGUC27dJPX U0XHVd0y+MBysrDylK8Xwz9jzCxsx9PbvqMmt0i7Sl6HOEp6lFWhjG7jgM7ojylIKY5w 0oAQ== X-Gm-Message-State: AOJu0Yx08+8cEwzFGf7UtIiPQQA7I9NHBj9EMI9rWM4P7OL+qVMUgv6s P9D5osmvnqnG1Bxai8W8Y9gRFukllr8QMQ== X-Received: by 2002:a17:90b:1bcf:b0:27d:5568:e867 with SMTP id oa15-20020a17090b1bcf00b0027d5568e867mr8500687pjb.9.1697420861504; Sun, 15 Oct 2023 18:47:41 -0700 (PDT) Received: from ubuntu.. ([203.205.141.13]) by smtp.googlemail.com with ESMTPSA id pd17-20020a17090b1dd100b0027cfb5f010dsm3574377pjb.4.2023.10.15.18.47.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 15 Oct 2023 18:47:40 -0700 (PDT) From: Hengqi Chen To: linux-kernel@vger.kernel.org, bpf@vger.kernel.org Cc: keescook@chromium.org, ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, luto@amacapital.net, wad@chromium.org, alexyonghe@tencent.com, hengqi.chen@gmail.com Subject: [PATCH v2 3/5] seccomp: Introduce new flag SECCOMP_FILTER_FLAG_BPF_PROG_FD Date: Sun, 15 Oct 2023 23:29:51 +0000 Message-Id: <20231015232953.84836-4-hengqi.chen@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231015232953.84836-1-hengqi.chen@gmail.com> References: <20231015232953.84836-1-hengqi.chen@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-0.6 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on agentk.vger.email 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 (agentk.vger.email [0.0.0.0]); Sun, 15 Oct 2023 18:48:02 -0700 (PDT) Add a new flag SECCOMP_FILTER_FLAG_BPF_PROG_FD for SECCOMP_SET_MODE_FILTER. This indicates the seccomp filter is a seccomp bpf prog fd, not a sock_fprog. This 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 | 37 ++++++++++++++++++++++++++++++++++-- 3 files changed, 39 insertions(+), 3 deletions(-) diff --git a/include/linux/seccomp.h b/include/linux/seccomp.h index 175079552f68..7caa53b629d9 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_BPF_PROG_FD) /* 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..d6b243d1b4d5 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 signals) */ #define SECCOMP_FILTER_FLAG_WAIT_KILLABLE_RECV (1UL << 5) +/* Indicates that the filter is in form of bpf prog fd */ +#define SECCOMP_FILTER_FLAG_BPF_PROG_FD (1UL << 6) /* * All BPF programs must return a 32-bit value. diff --git a/kernel/seccomp.c b/kernel/seccomp.c index c9f6a19f7a4e..3a977e5932a4 100644 --- a/kernel/seccomp.c +++ b/kernel/seccomp.c @@ -525,7 +525,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 == BPF_PROG_TYPE_SECCOMP) + bpf_prog_put(filter->prog); + else + bpf_prog_destroy(filter->prog); kfree(filter); } } @@ -757,6 +760,33 @@ seccomp_prepare_user_filter(const char __user *user_filter) return sfilter; } +/** + * seccomp_prepare_filter_from_fd - prepares filter from a user-supplied fd + * @ufd: pointer to fd that refers to a seccomp bpf prog. + * + * 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 bpf_prog *prog; + int fd; + + if (copy_from_user(&fd, ufd, sizeof(fd))) + return ERR_PTR(-EFAULT); + + prog = bpf_prog_get_type(fd, BPF_PROG_TYPE_SECCOMP); + if (IS_ERR(prog)) + return ERR_PTR(-EBADF); + + sfilter = seccomp_prepare_filter(prog); + if (IS_ERR(sfilter)) + bpf_prog_put(prog); + + return sfilter; +} + #ifdef SECCOMP_ARCH_NATIVE /** * seccomp_is_const_allow - check if filter is constant allow with given data @@ -1970,7 +2000,10 @@ static long seccomp_set_mode_filter(unsigned int flags, return -EINVAL; /* Prepare the new filter before holding any locks. */ - prepared = seccomp_prepare_user_filter(filter); + if (flags & SECCOMP_FILTER_FLAG_BPF_PROG_FD) + prepared = seccomp_prepare_filter_from_fd(filter); + else + prepared = seccomp_prepare_user_filter(filter); if (IS_ERR(prepared)) return PTR_ERR(prepared); -- 2.34.1