From patchwork Tue Feb 6 06:38:31 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kui-Feng Lee X-Patchwork-Id: 13546717 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-yw1-f181.google.com (mail-yw1-f181.google.com [209.85.128.181]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2F231127B73 for ; Tue, 6 Feb 2024 06:38:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.181 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707201520; cv=none; b=THpXJMbdfC6Px2/Fa+Jq1z0TLq3TAfJrzNiPvY7h5aRIFKiWwFcO/OcRTj/4A23zm3Qf+RSXYTZm9ACWPmxbR/8vA9tdzWH4c3Uk+q1kjL2ct9CGQNbGCiBgt6YIpqPiB/aGQsw6rrDoi6SjtgBJEgoElH0OMPm19lF9h4I3cdk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707201520; c=relaxed/simple; bh=SLnnAWMh70aleO3hatX5bZMy8RdZkVmD4faASqQ7FFE=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=B1oQJzq6CmbXwX9oo/M01FBlFoNOsjpR7TL3KOsqQpiqs+FXV9c+jLKr63Y81+ONF8a87U+RZideLcnGabzbUpGWXHjHzIsXq6vHhYth0/of6UeolotTjTw4on/jccYiVog2MNmPAYCjO6zq05Ms7kIRwfHqGeOOOEtBSkHpMR4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=jf8jlOPM; arc=none smtp.client-ip=209.85.128.181 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="jf8jlOPM" Received: by mail-yw1-f181.google.com with SMTP id 00721157ae682-5edfcba97e3so52202367b3.2 for ; Mon, 05 Feb 2024 22:38:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1707201517; x=1707806317; 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=b6xWq6VF7W/r4HC8BvQEl1/+gIS1iF5UppNVvadQ1zk=; b=jf8jlOPMXfkomPQDwpjoOCxjg1THnuK1NMfGI8Pjta46gJMqtng0iS+VHMtVrxDq4U K+aO6gdZE3q/2UD69yTTb6Q/Td836hJvR9JJc3YBaXv3B6OGvFinIcAlMUfzSkXiys8A lD7s5fLunzJ2Hp7yyR6weKNnBlV5fZAszs12KTs9E0phAbxnoU8CSNH9lfKoHs7agr3c s0BBxWeXXhrz7Arb4+oIC/517sgh4OLTXTVSBGwS6GLjHu9mLqq5oBc3qO98oIDsJ2gD tYjFLnpd/bX/gpsCQoodickHOGCKJt9EI+MHCIjGhkygXNZYEbu7UOTZuVXwZ1lgVTh3 X20w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707201517; x=1707806317; 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=b6xWq6VF7W/r4HC8BvQEl1/+gIS1iF5UppNVvadQ1zk=; b=Y6Rc1hf8taSE0dOM7IxbQiU5XeUlMLgp7xHR0eVPx9D6HuvjsOQTWzeqlz9QMabNBw Qiic+bjEKB4PB8culXnuBwEwSpamWJ7XDMlJ2rjJnWg4pGDx7MvaSwY7vuLQ86QgA7XZ bVH0m0TqiUO5qA9lq9w5Iv7q3aebZcRif0xoUVQHRMYrBSPU+UFNzo1FA5yKwcUdz12Q cOvvi9boXfdqgTxKHlIk80mU8pCi5RwmkpKk7S4svXwe9z4TlmEuHAyumc00C+pzJX/J UJBrm6w3wXf5Cbq4Gl3eGiJY+D2PqW3nIEHv0Ft9Vw4eMjCfutOU66ri+LRkL+O/Flxw QoVA== X-Gm-Message-State: AOJu0YxA+4z8uRHT8CbbVXqN5O0tTmsS+VjI6iItF8hRtCC1eWrxN8QN 0COe54UTMwerXeljY5O1DgfE+pecjWPKfH41PqJ+KQsjWA97gyUshGxRyQ4/cCk= X-Google-Smtp-Source: AGHT+IGGysi01DNvpuLm3f0WnQNKgX59BdPP2uy8KFa1ob5/akUG1CjUWQbB91pKn9JIloB8hyk+xA== X-Received: by 2002:a81:d108:0:b0:5ff:76c5:f638 with SMTP id w8-20020a81d108000000b005ff76c5f638mr738336ywi.21.1707201517621; Mon, 05 Feb 2024 22:38:37 -0800 (PST) X-Forwarded-Encrypted: i=0; AJvYcCWeaeEXqUyp7erESg2H4zsJydsu++9tvmzpBKvh4f99CEa4b4msmk2qycFxtBrs5kdjgysKAhl380gdba9P+dbaNf5pWDJy/I1hbW9TVCxsoQrdVCY1nbHsBRYHDizFHVTlo4Yd/5j6T6aHVgs5VxvyqTKsU/cmQgXToe8wxxsCV4PI6VeL6tksqd/NE3y2c7ua9iKo4IBuXsRsYZJ+DZWTbsV21KKhbE7d9tyK28SAQsPgdoKcripQe8EHDJlqbSvqKtq104zinxcZj3218t5asRZv4S37upVcDHvvLYa8MlQ= Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:3a27:6d1a:7c79:c81e]) by smtp.gmail.com with ESMTPSA id ez9-20020a05690c308900b005ffb91a94e6sm64277ywb.59.2024.02.05.22.38.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 Feb 2024 22:38:37 -0800 (PST) From: thinker.li@gmail.com To: bpf@vger.kernel.org, ast@kernel.org, martin.lau@linux.dev, song@kernel.org, kernel-team@meta.com, andrii@kernel.org, davemarchevsky@meta.com, dvernet@meta.com Cc: sinquersw@gmail.com, kuifeng@meta.com, Kui-Feng Lee Subject: [PATCH bpf-next v5 1/3] bpf: add btf pointer to struct bpf_ctx_arg_aux. Date: Mon, 5 Feb 2024 22:38:31 -0800 Message-Id: <20240206063833.2520479-2-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240206063833.2520479-1-thinker.li@gmail.com> References: <20240206063833.2520479-1-thinker.li@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net From: Kui-Feng Lee Enable the providers to use types defined in a module instead of in the kernel (btf_vmlinux). Signed-off-by: Kui-Feng Lee --- include/linux/bpf.h | 1 + kernel/bpf/btf.c | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index 1ebbee1d648e..9a2ee9456989 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -1416,6 +1416,7 @@ struct bpf_ctx_arg_aux { u32 offset; enum bpf_reg_type reg_type; u32 btf_id; + struct btf *btf; }; struct btf_mod_pair { diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c index f7725cb6e564..aa72674114af 100644 --- a/kernel/bpf/btf.c +++ b/kernel/bpf/btf.c @@ -6266,7 +6266,7 @@ bool btf_ctx_access(int off, int size, enum bpf_access_type type, } info->reg_type = ctx_arg_info->reg_type; - info->btf = btf_vmlinux; + info->btf = ctx_arg_info->btf ? ctx_arg_info->btf : btf_vmlinux; info->btf_id = ctx_arg_info->btf_id; return true; } From patchwork Tue Feb 6 06:38:32 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kui-Feng Lee X-Patchwork-Id: 13546718 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-yw1-f181.google.com (mail-yw1-f181.google.com [209.85.128.181]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8CE3B1802A for ; Tue, 6 Feb 2024 06:38:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.181 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707201522; cv=none; b=rIBOk6pZo8FlmXbEEY4/y6RSpUxbmAfZCokCO9YwJt9YJPEPpp5XRjWHTfJCY4S2H/n0/Sl9VXgGKRliCtvYwawkI6ppq5tT1teCKlUQ4mkMCvOcv/VJ93PL7E7fvkxYROBFGPC0Dshu78asztfVUxGw21wvFI3K2yTKXnQonRQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707201522; c=relaxed/simple; bh=j3RihJRtuHyHGFf92sKUHfzbLbcPPIYQEtA+chOIC78=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=GpvhIeFSMd3NQl6MBOrPzAuuFN6REs+ivnmyVxyOmKCV1ftTTwHaTZ0OE3wk26DrnEp7E+sqmvD0ZYdD4d0qlEMJWpNciqyErs6qG1w0Q8zuGgRkGvubGo0xE0mdkWIILmaVqOTJbTJ5etbNVMPoIuaNPZaBBQx9O9BJZrUo6+k= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=TBBXhl7g; arc=none smtp.client-ip=209.85.128.181 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="TBBXhl7g" Received: by mail-yw1-f181.google.com with SMTP id 00721157ae682-6040d33380cso54203317b3.1 for ; Mon, 05 Feb 2024 22:38:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1707201519; x=1707806319; 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=6ULnswL6o5lkj9ReIFVMsJFgJqihE57nJfIdfhIR/XA=; b=TBBXhl7gTlI08G8EA9Tg3Po9dCYuna52ps2XlGnmMqA+jZgkNU3YADpAwX1h3CGXTe XndAOfMOTj7VgKYPz9mnMC9Hzr7j7ozHEza8l33mJVI679Nlv9JGMUe0a3YeM8WILUDE 0BG74QO3svxz5JtvqjgMACgV9Sli3BgR75OqyCzeCrnuMOUqVCPXSQcqnNKj3VHNIQY/ VEN//nZiUBIhVWNzISm8/1k2FzpTHzVCEG1qoiFHwDFIxKlr7lBaLA5bFdwY9Yw5mKP8 Yqc/dANBCIJ+2zas5ngxKI694aglUqWwSxrrpJ7YUWTC5cB/4l8RIzsi9FT31zEKE7s1 4/yw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707201519; x=1707806319; 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=6ULnswL6o5lkj9ReIFVMsJFgJqihE57nJfIdfhIR/XA=; b=WTymjG7fiyp1y8ZLLb5zD82VcDYH/7dX0/EhjY+Qjp8I7Oy1ttClTjnKAEU4O6RzHF s31ffuzGJJqiNEVtm3yERwbgcqAi2BRmkpVuoAO0iCSmLxPhyR3nDGq4LqOXA0DZ9VYE M6alBcERn95sBGmI6PNj53bfmMfRWt3yxz5Ed0UblFVzjffV1wiWSnbH5CkkrWf/PWTw 39zMgPAwe5JAyNK5NbdotIlz/wAtKAALdxOxtwxWK20oFtAs3v2Nxpye+hGx0VpMtilH Gnr/dEvp6OX/DIsXhhc0xZFWbz5UTGrOTEeREMR7HAI25yuAHdSg8bh4sNdRSVFxc9ld LU7g== X-Gm-Message-State: AOJu0YwR/STl40IynB199HxyVml/Ja5wu9TSegda38BObUJDNGJbeSNi CSJ5qGP54VkyIg663xUb+uJcEWsNQIlzYrEQg+xhMlH12lit6cpq57bq1Pf0Xd4= X-Google-Smtp-Source: AGHT+IGrshhgKs+kyGBAa8F44rO3R0UNJKPiVTJKYg7IKs9am5vczI9pWxte/NjtU7F9LBZ1X/gv8g== X-Received: by 2002:a81:bd07:0:b0:5ff:aafc:32c7 with SMTP id b7-20020a81bd07000000b005ffaafc32c7mr951420ywi.26.1707201518856; Mon, 05 Feb 2024 22:38:38 -0800 (PST) X-Forwarded-Encrypted: i=0; AJvYcCUi+DmcQmK1Fx265eprtNf6gfF7cXZ+0CIoqnC2Xd26aDp7WrDAKvBubVJvZ1xnB93ryiO8HDKXNZKheoQFBEVewk7Oq5FcsUo3O7j3FC6Kxn2EHhu7qMJasVf09YfrQtDadUdLdCrkKxBPlZsm8CR3WqWBQtok1EzfahcW9jk24QSAD6jznk2Lxuvg3qEh8ClaciX+nqaB909m0CeihHn+6F0yG1dBpj+eudhfRhQd9zuxvsHRZ1LSgh4LUFBZUv9lIkh/dlSld3aER+oE7DByMm5RjjbYaQ6nMUu8tsQzeDI= Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:3a27:6d1a:7c79:c81e]) by smtp.gmail.com with ESMTPSA id ez9-20020a05690c308900b005ffb91a94e6sm64277ywb.59.2024.02.05.22.38.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 Feb 2024 22:38:38 -0800 (PST) From: thinker.li@gmail.com To: bpf@vger.kernel.org, ast@kernel.org, martin.lau@linux.dev, song@kernel.org, kernel-team@meta.com, andrii@kernel.org, davemarchevsky@meta.com, dvernet@meta.com Cc: sinquersw@gmail.com, kuifeng@meta.com, Kui-Feng Lee Subject: [PATCH bpf-next v5 2/3] bpf: Create argument information for nullable arguments. Date: Mon, 5 Feb 2024 22:38:32 -0800 Message-Id: <20240206063833.2520479-3-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240206063833.2520479-1-thinker.li@gmail.com> References: <20240206063833.2520479-1-thinker.li@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net From: Kui-Feng Lee Collect argument information from the type information of stub functions to mark arguments of BPF struct_ops programs with PTR_MAYBE_NULL if they are nullable. A nullable argument is annotated by suffixing "__nullable" at the argument name of stub function. For nullable arguments, this patch sets an arg_info to label their reg_type with PTR_TO_BTF_ID | PTR_TRUSTED | PTR_MAYBE_NULL. This makes the verifier to check programs and ensure that they properly check the pointer. The programs should check if the pointer is null before accessing the pointed memory. The implementer of a struct_ops type should annotate the arguments that can be null. The implementer should define a stub function (empty) as a placeholder for each defined operator. The name of a stub function should be in the pattern "__". For example, for test_maybe_null of struct bpf_testmod_ops, it's stub function name should be "bpf_testmod_ops__test_maybe_null". You mark an argument nullable by suffixing the argument name with "__nullable" at the stub function. Since we already has stub functions for kCFI, we just reuse these stub functions with the naming convention mentioned earlier. These stub functions with the naming convention is only required if there are nullable arguments to annotate. For functions having not nullable arguments, stub functions are not necessary for the purpose of this patch. This patch will prepare a list of struct bpf_ctx_arg_aux, aka arg_info, for each member field of a struct_ops type. "arg_info" will be assigned to "prog->aux->ctx_arg_info" of BPF struct_ops programs in check_struct_ops_btf_id() so that it can be used by btf_ctx_access() later to set reg_type properly for the verifier. Signed-off-by: Kui-Feng Lee --- include/linux/bpf.h | 18 ++++ kernel/bpf/bpf_struct_ops.c | 185 ++++++++++++++++++++++++++++++++++-- kernel/bpf/btf.c | 40 ++++++++ kernel/bpf/verifier.c | 6 ++ 4 files changed, 242 insertions(+), 7 deletions(-) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index 9a2ee9456989..29d9ec1c4fd9 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -1709,6 +1709,19 @@ struct bpf_struct_ops { struct btf_func_model func_models[BPF_STRUCT_OPS_MAX_NR_MEMBERS]; }; +/* Every member of a struct_ops type has an instance even the member is not + * an operator (function pointer). The "arg_info" field will be assigned to + * prog->aux->arg_info of BPF struct_ops programs to provide the argument + * information required by the verifier to verify the program. + * + * btf_ctx_access() will lookup prog->aux->arg_info to find the + * corresponding entry for an given argument. + */ +struct bpf_struct_ops_member_arg_info { + struct bpf_ctx_arg_aux *arg_info; + u32 arg_info_cnt; +}; + struct bpf_struct_ops_desc { struct bpf_struct_ops *st_ops; @@ -1716,6 +1729,9 @@ struct bpf_struct_ops_desc { const struct btf_type *value_type; u32 type_id; u32 value_id; + + /* Collection of argument information for each member */ + struct bpf_struct_ops_member_arg_info *member_arg_info; }; enum bpf_struct_ops_state { @@ -2500,6 +2516,8 @@ int bpf_prog_test_run_sk_lookup(struct bpf_prog *prog, int bpf_prog_test_run_nf(struct bpf_prog *prog, const union bpf_attr *kattr, union bpf_attr __user *uattr); +int btf_ctx_arg_offset(struct btf *btf, const struct btf_type *func_proto, + u32 arg_no); bool btf_ctx_access(int off, int size, enum bpf_access_type type, const struct bpf_prog *prog, struct bpf_insn_access_aux *info); diff --git a/kernel/bpf/bpf_struct_ops.c b/kernel/bpf/bpf_struct_ops.c index f98f580de77a..0db7e12a9244 100644 --- a/kernel/bpf/bpf_struct_ops.c +++ b/kernel/bpf/bpf_struct_ops.c @@ -116,17 +116,162 @@ static bool is_valid_value_type(struct btf *btf, s32 value_id, return true; } +#define MAYBE_NULL_SUFFIX "__nullable" +#define MAX_STUB_NAME 128 + +static bool match_nullable_suffix(const char *name) +{ + int suffix_len, len; + + if (!name) + return false; + + suffix_len = sizeof(MAYBE_NULL_SUFFIX) - 1; + len = strlen(name); + if (len <= suffix_len) + return false; + + return !strcmp(name + len - suffix_len, MAYBE_NULL_SUFFIX); +} + +/* Return the type info of a stub function, if it exists. + * + * The name of the stub function is made up of the name of the struct_ops + * and the name of the function pointer member, separated by "__". For + * example, if the struct_ops is named "foo_ops" and the function pointer + * member is named "bar", the stub function name would be "foo_ops__bar". + */ +static const struct btf_type * +find_stub_func_proto(struct btf *btf, const char *st_op_name, + const char *member_name) +{ + char stub_func_name[MAX_STUB_NAME]; + const struct btf_type *t, *func_proto; + s32 btf_id; + + snprintf(stub_func_name, MAX_STUB_NAME, "%s__%s", + st_op_name, member_name); + btf_id = btf_find_by_name_kind(btf, stub_func_name, BTF_KIND_FUNC); + if (btf_id < 0) + return NULL; + t = btf_type_by_id(btf, btf_id); + if (!t) + return NULL; + func_proto = btf_type_by_id(btf, t->type); + + return func_proto; +} + +/* Prepare argument info for every nullable argument of a member of a + * struct_ops type. + * + * Initialize a struct bpf_struct_ops_member_arg_info according to type + * info of the arguments of a stub function. (Check kCFI for more + * information about stub functions.) + * + * Each member in the struct_ops type has a struct + * bpf_struct_ops_member_arg_info to provide an array of struct + * bpf_ctx_arg_aux, which in turn provides the information that used by the + * verifier to check the arguments of the BPF struct_ops program assigned + * to the member. Here, we only care about the arguments that are marked as + * __nullable. + * + * The array of struct bpf_ctx_arg_aux is eventually assigned to + * prog->aux->ctx_arg_info of BPF struct_ops programs and passed to the + * verifier. (See check_struct_ops_btf_id()) + * + * member_arg_info->arg_info will be the list of struct bpf_ctx_arg_aux if + * success. If fails, it will be kept untouched. + */ +static int prepare_arg_info(struct btf *btf, + const char *st_ops_name, + const char *member_name, + const struct btf_type *func_proto, + struct bpf_struct_ops_member_arg_info *member_arg_info) +{ + const struct btf_type *stub_func_proto, *pointed_type; + const struct btf_param *args, *member_args; + struct bpf_ctx_arg_aux *arg_info, *ai_buf; + u32 nargs, arg_no, arg_info_cnt = 0; + const char *arg_name; + s32 arg_btf_id; + int offset; + + stub_func_proto = find_stub_func_proto(btf, st_ops_name, member_name); + if (!stub_func_proto) + return 0; + + args = btf_params(stub_func_proto); + nargs = btf_type_vlen(stub_func_proto); + if (nargs != btf_type_vlen(func_proto)) { + pr_warn("the number of arguments of the stub function %s__%s does not match the number of arguments of the member %s of struct %s\n", + st_ops_name, member_name, member_name, st_ops_name); + return -EINVAL; + } + + member_args = btf_params(func_proto); + + ai_buf = kcalloc(nargs, sizeof(*ai_buf), GFP_KERNEL); + if (!ai_buf) + return -ENOMEM; + + for (arg_no = 0; arg_no < nargs; arg_no++) { + /* Skip arguments that is not suffixed with + * "__nullable". + */ + arg_name = btf_name_by_offset(btf, + args[arg_no].name_off); + if (!match_nullable_suffix(arg_name)) + continue; + + /* Should be a pointer to struct, array, scalar, or enum */ + pointed_type = btf_type_resolve_ptr(btf, + member_args[arg_no].type, + &arg_btf_id); + if (!pointed_type || + !btf_type_is_struct(pointed_type)) + goto err_out; + + offset = btf_ctx_arg_offset(btf, stub_func_proto, arg_no); + if (offset < 0) + goto err_out; + + /* Fill the information of the new argument */ + arg_info = ai_buf + arg_info_cnt++; + arg_info->reg_type = + PTR_TRUSTED | PTR_TO_BTF_ID | PTR_MAYBE_NULL; + arg_info->btf_id = arg_btf_id; + arg_info->btf = btf; + arg_info->offset = offset; + } + + if (arg_info_cnt) { + member_arg_info->arg_info = ai_buf; + member_arg_info->arg_info_cnt = arg_info_cnt; + } else { + kfree(ai_buf); + } + + return 0; + +err_out: + kfree(ai_buf); + + return -EINVAL; +} + int bpf_struct_ops_desc_init(struct bpf_struct_ops_desc *st_ops_desc, struct btf *btf, struct bpf_verifier_log *log) { + struct bpf_struct_ops_member_arg_info *member_arg_info; struct bpf_struct_ops *st_ops = st_ops_desc->st_ops; const struct btf_member *member; const struct btf_type *t; s32 type_id, value_id; char value_name[128]; const char *mname; - int i; + int i, err; if (strlen(st_ops->name) + VALUE_PREFIX_LEN >= sizeof(value_name)) { @@ -160,6 +305,11 @@ int bpf_struct_ops_desc_init(struct bpf_struct_ops_desc *st_ops_desc, if (!is_valid_value_type(btf, value_id, t, value_name)) return -EINVAL; + member_arg_info = kcalloc(btf_type_vlen(t), sizeof(*member_arg_info), + GFP_KERNEL); + if (!member_arg_info) + return -ENOMEM; + for_each_member(i, t, member) { const struct btf_type *func_proto; @@ -167,32 +317,44 @@ int bpf_struct_ops_desc_init(struct bpf_struct_ops_desc *st_ops_desc, if (!*mname) { pr_warn("anon member in struct %s is not supported\n", st_ops->name); - return -EOPNOTSUPP; + err = -EOPNOTSUPP; + goto errout; } if (__btf_member_bitfield_size(t, member)) { pr_warn("bit field member %s in struct %s is not supported\n", mname, st_ops->name); - return -EOPNOTSUPP; + err = -EOPNOTSUPP; + goto errout; } func_proto = btf_type_resolve_func_ptr(btf, member->type, NULL); - if (func_proto && - btf_distill_func_proto(log, btf, + if (!func_proto) + continue; + + if (btf_distill_func_proto(log, btf, func_proto, mname, &st_ops->func_models[i])) { pr_warn("Error in parsing func ptr %s in struct %s\n", mname, st_ops->name); - return -EINVAL; + err = -EINVAL; + goto errout; } + + err = prepare_arg_info(btf, st_ops->name, mname, + func_proto, + member_arg_info + i); + if (err) + goto errout; } if (st_ops->init(btf)) { pr_warn("Error in init bpf_struct_ops %s\n", st_ops->name); - return -EINVAL; + err = -EINVAL; + goto errout; } st_ops_desc->type_id = type_id; @@ -200,7 +362,16 @@ int bpf_struct_ops_desc_init(struct bpf_struct_ops_desc *st_ops_desc, st_ops_desc->value_id = value_id; st_ops_desc->value_type = btf_type_by_id(btf, value_id); + st_ops_desc->member_arg_info = member_arg_info; + return 0; + +errout: + while (i > 0) + kfree(member_arg_info[--i].arg_info); + kfree(member_arg_info); + + return err; } static int bpf_struct_ops_map_get_next_key(struct bpf_map *map, void *key, diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c index aa72674114af..6df390ade2c0 100644 --- a/kernel/bpf/btf.c +++ b/kernel/bpf/btf.c @@ -1699,6 +1699,21 @@ static void btf_free_struct_meta_tab(struct btf *btf) static void btf_free_struct_ops_tab(struct btf *btf) { struct btf_struct_ops_tab *tab = btf->struct_ops_tab; + struct bpf_struct_ops_member_arg_info *ma_info; + int i, j; + + if (!tab) + return; + + for (i = 0; i < tab->cnt; i++) { + ma_info = tab->ops[i].member_arg_info; + if (!ma_info) + continue; + + for (j = 0; j < btf_type_vlen(tab->ops[i].type); j++) + kfree(ma_info[j].arg_info); + kfree(ma_info); + } kfree(tab); btf->struct_ops_tab = NULL; @@ -6130,6 +6145,31 @@ static bool prog_args_trusted(const struct bpf_prog *prog) } } +int btf_ctx_arg_offset(struct btf *btf, const struct btf_type *func_proto, + u32 arg_no) +{ + const struct btf_param *args; + const struct btf_type *t; + int off = 0, i; + u32 sz, nargs; + + nargs = btf_type_vlen(func_proto); + /* It is the return value if arg_no == nargs */ + if (arg_no > nargs) + return -EINVAL; + + args = btf_params(func_proto); + for (i = 0; i < arg_no; i++) { + t = btf_type_by_id(btf, args[i].type); + t = btf_resolve_size(btf, t, &sz); + if (IS_ERR(t)) + return -EINVAL; + off += roundup(sz, 8); + } + + return off; +} + bool btf_ctx_access(int off, int size, enum bpf_access_type type, const struct bpf_prog *prog, struct bpf_insn_access_aux *info) diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 64fa188d00ad..8d7e761cda0d 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -20433,6 +20433,12 @@ static int check_struct_ops_btf_id(struct bpf_verifier_env *env) } } + /* btf_ctx_access() used this to provide argument type info */ + prog->aux->ctx_arg_info = + st_ops_desc->member_arg_info[member_idx].arg_info; + prog->aux->ctx_arg_info_size = + st_ops_desc->member_arg_info[member_idx].arg_info_cnt; + prog->aux->attach_func_proto = func_proto; prog->aux->attach_func_name = mname; env->ops = st_ops->verifier_ops; From patchwork Tue Feb 6 06:38:33 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kui-Feng Lee X-Patchwork-Id: 13546719 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-yw1-f171.google.com (mail-yw1-f171.google.com [209.85.128.171]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CFA35127B73 for ; Tue, 6 Feb 2024 06:38:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707201523; cv=none; b=F/MWRmx47hkPTOJSY0YPzY60iOGaf5aUeK0W2IRMwxPejz0y1WDC9V3BDXZpCgQ86NENIogxJxacR9pxDYcxysS/9fa+TXCr8IqeIbLmKb2gC66bgHLi6bh3DAQuGiBEIN8qbMUzsf0G75w/5uzu/XnfLB+agw9qzL3MDiQzDH0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707201523; c=relaxed/simple; bh=a0jlfop1Qv+gTC6pGRB6DtDr8UkdNM8YTgEaKyma1so=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=F2i2wW4R8ZobR89xrz3o2TS/hoW3g3tSNNgyyad25g9T0AsKtY1hxQOcD31jnNcJjCYpcs7t6dMQLu/o8j71/yGIR1C/JWiP4wnK9nROMoig6Bz5fcCXUSU6Ut/vh6+POXvz5kDpylsg/ldderGIYjo/OpXtStNhn7eZhJkjD9A= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=OlWM4BsA; arc=none smtp.client-ip=209.85.128.171 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="OlWM4BsA" Received: by mail-yw1-f171.google.com with SMTP id 00721157ae682-6047a616bfeso2935257b3.3 for ; Mon, 05 Feb 2024 22:38:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1707201520; x=1707806320; 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=DaFQB1S2OEFC+ncdlZUFtvATDjuRd6DEfxpNjkX8SCI=; b=OlWM4BsAN1Lqzom7WqBoBQ682QK6j9XFbc+Lm16qRPBOXh1KYcMGfZdflPQYxWr6hE y+4yat/ymR/J65xQ0303GzBtvVVImp0pxmJ30W85JJ1rtjxvY4Eo5N2WeBngaxFvwi52 UTg+ThXFHsDnS3aA28+fyLXQoQH6L10yKrMV3vMAs1QquGgNkVKSRwkZb+OP2gsuv7BX zyWZ7X1Em3BubPV0IhJ69UJ49DGxoEuqpsKfuMn4Dzsb7kD7ESoRyfRsBMOlzCDHVW+Q YiMeN+1Qjq9gr9DqCHjTQbp5rmW0w4N8sy3t3sIN7dpimmVmy/D3E19kN5ijDe2F7AjH nOGA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707201520; x=1707806320; 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=DaFQB1S2OEFC+ncdlZUFtvATDjuRd6DEfxpNjkX8SCI=; b=weVTwOhn6fymtLmuU4w0f0azvgMjbkIEM4oQ0B7jeQZyPKzWYyfh2YOq9w/zuwhxbB kLeWVUZpdxWdzChTye2FWv08MnSRM+A9mkhf43a5nTV8RGWdR7fuvuNIKgZuwxeWvkXd 9gwAZ5BRPzNGOx4rCsN/tTVIMiMzWXqrZ9LNVW9uYoEiZAca90vEpWszuK70lNADnupP d6cZd1rbvhgMKV2bN2oY/HRLIR0Gobd+aS1sskRjW4o+V4oCShP0cDFonx3Iw2aDgS1J g3JBukTyGV2U55HeEIAwK3bVFS7nJfF3YP7fRffvKDE4g3RBRDSVZmgPymPc9yQ05GOO tDnA== X-Gm-Message-State: AOJu0Yz5xgJcBRO5VkRPJQEUTE9HlB5E4cHC5uJCb95PXr5gFEh9Dh2K yTJngDh+xxV/m4VVGP3X3YjjqV3JIPxMJvkwmVT/D8h8SF5Q0ctNW2vrcH2WAqA= X-Google-Smtp-Source: AGHT+IGNQrdKf5nxxEn+Bc2qVNgwX8jmQPOK20ISlMMYzsFh64SE6q1dsfUwjNgE1rT1OpXHwrJk3w== X-Received: by 2002:a0d:e444:0:b0:604:5a7b:19cf with SMTP id n65-20020a0de444000000b006045a7b19cfmr780015ywe.38.1707201520077; Mon, 05 Feb 2024 22:38:40 -0800 (PST) X-Forwarded-Encrypted: i=0; AJvYcCU4I2+3x5CMtrTLenM0NiQwzv67XWYyfmN0Md0g6dr7eN5AmQhfCtXoNCCgBga8qQlRPm1IykpsafxwTCCnBQaDnjM4im395GG+kd8eHMFAtgnGgFgwGIs9CpjBS8YT9jHrurvkYLdsHF+EmPUQqANXEsCU0ce/CgYWc2co0QQfGp+u/t72pcA/9IWhLcNlbZPWtmTuFNBzpblJOiWpgJmNaH3PotQRYTBTcW2VjnciYMvyXd2/SbCANzzmnX6dSb2/veCc26PvvQdJ1dvm0qSu+s+7NHNSLCGZcWcveVr901U= Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:3a27:6d1a:7c79:c81e]) by smtp.gmail.com with ESMTPSA id ez9-20020a05690c308900b005ffb91a94e6sm64277ywb.59.2024.02.05.22.38.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 Feb 2024 22:38:39 -0800 (PST) From: thinker.li@gmail.com To: bpf@vger.kernel.org, ast@kernel.org, martin.lau@linux.dev, song@kernel.org, kernel-team@meta.com, andrii@kernel.org, davemarchevsky@meta.com, dvernet@meta.com Cc: sinquersw@gmail.com, kuifeng@meta.com, Kui-Feng Lee Subject: [PATCH bpf-next v5 3/3] selftests/bpf: Test PTR_MAYBE_NULL arguments of struct_ops operators. Date: Mon, 5 Feb 2024 22:38:33 -0800 Message-Id: <20240206063833.2520479-4-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240206063833.2520479-1-thinker.li@gmail.com> References: <20240206063833.2520479-1-thinker.li@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net From: Kui-Feng Lee Test if the verifier verifies nullable pointer arguments correctly for BPF struct_ops programs. "test_maybe_null" in struct bpf_testmod_ops is the operator defined for the test cases here. It has several pointer arguments to various types. These pointers are majorly classified to 3 categories; pointers to struct types, pointers to scalar types, and pointers to array types. They are handled sightly differently. A BPF program should check a pointer for NULL beforehand to access the value pointed by the nullable pointer arguments, or the verifier should reject the programs. The test here includes two parts; the programs checking pointers properly and the programs not checking pointers beforehand. The test checks if the verifier accepts the programs checking properly and rejects the programs not checking at all. Signed-off-by: Kui-Feng Lee --- .../selftests/bpf/bpf_testmod/bpf_testmod.c | 12 ++++- .../selftests/bpf/bpf_testmod/bpf_testmod.h | 7 +++ .../prog_tests/test_struct_ops_maybe_null.c | 47 +++++++++++++++++++ .../bpf/progs/struct_ops_maybe_null.c | 31 ++++++++++++ .../bpf/progs/struct_ops_maybe_null_fail.c | 25 ++++++++++ 5 files changed, 121 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/test_struct_ops_maybe_null.c create mode 100644 tools/testing/selftests/bpf/progs/struct_ops_maybe_null.c create mode 100644 tools/testing/selftests/bpf/progs/struct_ops_maybe_null_fail.c diff --git a/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c b/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c index a06daebc75c9..891a2b5f422c 100644 --- a/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c +++ b/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c @@ -555,7 +555,10 @@ static int bpf_dummy_reg(void *kdata) { struct bpf_testmod_ops *ops = kdata; - ops->test_2(4, 3); + if (ops->test_maybe_null) + ops->test_maybe_null(0, NULL); + else + ops->test_2(4, 3); return 0; } @@ -573,9 +576,16 @@ static void bpf_testmod_test_2(int a, int b) { } +static int bpf_testmod_ops__test_maybe_null(int dummy, + struct task_struct *task__nullable) +{ + return 0; +} + static struct bpf_testmod_ops __bpf_testmod_ops = { .test_1 = bpf_testmod_test_1, .test_2 = bpf_testmod_test_2, + .test_maybe_null = bpf_testmod_ops__test_maybe_null, }; struct bpf_struct_ops bpf_bpf_testmod_ops = { diff --git a/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.h b/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.h index 537beca42896..c51580c9119d 100644 --- a/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.h +++ b/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.h @@ -5,6 +5,8 @@ #include +struct task_struct; + struct bpf_testmod_test_read_ctx { char *buf; loff_t off; @@ -28,9 +30,14 @@ struct bpf_iter_testmod_seq { int cnt; }; +typedef u32 (*ar_t)[2]; +typedef u32 (*ar2_t)[]; + struct bpf_testmod_ops { int (*test_1)(void); void (*test_2)(int a, int b); + /* Used to test nullable arguments. */ + int (*test_maybe_null)(int dummy, struct task_struct *task); }; #endif /* _BPF_TESTMOD_H */ diff --git a/tools/testing/selftests/bpf/prog_tests/test_struct_ops_maybe_null.c b/tools/testing/selftests/bpf/prog_tests/test_struct_ops_maybe_null.c new file mode 100644 index 000000000000..1c057c62d893 --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/test_struct_ops_maybe_null.c @@ -0,0 +1,47 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2024 Meta Platforms, Inc. and affiliates. */ +#include +#include + +#include "struct_ops_maybe_null.skel.h" +#include "struct_ops_maybe_null_fail.skel.h" + +/* Test that the verifier accepts a program that access a nullable pointer + * with a proper check. + */ +static void maybe_null(void) +{ + struct struct_ops_maybe_null *skel; + + skel = struct_ops_maybe_null__open_and_load(); + if (!ASSERT_OK_PTR(skel, "struct_ops_module_open_and_load")) + return; + + struct_ops_maybe_null__destroy(skel); +} + +/* Test that the verifier rejects a program that access a nullable pointer + * without a check beforehand. + */ +static void maybe_null_fail(void) +{ + struct struct_ops_maybe_null_fail *skel; + + skel = struct_ops_maybe_null_fail__open_and_load(); + if (ASSERT_ERR_PTR(skel, "struct_ops_module_fail__open_and_load")) + return; + + struct_ops_maybe_null_fail__destroy(skel); +} + +void test_struct_ops_maybe_null(void) +{ + /* The verifier verifies the programs at load time, so testing both + * programs in the same compile-unit is complicated. We run them in + * separate objects to simplify the testing. + */ + if (test__start_subtest("maybe_null")) + maybe_null(); + if (test__start_subtest("maybe_null_fail")) + maybe_null_fail(); +} diff --git a/tools/testing/selftests/bpf/progs/struct_ops_maybe_null.c b/tools/testing/selftests/bpf/progs/struct_ops_maybe_null.c new file mode 100644 index 000000000000..c5769c742900 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/struct_ops_maybe_null.c @@ -0,0 +1,31 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2024 Meta Platforms, Inc. and affiliates. */ +#include +#include +#include +#include "../bpf_testmod/bpf_testmod.h" + +char _license[] SEC("license") = "GPL"; + +u64 tgid = 0; + +/* This is a test BPF program that uses struct_ops to access an argument + * that may be NULL. This is a test for the verifier to ensure that it can + * rip PTR_MAYBE_NULL correctly. There are tree pointers; task, scalar, and + * ar. They are used to test the cases of PTR_TO_BTF_ID, PTR_TO_BUF, and array. + */ +SEC("struct_ops/test_maybe_null") +int BPF_PROG(test_maybe_null, int dummy, + struct task_struct *task) +{ + if (task) + tgid = task->tgid; + + return 0; +} + +SEC(".struct_ops.link") +struct bpf_testmod_ops testmod_1 = { + .test_maybe_null = (void *)test_maybe_null, +}; + diff --git a/tools/testing/selftests/bpf/progs/struct_ops_maybe_null_fail.c b/tools/testing/selftests/bpf/progs/struct_ops_maybe_null_fail.c new file mode 100644 index 000000000000..566be47fb40b --- /dev/null +++ b/tools/testing/selftests/bpf/progs/struct_ops_maybe_null_fail.c @@ -0,0 +1,25 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2024 Meta Platforms, Inc. and affiliates. */ +#include +#include +#include +#include "../bpf_testmod/bpf_testmod.h" + +char _license[] SEC("license") = "GPL"; + +int tgid = 0; + +SEC("struct_ops/test_maybe_null_struct_ptr") +int BPF_PROG(test_maybe_null_struct_ptr, int dummy, + struct task_struct *task) +{ + tgid = task->tgid; + + return 0; +} + +SEC(".struct_ops.link") +struct bpf_testmod_ops testmod_struct_ptr = { + .test_maybe_null = (void *)test_maybe_null_struct_ptr, +}; +