Received: by 2002:a05:7412:d8a:b0:e2:908c:2ebd with SMTP id b10csp2236591rdg; Sun, 15 Oct 2023 18:47:45 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHbWgLZXce9LNv/gKzMO9zQXOnViOjL1J4uOcVfJNNjrTSzIPJS2wmDXwTelp/ONALRgQo4 X-Received: by 2002:a92:d6d2:0:b0:357:6184:ba82 with SMTP id z18-20020a92d6d2000000b003576184ba82mr7897566ilp.9.1697420865715; Sun, 15 Oct 2023 18:47:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697420865; cv=none; d=google.com; s=arc-20160816; b=hDsXxAHL0WVOuAM+8il5UpOwpar63iejDX9VUwtZVVGrUPQqQFXPgUfyizP8GEoPFQ xVJVuM55QARdLGpxKiWE4/2BXFZ2PMA7s5zKcmevKmpFlPUgaBoEnexoo2Rj/8Ukt8JR ekMaLwM/s89v4FFw9PczYvnlkngAuQWKpEtm2QwX5CO6OETmteC5lPzpwzhQsuXTpdNb lb3w2iOjURPZBNNl7ywRivO5N4Fy1itkpgMtZ3D0K6gYput/WHB8V6zbiZm82mhgXUz9 BqNptUCuGfEp5uL4iPjKBBXy78Xdh2jGhWg3aN6YvP98lAoEb5Lyl+78FnZaVLZQZV2x QrCA== 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=jHaBMootb9r45Igd+ylGLOd17RhwzfVYT0h/J8CZlqY=; fh=UUVBB6hdZtgkdevQaSWXrPPcR6FiQNJo3IQxWCli3s0=; b=w+/GfC8+G7VUAQlnrqjbnE/n7wqHatk+3ZaxKEHDrGuLXT0ni56+AZlz3rmft4zl7D e54ZXuFtk8SYUelYyVcbjVNAi38CEtVS4uOlg+zXv18bKGDddC/y/GYl819057qqhIk/ Ho9x4lAJZX6R52EUXBzKOIiVb05DzldM+MmLCYIVtv6koeUNLQF/QpskLa5SdlTEGk85 oHZtrSrJb5bluBqnwd6r/xLRp68MV6slBHXVR5ZvRdj5YEfISzbDd52LWPycRcvDZAcJ RCJignyv6PgXGFI8YmYX7yenhOZXaeitmGt8CJaKvZUfypD92QQdvPDO0K6E8le91c2E F2ug== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=O0PzweRp; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 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 lipwig.vger.email (lipwig.vger.email. [2620:137:e000::3:3]) by mx.google.com with ESMTPS id d3-20020a633603000000b005b368301d97si3872785pga.305.2023.10.15.18.47.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 15 Oct 2023 18:47:45 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) client-ip=2620:137:e000::3:3; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=O0PzweRp; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 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 lipwig.vger.email (Postfix) with ESMTP id 7280080963B7; Sun, 15 Oct 2023 18:47:43 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231168AbjJPBrd (ORCPT + 99 others); Sun, 15 Oct 2023 21:47:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33500 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229642AbjJPBrc (ORCPT ); Sun, 15 Oct 2023 21:47:32 -0400 Received: from mail-pj1-x1035.google.com (mail-pj1-x1035.google.com [IPv6:2607:f8b0:4864:20::1035]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E5FC9DA; Sun, 15 Oct 2023 18:47:30 -0700 (PDT) Received: by mail-pj1-x1035.google.com with SMTP id 98e67ed59e1d1-27cfb8442f9so2343601a91.2; Sun, 15 Oct 2023 18:47:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1697420849; x=1698025649; 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=jHaBMootb9r45Igd+ylGLOd17RhwzfVYT0h/J8CZlqY=; b=O0PzweRpR+kxxWGKmR/pLQN3DpwPHvNwjesLnWZqY3pZ6wtIJqr4OUgsr3tCH4WS5M G1L6d2cSsVijzffs1GL8VCYld0pg0JAEPOehz02HKnCpgkCc28SIgDISgPUTHUnlHvIn BKoxMyjMX3IHb563bqRh1MPO79otOhOucTNAL/i+esTuaoMpoc+G78+JsVeNIVLhLgDF z1NcBiCF007Ql1jLIyFhto8djmLJnc4IhrJ9n8cDeYx1BVJppiRBCIEM9kBMCvnt6Lm9 bwgcvsSkPSR3RzGDrxL3Za8fwrlvR0k/hzWoNl00SlKjudZQhdfDZ5Cd4Kak04FpNXCy V3Rw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697420849; x=1698025649; 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=jHaBMootb9r45Igd+ylGLOd17RhwzfVYT0h/J8CZlqY=; b=H2SnO6nHjrS77ri6tRXSLNTOmRBENHs+jfZwS+9txrV8g3q9fFe0ob2wCd5LXemesk d/RhtXsgkFThoAvVZ+CRATadhVCrcleXJ5OsiPYiwG7GO/DhaczSsgcmPcOLT5+tSV9M UQhiBXkgjOQWHyW4nkdRcBuzg+52gzDnm+R89EzMSfQeuhlBF6gvWrR9KSX3b/PUQCf6 fjjrXsdXH1KpV9Iq3Rb+42T8MjCkofriH/ClyWzOmF36ANyQX1quxDZF0hY/qhF0skET BphlIxRLeFaCfYRQ6ujXb0elZQ+zvtFjrXVW/j7nsoJNIw1s+pYOKjPjaqqw1WsnZJ56 51dw== X-Gm-Message-State: AOJu0YzjdG05RbZKNwQexe1b4jsEd3teWrlAv0QRoD25jHSiwOw5/8Zd tc9zJdkPf4bQ1grUJEjDVtYYxyrsze50Ng== X-Received: by 2002:a17:90b:1b4b:b0:27d:3290:dee3 with SMTP id nv11-20020a17090b1b4b00b0027d3290dee3mr5965226pjb.2.1697420849567; Sun, 15 Oct 2023 18:47:29 -0700 (PDT) Received: from ubuntu.. ([203.205.141.13]) by smtp.googlemail.com with ESMTPSA id pd17-20020a17090b1dd100b0027cfb5f010dsm3574377pjb.4.2023.10.15.18.47.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 15 Oct 2023 18:47:28 -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 1/5] seccomp: Refactor filter copy/create for reuse Date: Sun, 15 Oct 2023 23:29:49 +0000 Message-Id: <20231015232953.84836-2-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 lipwig.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 (lipwig.vger.email [0.0.0.0]); Sun, 15 Oct 2023 18:47:43 -0700 (PDT) This extracts two helpers for reuse in subsequent additions. No functional change intended, just a prep work. Signed-off-by: Hengqi Chen --- kernel/seccomp.c | 90 +++++++++++++++++++++++++++++++++--------------- 1 file changed, 63 insertions(+), 27 deletions(-) diff --git a/kernel/seccomp.c b/kernel/seccomp.c index 255999ba9190..faf84fc892eb 100644 --- a/kernel/seccomp.c +++ b/kernel/seccomp.c @@ -640,14 +640,14 @@ static inline void seccomp_sync_threads(unsigned long flags) } /** - * seccomp_prepare_filter: Prepares a seccomp filter for use. - * @fprog: BPF program to install + * seccomp_prepare_prog - prepares a JITed BPF filter for use. + * @pfp: the unattached filter that is created + * @fprog: the filter program * - * Returns filter on success or an ERR_PTR on failure. + * Returns 0 on success and non-zero otherwise. */ -static struct seccomp_filter *seccomp_prepare_filter(struct sock_fprog *fprog) +static int seccomp_prepare_prog(struct bpf_prog **pfp, struct sock_fprog *fprog) { - struct seccomp_filter *sfilter; int ret; const bool save_orig = #if defined(CONFIG_CHECKPOINT_RESTORE) || defined(SECCOMP_ARCH_NATIVE) @@ -657,10 +657,27 @@ static struct seccomp_filter *seccomp_prepare_filter(struct sock_fprog *fprog) #endif if (fprog->len == 0 || fprog->len > BPF_MAXINSNS) - return ERR_PTR(-EINVAL); + return -EINVAL; BUG_ON(INT_MAX / fprog->len < sizeof(struct sock_filter)); + ret = bpf_prog_create_from_user(pfp, fprog, seccomp_check_filter, save_orig); + if (ret < 0) + return ret; + + return 0; +} + +/** + * seccomp_prepare_filter: Prepares a seccomp filter for use. + * @prog: BPF program to install + * + * Returns filter on success or an ERR_PTR on failure. + */ +static struct seccomp_filter *seccomp_prepare_filter(struct bpf_prog *prog) +{ + struct seccomp_filter *sfilter; + /* * Installing a seccomp filter requires that the task has * CAP_SYS_ADMIN in its namespace or be running with no_new_privs. @@ -677,13 +694,7 @@ static struct seccomp_filter *seccomp_prepare_filter(struct sock_fprog *fprog) return ERR_PTR(-ENOMEM); mutex_init(&sfilter->notify_lock); - ret = bpf_prog_create_from_user(&sfilter->prog, fprog, - seccomp_check_filter, save_orig); - if (ret < 0) { - kfree(sfilter); - return ERR_PTR(ret); - } - + sfilter->prog = prog; refcount_set(&sfilter->refs, 1); refcount_set(&sfilter->users, 1); init_waitqueue_head(&sfilter->wqh); @@ -692,31 +703,56 @@ static struct seccomp_filter *seccomp_prepare_filter(struct sock_fprog *fprog) } /** - * seccomp_prepare_user_filter - prepares a user-supplied sock_fprog + * seccomp_copy_user_filter - copies a user-supplied sock_fprog * @user_filter: pointer to the user data containing a sock_fprog. + * @fprog: pointer to store the copied BPF program * * Returns 0 on success and non-zero otherwise. */ -static struct seccomp_filter * -seccomp_prepare_user_filter(const char __user *user_filter) +static int seccomp_copy_user_filter(const char __user *user_filter, struct sock_fprog *fprog) { - struct sock_fprog fprog; - struct seccomp_filter *filter = ERR_PTR(-EFAULT); - #ifdef CONFIG_COMPAT if (in_compat_syscall()) { struct compat_sock_fprog fprog32; if (copy_from_user(&fprog32, user_filter, sizeof(fprog32))) - goto out; - fprog.len = fprog32.len; - fprog.filter = compat_ptr(fprog32.filter); + return -EFAULT; + fprog->len = fprog32.len; + fprog->filter = compat_ptr(fprog32.filter); } else /* falls through to the if below. */ #endif - if (copy_from_user(&fprog, user_filter, sizeof(fprog))) - goto out; - filter = seccomp_prepare_filter(&fprog); -out: - return filter; + if (copy_from_user(fprog, user_filter, sizeof(*fprog))) + return -EFAULT; + + return 0; +} + +/** + * seccomp_prepare_user_filter - prepares a user-supplied sock_fprog + * @user_filter: pointer to the user data containing a sock_fprog. + * + * Returns filter on success or an ERR_PTR on failure. + */ +static struct seccomp_filter * +seccomp_prepare_user_filter(const char __user *user_filter) +{ + struct seccomp_filter *sfilter; + struct sock_fprog fprog; + struct bpf_prog *prog; + int ret; + + ret = seccomp_copy_user_filter(user_filter, &fprog); + if (ret) + return ERR_PTR(ret); + + ret = seccomp_prepare_prog(&prog, &fprog); + if (ret) + return ERR_PTR(ret); + + sfilter = seccomp_prepare_filter(prog); + if (IS_ERR(sfilter)) + bpf_prog_destroy(prog); + + return sfilter; } #ifdef SECCOMP_ARCH_NATIVE -- 2.34.1