Received: by 2002:a05:7412:3784:b0:e2:908c:2ebd with SMTP id jk4csp1819443rdb; Tue, 3 Oct 2023 01:44:34 -0700 (PDT) X-Google-Smtp-Source: AGHT+IF0v6Upn7QLiureUEbFirn2zWqrcbggg7/MOkELz8pt+8VFOItXCBZC0uCMZraz51LRME1S X-Received: by 2002:a17:90a:f404:b0:274:84a2:f0d8 with SMTP id ch4-20020a17090af40400b0027484a2f0d8mr14081990pjb.25.1696322674489; Tue, 03 Oct 2023 01:44:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696322674; cv=none; d=google.com; s=arc-20160816; b=0wMbV6TMvUjDnZPUmadZMbb13QvrdFZwOfqY0LoZ8QOHlQip2axeSqWY7LSfwQC+Mx +ZwTbsVusZk8+g8VSQxM6yxGd/WPSvrmIRM1z0jYyqhph1PsvpGpCqDa6r23b4nTRAJx YofDA/XGXYjmEFbLhGXOVVEYhHFdAoivF/3oGV0p+rCcv4V2Bze8ml8+vS5ImfLVYZx1 I1FTA758xpz6o1KyMXCuWsDDC8DGi01QqnsPnHGFqzDqKLEU9w6AUQJVzvIAU/okONBx +dr4W11KkOL7Ez5fWdPsSlsyypgnrV+klcQ+jmGYhLH223H47SiaLZ9gcV99m9IBXklQ zQ3w== 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=H9i4lTlGpn05Zib6O3CFGJDGsZ1WXq3wp2jJyI4hXGQ=; fh=DRFGKMrXi0yLz3nURRYjQq65yfvW5/0oGnrPIKoecFQ=; b=aufF067uO9rQW1K6o6SzqmYoSYMgsBB/5pbd7qTNgOXo+UzJ4+NfNjQxsPha4YpT1v sJqioH9PtENYz4R/fQLacReggpz5MBID9PvuhpTYefGB2cfgtgGA9EniNtVvp5BBE0Dz pmJ9xqES/fVMxymc0/EnnwUTf+i9XrMsONW6EcKlJ8YEBZz/FDHwu/nMeh1RBNMDRGNx xUqRVlR8zetfWZvdT+8SjRmMoxZBxBNKZwzbdla2tN1BLwmj2xTJkYH5BSVpCHVRaxpU 5FT+0sTMcOEtO0Bj3DIWLM6guu/fLwLFRjeBHmao1IupXtFflMRwpBP3dIVezRePHRZr Di+w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=ItklvqAO; 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 oc14-20020a17090b1c0e00b0027927d7c0ebsi1024841pjb.45.2023.10.03.01.44.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 03 Oct 2023 01:44:34 -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=ItklvqAO; 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 62CBA80FA87A; Tue, 3 Oct 2023 01:44:31 -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 S231241AbjJCIoa (ORCPT + 99 others); Tue, 3 Oct 2023 04:44:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43142 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231223AbjJCIo2 (ORCPT ); Tue, 3 Oct 2023 04:44:28 -0400 Received: from mail-pl1-x635.google.com (mail-pl1-x635.google.com [IPv6:2607:f8b0:4864:20::635]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C046FAD; Tue, 3 Oct 2023 01:44:23 -0700 (PDT) Received: by mail-pl1-x635.google.com with SMTP id d9443c01a7336-1c724577e1fso4927895ad.0; Tue, 03 Oct 2023 01:44:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1696322663; x=1696927463; 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=H9i4lTlGpn05Zib6O3CFGJDGsZ1WXq3wp2jJyI4hXGQ=; b=ItklvqAOlbJyF2yYnQ4uGMLpl9Mb5wguTz5ECBBDTQ26hfJbbA6Uvq9SGgM89T71CU 1pMfhdLscLouaAuPyDHiG59hR0cxsz82isV8WuXUZXbu2nv2tGXLsodPh4TkZ4K5iU9m q+Hrp0b6EWWwjzRz29cihBReZpruwRNIXMdGGuAPuRhIjZn+CKZ43VhpDUiB0vVq7yng ZY+fCZnoURNUQD6f8LhTCkmYgKv9PdzGMp0CtlkpPTCesE5ZBhnYaNaHgbUtFu+olUtz CST1YKl8PLRh32KYaL2wT+mSSlMmCY0mjPxB0lylWvNu1JAEQ6b0nNxvQVLviRMT2ncj T8Qg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696322663; x=1696927463; 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=H9i4lTlGpn05Zib6O3CFGJDGsZ1WXq3wp2jJyI4hXGQ=; b=XO9aNCV9lGUfFYN8Jcn1sp3KHmxDnaaSxypFpOpHS9/n8UfHor6MVtUMa4RNx8IClS ujnX1CwFGJEnQ0tuJyT8cw65hSN6RtbeL8sB8huECsSPqPo7OQIEisuEpzVcSNd45b6C rOd37GV1dnBujMtXRL14r2y/sFoLPHQ09Ev0jKa3k8+e2YI+Z3VgaEPvnXrjNvcUz3xC 44Fr/VbxP89wuxlpsBNcelwjEUOw1OMnXDjKUtsVfr9zJJyuLaL7ibtLVTo6kqATv3yS qBdfWj/TF60Uo/Ex/QArifnggrkAaRTpcY2CdxmRG7UOXel48g/EvEqmXC9s2P/Pu8Vw meVA== X-Gm-Message-State: AOJu0Yz9sxTzPUdH6hxB/HhJGc75bap7iGzvqbZTrtUSc9nvF5AoOGsc MVtPKo/QqnJd77yYQux8cClVUrDf49e3eV+o X-Received: by 2002:a17:903:2348:b0:1c7:2661:91e1 with SMTP id c8-20020a170903234800b001c7266191e1mr16827636plh.15.1696322662929; Tue, 03 Oct 2023 01:44:22 -0700 (PDT) Received: from ubuntu.. ([113.64.184.44]) by smtp.googlemail.com with ESMTPSA id y16-20020a17090322d000b001bc445e249asm902876plg.124.2023.10.03.01.44.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 03 Oct 2023 01:44:22 -0700 (PDT) From: Hengqi Chen To: linux-kernel@vger.kernel.org, bpf@vger.kernel.org Cc: keescook@chromium.org, luto@amacapital.net, wad@chromium.org, alexyonghe@tencent.com, hengqi.chen@gmail.com Subject: [RFC PATCH 1/2] seccomp: Introduce SECCOMP_LOAD_FILTER operation Date: Tue, 3 Oct 2023 08:38:35 +0000 Message-Id: <20231003083836.100706-2-hengqi.chen@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231003083836.100706-1-hengqi.chen@gmail.com> References: <20231003083836.100706-1-hengqi.chen@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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]); Tue, 03 Oct 2023 01:44:31 -0700 (PDT) This patch adds a new operation named SECCOMP_LOAD_FILTER. It accepts the same arguments as SECCOMP_SET_MODE_FILTER but only performs the loading process. If succeed, return a new fd associated with the JITed BPF program (the filter). The filter can then be pinned to bpffs using the returned fd and reused for different processes. Signed-off-by: Hengqi Chen --- include/uapi/linux/seccomp.h | 1 + kernel/seccomp.c | 64 ++++++++++++++++++++++++++++++++++++ 2 files changed, 65 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 /* 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..7aff22f56a91 100644 --- a/kernel/seccomp.c +++ b/kernel/seccomp.c @@ -1996,12 +1996,71 @@ static long seccomp_set_mode_filter(unsigned int flags, seccomp_filter_free(prepared); return ret; } + +static long seccomp_load_filter(const char __user *filter) +{ + struct sock_fprog fprog; + struct bpf_prog *prog; + int ret = -EFAULT; + const bool save_orig = +#if defined(CONFIG_CHECKPOINT_RESTORE) || defined(SECCOMP_ARCH_NATIVE) + true; +#else + false; +#endif + +#ifdef CONFIG_COMPAT + if (in_compat_syscall()) { + struct compat_sock_fprog fprog32; + if (copy_from_user(&fprog32, filter, sizeof(fprog32))) + goto out; + fprog.len = fprog32.len; + fprog.filter = compat_ptr(fprog32.filter); + } else /* falls through to the if below. */ +#endif + if (copy_from_user(&fprog, filter, sizeof(fprog))) + goto out; + + ret = -EINVAL; + if (fprog.len == 0 || fprog.len > BPF_MAXINSNS) + goto out; + + BUG_ON(INT_MAX / fprog.len < sizeof(struct sock_filter)); + + ret = bpf_prog_create_from_user(&prog, &fprog, seccomp_check_filter, save_orig); + if (ret < 0) + goto out; + + ret = security_bpf_prog_alloc(prog->aux); + if (ret) { + ret = -EINVAL; + goto prog_free; + } + + prog->aux->user = get_current_user(); + atomic64_set(&prog->aux->refcnt, 1); + + ret = bpf_prog_new_fd(prog); + if (ret < 0) + bpf_prog_put(prog); + return ret; + +prog_free: + bpf_prog_free(prog); +out: + return ret; +} #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 static long seccomp_get_action_avail(const char __user *uaction) @@ -2063,6 +2122,11 @@ static long do_seccomp(unsigned int op, unsigned int flags, return -EINVAL; return seccomp_get_notif_sizes(uargs); + case SECCOMP_LOAD_FILTER: + if (flags != 0) + return -EINVAL; + + return seccomp_load_filter(uargs); default: return -EINVAL; } -- 2.34.1