From patchwork Mon Mar 7 05:29:18 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shung-Hsi Yu X-Patchwork-Id: 12771174 X-Patchwork-Delegate: bpf@iogearbox.net 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 87586C433EF for ; Mon, 7 Mar 2022 05:29:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234692AbiCGFa2 (ORCPT ); Mon, 7 Mar 2022 00:30:28 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48662 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231848AbiCGFa0 (ORCPT ); Mon, 7 Mar 2022 00:30:26 -0500 Received: from de-smtp-delivery-102.mimecast.com (de-smtp-delivery-102.mimecast.com [194.104.109.102]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 905C33B018 for ; Sun, 6 Mar 2022 21:29:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=mimecast20200619; t=1646630969; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type; bh=PYL2+nfFveOBDxu8oSAjkR6wgoWTMSulu+kLnrHAIKM=; b=AyhcOapLxRUIe9DJTRR4iW+GB47qM/fqlybJX0/rZYik/N5GrNjUmsLltSjFF2FVCE2B9o s3PfmIHEvu9EzMjZzg/V5rTxRztuZs3vGTNM5mC87gZBUaOgLxum9kYHzngYLITAWOVKqx V62oaInK3Tkcy436hAD9Rxa6kY0bCyk= Received: from EUR04-VI1-obe.outbound.protection.outlook.com (mail-vi1eur04lp2057.outbound.protection.outlook.com [104.47.14.57]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id de-mta-3-wssm1ythMjCLyW7Wtv3zuQ-1; Mon, 07 Mar 2022 06:29:28 +0100 X-MC-Unique: wssm1ythMjCLyW7Wtv3zuQ-1 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=eUWBVCf0cthj7MD1c5VTRfaaa25GM0PPbt0IZm4B3uN4WgXcFOy2CoqkR/T0DgUlRgsviWRKAsw99aJdY1NyGzEmJ3ctNd4G7gI/gEfnrSv5hjScfYcn31L7kzOPZETcnObP7Zw1lj1gdaxHLeYBMKGIEu+x9heKp0REzpm3nH1FEfg9mIFzASX6Tf/S+KZBXAwpIYYb+pYUX1NlkbCXWQsZ0eVu9Guj8Ek1OEg5T19XbY2h4tq10YwegHu0WJnFpbPsWZN/LgmTnqxXK7LLwS+ncvgjFek/2wlkTD8uY4lwaE0vt9XSIxFRYuE5C45Zxlsg+YIu2oS/RpicNN0Y9w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=PYL2+nfFveOBDxu8oSAjkR6wgoWTMSulu+kLnrHAIKM=; b=SjJF+Tlkp4eixsFs7YhSppyRIHMtMEGqLytCMGmlBalZiIkh+/a/hNjoxDa3cifLr5OuNLPOB63/KsXvLxOOMhpDsTFa2yju7UupXD1PKtz3wTgQnhjO2cFNrTHJaqUUw6S/0KpYhhvZw8txoVUZhdvggBAHPy6dCundN4xLLnit0P22KQIo134nm1ygAakalcVUsy6dzmpP+xQiy75v68yjJpotMlUuq754nyIyL1W3XRwTBQlf6706tSjV3a7W5oLZO4IVI2G9So+kGjLqYvdJeEeGwt7+JWyQ0KhKYYcRuW8Gm2VdJLjBblokJCmDYliN9RFJApSugQoxApf1SA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=suse.com; dmarc=pass action=none header.from=suse.com; dkim=pass header.d=suse.com; arc=none Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=suse.com; Received: from DB9PR04MB8107.eurprd04.prod.outlook.com (2603:10a6:10:243::20) by DBAPR04MB7206.eurprd04.prod.outlook.com (2603:10a6:10:1a4::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5038.13; Mon, 7 Mar 2022 05:29:27 +0000 Received: from DB9PR04MB8107.eurprd04.prod.outlook.com ([fe80::5d4:d76f:ecea:e459]) by DB9PR04MB8107.eurprd04.prod.outlook.com ([fe80::5d4:d76f:ecea:e459%9]) with mapi id 15.20.5038.026; Mon, 7 Mar 2022 05:29:27 +0000 Date: Mon, 7 Mar 2022 13:29:18 +0800 From: Shung-Hsi Yu To: bpf@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , netdev@vger.kernel.org Subject: [PATCH bpf-next] bpf: determine buf_info inside check_buffer_access() Message-ID: Content-Disposition: inline X-ClientProxiedBy: HK2PR0401CA0004.apcprd04.prod.outlook.com (2603:1096:202:2::14) To DB9PR04MB8107.eurprd04.prod.outlook.com (2603:10a6:10:243::20) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: e1465ea1-b428-483c-8c05-08d9fffb7257 X-MS-TrafficTypeDiagnostic: DBAPR04MB7206:EE_ X-Microsoft-Antispam-PRVS: X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 9FbX3T2VqJb74HWEHPuTjL6432q6GKJhJn+XrB8pQHeNucV5Sa6KJeNxdtWY6LI2VyCrntNndPlbRYyAq9W3Nn27KxaCov7qB07uQe/DJaCCj0eZ4GV6SxSRJpFISoonCeW02z7/EiFgZtpP1BzanZ2NRaIfojo4rCr9mE2YLwfNZ5Ej84O4pX8KQg+iGxAphQ0X9vQHocvi5oxG0bG2xjtuvKYy1UdvX/60MMnfVw/ikhAh4X2t5bpT6aXA5c/4oWDREZTmGoXBwA0CbX4YWBPHzcA6rF42a3zeyV+TjSDoIG0g55mpBIq8qh1sSIEc8yYnWhQ/IH8MSXhOXDRqszxsgQN8e35lk1PhuN7CiPhlRyE9GJWApFACyfpE0q34tYPpMDbMqy9PNUeBUqBffyH/Gwu/YbNeBnfrEE0ltAnCZOpAhNdWt9F474sc9Q37K55deEkz8BCbUbzRMMJBB6MVyZ5WQ1C06DN2tt0nIeEeukHKzGu+T0JG8ohJQu3zBsA8UahBrZbpsUheNdxE7N9X4bJ+3GBhi9R+TRkOzbs2DJUZV7wQip1XwiBRDsE1pVw2JfAAPubfp6iWhSfPlhyJt8Oyq+3lbe/Zs8FV+gp1OWdi9F4B8ocwm4cwlbXR/D2SjBWGxtKxZxUpyDQsNw== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:DB9PR04MB8107.eurprd04.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230001)(7916004)(366004)(8936002)(7416002)(66946007)(66556008)(4326008)(508600001)(6512007)(9686003)(6506007)(6666004)(83380400001)(33716001)(186003)(26005)(8676002)(66476007)(86362001)(2906002)(316002)(6486002)(54906003)(5660300002)(38100700002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?q?ybaTCCshT2n/a2CmBYkRt+yUaMPp?= =?utf-8?q?mPcUM/Gwo2lhhmvNi/MxC60FaGCW8AdWZwJWfv3F41sEjWlvTilFeT5Fqn4K8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-8?q?hd1frdON0zb12PeAqBcv0TNQWTOKxb0n5Q7Fxr9xlyiIbv3lyy3y7BwE8PsipK3Z1?= =?utf-8?q?u0g8TqgAV6SOmlWsohelnTsWRNIm7jtT++utW3quXkY/WuboHmHrMsfp1+xfzphQv?= =?utf-8?q?CXm5j64VGbJ2XMHzFtA+3D8w+ZcoDEqm8V1FbAfWdnLNEk9Lj+9iajTlOBTMVvmcc?= =?utf-8?q?rhQckBo0cV8Rt2FF/0Ra4iHXyA84sjfTxCggKj1Qy7e3sJk4/zuytMWqGu0DZ+hMH?= =?utf-8?q?dUoermCnLYEqzsydHSdx/tEpb6k0xaYGvMmtEK/G6B7pGI+vQqVVHtsSbKjM9D4ti?= =?utf-8?q?D/lEfbejDfqEhzf4Br5vdvFTL5V5pge5VAiP2p3eT0ySx/m2h5aWjQpL/Le6gvPC1?= =?utf-8?q?TPexlYkzwIp0YR83qubYfgibYqAmLcD6tDq7M3CgduT3fwMY/OL4vNvv24BcYqfBx?= =?utf-8?q?mxGBWxY/JWWj7KgSfZqRa4KOr0w5cO5DL0aMPOmdYAk7PXGFgEGWW7P/qIfzxIeAU?= =?utf-8?q?0UesWNfhatSEkJ9BnYWlfxRFAY82nu25EQnNuf97AlpEErApZUB6Khy1xUBMkPn02?= =?utf-8?q?v8ohiWOQVEqo032ggl7J7/lUBknM1aZpXg=3D=3D?= X-OriginatorOrg: suse.com X-MS-Exchange-CrossTenant-Network-Message-Id: e1465ea1-b428-483c-8c05-08d9fffb7257 X-MS-Exchange-CrossTenant-AuthSource: DB9PR04MB8107.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 07 Mar 2022 05:29:27.1103 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: f7a17af6-1c5c-4a36-aa8b-f5be247aa4ba X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: V4macZq3ALJI/kA3fuihijo+BxPt5gxWOm4zTA9CpDQS0CjGO0xliePSzTeSYeimsveuZ5XzD3ax8SsPq66o0Q== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DBAPR04MB7206 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net Instead of determining buf_info string in the caller of check_buffer_access(), we can determine whether the register type is read-only through type_is_rdonly_mem() helper inside check_buffer_access() and construct buf_info, making the code slightly cleaner. Signed-off-by: Shung-Hsi Yu Acked-by: Yonghong Song --- Initially I tried to remove the buf_info argument from __check_buffer_access(), however check_tp_buffer_access() uses "tracepoint" (rather than the usual "rdonly"/"rdwr") as it's buf_info, thus I decide to leave __check_buffer_access() as-is, and only change check_buffer_access() instead. kernel/bpf/verifier.c | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index a39eedecc93a..518238029e46 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -4047,9 +4047,9 @@ static int check_buffer_access(struct bpf_verifier_env *env, const struct bpf_reg_state *reg, int regno, int off, int size, bool zero_size_allowed, - const char *buf_info, u32 *max_access) { + const char *buf_info = type_is_rdonly_mem(reg->type) ? "rdonly" : "rdwr"; int err; err = __check_buffer_access(env, buf_info, reg, regno, off, size); @@ -4543,7 +4543,6 @@ static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regn value_regno); } else if (base_type(reg->type) == PTR_TO_BUF) { bool rdonly_mem = type_is_rdonly_mem(reg->type); - const char *buf_info; u32 *max_access; if (rdonly_mem) { @@ -4552,15 +4551,13 @@ static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regn regno, reg_type_str(env, reg->type)); return -EACCES; } - buf_info = "rdonly"; max_access = &env->prog->aux->max_rdonly_access; } else { - buf_info = "rdwr"; max_access = &env->prog->aux->max_rdwr_access; } err = check_buffer_access(env, reg, regno, off, size, false, - buf_info, max_access); + max_access); if (!err && value_regno >= 0 && (rdonly_mem || t == BPF_READ)) mark_reg_unknown(env, regs, value_regno); @@ -4823,7 +4820,6 @@ static int check_helper_mem_access(struct bpf_verifier_env *env, int regno, struct bpf_call_arg_meta *meta) { struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; - const char *buf_info; u32 *max_access; switch (base_type(reg->type)) { @@ -4850,15 +4846,13 @@ static int check_helper_mem_access(struct bpf_verifier_env *env, int regno, if (meta && meta->raw_mode) return -EACCES; - buf_info = "rdonly"; max_access = &env->prog->aux->max_rdonly_access; } else { - buf_info = "rdwr"; max_access = &env->prog->aux->max_rdwr_access; } return check_buffer_access(env, reg, regno, reg->off, access_size, zero_size_allowed, - buf_info, max_access); + max_access); case PTR_TO_STACK: return check_stack_range_initialized( env,