From patchwork Fri Feb 2 22:05:11 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: 13543489 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-yw1-f172.google.com (mail-yw1-f172.google.com [209.85.128.172]) (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 4B037136984 for ; Fri, 2 Feb 2024 22:05:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706911523; cv=none; b=EdgFlZe+K+PDIpev35AGfULvo4IqCPA2L2azKMv8qzze0UjDUVzWvNI4nXMB1SRly75WraStxl37XvVbgojKX8j428uMN8M+mrXdMu5Yq4vHro7aiIR4oJNr/zVl/EJyebpkUa6fpDynB+c8zqWgEJId5mo/VmIETjJ/OojY5jY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706911523; c=relaxed/simple; bh=Ny24tvBhyZ3dB3ySo1Lq9f4pvkPYuB9jcIDUqmP0AgY=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=GvUyqoriZWVY46wiepx9kCd4YJYFf8rI0tKKIkQ6sfjNg/uzlxvLQNZyze+k2PE8Lo1ZnnHxwXggMVSad+r6/yiKExQPOSO5QzVf+GPPdr0DPYzozhCihX5CCBZYHPaGCNoY5dFMVgwvZECZ4AJDS3YSuNgIFHjxUQqtHoNQRFI= 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=OY/hXZNv; arc=none smtp.client-ip=209.85.128.172 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="OY/hXZNv" Received: by mail-yw1-f172.google.com with SMTP id 00721157ae682-604055e48a3so27641607b3.0 for ; Fri, 02 Feb 2024 14:05:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1706911521; x=1707516321; 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=nGT9jLe359IgV5BY3wwOAF0jhl296naPo76q2f8Vyfc=; b=OY/hXZNvLr/oEImrj941QdvMAaE94QjVM9yk3sHGFKRuF3F0HNWM5Mw6eLEQq7lfFb XrZWSNld+gvDnxHFHTPhoabmRP0rTyG/QADnlKHrVl5j6bZSh5rVHlmIpZpZxlOY9/Zz fKe1CfQIl2Qi00Z6EOSiOhS3aVMTmMHCBtZzL0Pc1qOlGALN1uY58IUmPPj6KaDtE7Kr U1ZC8YCOUB038yezo7qmIYtCE89+QwfTFv011zdTeEwHVxbjHnFt8ZY6OJPiOO2E1lkG WRkUH5GzMmNwVPca864FKhn3zuY+Paz/Is64MTddXdbsvFCxMadAHTM5RvAi4Wy9TRIv 8s0g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706911521; x=1707516321; 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=nGT9jLe359IgV5BY3wwOAF0jhl296naPo76q2f8Vyfc=; b=k74EMpCuZBZph54h7qk7/6bcUqf0J39g4/dt++yC7WXeJqAJGsQORb7BS+TVGf8fyO NJPfv9/xkhoZL6jxsofTG8hY7DZGQgURJxo7c5QJPVi+KsWxlvaLt8Rz6/Hde2DAJMlV Y6Hlqdet0DLBmkZTYMTgpOoDGFJ0QGEME43hBKCKpPMHFQsmLxH4jus6wyT2dz7TTTjB trz5OAnRPIVFitZaWLrtokCPAiQtLNrB4tleJVUAXdpKU4jR3WSS5gn3TfXSlSdvirPX WFE5JVx0q5ddlW9W9SchODk9ZPSmy/DWeMTF3obUdaz4cYT9qYtwKMfmAZj/0aig+KGw 7t2g== X-Gm-Message-State: AOJu0Yyi1xLY0v9Nm1B+ATqNSI/t0v51qjjiKp8D9PR1P5J5VL5vJS6d mZ4Ha6Gj440QwfHkD5a11UzoXoy+pR1GI8WEjy2vIRG7DBxeXm5owzDCLDY1cqE= X-Google-Smtp-Source: AGHT+IFu23aLQ75v5YDYNDO/zosmKlVcr39+NDcwvgLu1xpQeiQfHItTsSOdN9D+Kw5HWoxRfK515A== X-Received: by 2002:a81:4508:0:b0:602:b5dc:1091 with SMTP id s8-20020a814508000000b00602b5dc1091mr3424686ywa.3.1706911520591; Fri, 02 Feb 2024 14:05:20 -0800 (PST) X-Forwarded-Encrypted: i=0; AJvYcCXjDuFcUWwfdytApJX4CfQH8kMZu/A78cDTh/GcF05ms5JyrMq7BFfQ2zpH4/UzN6hqVidWh+Hj0XXOTRzPEet4ReMnjyhc1l5E0xU2ycdqW3itSjw1vdTj6VyYknYDa1W8hOLFfpvuSWNoDA+i5MWqUufYA0ac/pYp+pQ3qzE6mC8ntxxERTRazGd1PKiY5auk8sR6iexXx+ljV2ketf0xs3FpQmfH5JjmiVEOu94puC8NlH5G5+0q3OnRMAqLzFROnL6n6sq6yIjm6UnzAw999aGR7I4nGApiBhjlt3C+RH0= Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:b98b:e4f8:58e3:c2f]) by smtp.gmail.com with ESMTPSA id z70-20020a814c49000000b006042345d3e2sm630696ywa.141.2024.02.02.14.05.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 Feb 2024 14:05:20 -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: [RFC bpf-next v4 1/6] bpf: Allow PTR_TO_BTF_ID even for pointers to int. Date: Fri, 2 Feb 2024 14:05:11 -0800 Message-Id: <20240202220516.1165466-2-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240202220516.1165466-1-thinker.li@gmail.com> References: <20240202220516.1165466-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 X-Patchwork-State: RFC From: Kui-Feng Lee Pointers to int are always accepted when checking accesses of a context in btf_ctx_access(). However, we are going to support pointers to scalar types with PTR_TO_BTF_ID. Changing the order of checking prog->aux->ctx_arg_info and checking is_int_ptr() enables the extension in the following patches. Signed-off-by: Kui-Feng Lee --- kernel/bpf/btf.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c index ef380e546952..0847035bba99 100644 --- a/kernel/bpf/btf.c +++ b/kernel/bpf/btf.c @@ -6252,9 +6252,6 @@ bool btf_ctx_access(int off, int size, enum bpf_access_type type, */ return true; - if (is_int_ptr(btf, t)) - return true; - /* this is a pointer to another type */ for (i = 0; i < prog->aux->ctx_arg_info_size; i++) { const struct bpf_ctx_arg_aux *ctx_arg_info = &prog->aux->ctx_arg_info[i]; @@ -6272,6 +6269,9 @@ bool btf_ctx_access(int off, int size, enum bpf_access_type type, } } + if (is_int_ptr(btf, t)) + return true; + info->reg_type = PTR_TO_BTF_ID; if (prog_args_trusted(prog)) info->reg_type |= PTR_TRUSTED; From patchwork Fri Feb 2 22:05:12 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: 13543490 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-yb1-f170.google.com (mail-yb1-f170.google.com [209.85.219.170]) (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 641418592E for ; Fri, 2 Feb 2024 22:05:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.170 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706911525; cv=none; b=C6VE7hokkRnNRoYdK55WTLQ9yXLcsU3UELMsT54q72ZWTCWH+vcB8/dH+h5PJhPcglZNNjb6YkaO4U/8sn8GGKZie9zwH/OuLctrC6Yr8/KpgpJqKouYX4pbLbPk8ivdFpGiFzZ27dDhCeGXgyWMDhIAhO/g3n8CLt3KjT5y6Bw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706911525; c=relaxed/simple; bh=O1oCBvlxfg7NF4j4lRYnyl6XQ03nTIus2l8vfZpP97s=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=L8IxIiFop2kbfluuiSug6HSdMQMpChYyGy4Y1oJuzYKz5zENPKLywjSE57SfgeS5+5NVjLcKBtPlVJaazaHI2I7pHRATjeg1s35JmFMCT3ERgNCnC5ZWgHodAd43DnCS+MkuMHviLBpNLfBPECCusxiXzg9YyDL4c+8YgOQa4IM= 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=VtuAk6DX; arc=none smtp.client-ip=209.85.219.170 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="VtuAk6DX" Received: by mail-yb1-f170.google.com with SMTP id 3f1490d57ef6-dc6d8bd612dso2255575276.1 for ; Fri, 02 Feb 2024 14:05:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1706911522; x=1707516322; 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=6TCCXGmiVXsI12H0OqHGgdlkTz9E0m7mXP0GU0SssLQ=; b=VtuAk6DXAq+NQGyfYZHx/FHPxn5YBZGO9UrbwXMQUYqm6Zjy0fAcdKFuBMct/AKcgd rAC1uR3BiNZysbiNGFW5Au8W59O1FR9kwLDFgo3pJLmu0etGccBkNC0NdeFb4rW6OMPr Tnw8TZWsr+FbqBq2J+dpx2ZiMCblgwtme+TjZV0ZItWuzJTi2cOl4AC/HtVtQREq7r1Z hvZqVHOsLjbfizpAynn7t31h7tWz8K7YgZboxAcYz52jnBqJk4G4KIGFu3SgpFWKzlAO Q3gKFHQAODOyhvhsDZJ39KBYIg9ypWvRQywueVxpUEe0AO6TjDzDFUUinx96Q8x2q0SI Sg7A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706911522; x=1707516322; 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=6TCCXGmiVXsI12H0OqHGgdlkTz9E0m7mXP0GU0SssLQ=; b=jTEnl38dlQ+xDaON0FZl3lM5vydzdZX/XCzyPkuonIWiTNFpQZpu7PUPixPTGpoa1b dce/orF1UnoL5PikRcZzu5txFzXR18YfDHQw5zdndZ+29pEWOM8Ew9/OQZglgM0EYtY8 f005SPTJ8DzHnQ1sp/Vk3132BRA0rDBukIht7EeCHz5LQ8x54lNebdGSdxC/Nhgd2A26 9Mf8mdLAtqxSZkkwIYtTE3LuJNOXiwx9DOnUQZNZXrZOv6ORIYudg8ttpXoubLu9EtFx pX/gvE/ZC9NhzoLFcc2KYkuxdmRGqOYZEF6BEP5K1EP+Kr44hOtdsmyXNSVKPhXJ5uFb jvZw== X-Gm-Message-State: AOJu0YwFRtH/b05meuzlcvadL0+jnh4oGaIaXsBDZMf+7MO9GeaDMV5E Xk5mlX5sczXx3SvBjG3MZsXLAExl+CUmwPFzWjh0Ily1MWXolG3lBa3PPSBdbGg= X-Google-Smtp-Source: AGHT+IGP/uS0ocx0F7Hx/sxpLRpfIJ8540iNzUuTpfthusLMJgxJ2CFCsrzAAit4qwyKZ7Hw4XYkZA== X-Received: by 2002:a05:690c:d21:b0:5ff:9565:6777 with SMTP id cn33-20020a05690c0d2100b005ff95656777mr3612000ywb.37.1706911521753; Fri, 02 Feb 2024 14:05:21 -0800 (PST) X-Forwarded-Encrypted: i=0; AJvYcCWpJj2cSRUort4JilehSd1MZw2wZIO+a+bwUTnYgkvSX4cppNBy/P+300N17h4l9ZYXMCM6CUrtGd7j5UetTh9OkbTtktG4Dt+K6Xxm2YhHdEJyvn5DGd65tQ1XNG/TqcovCRo8rr/JMhSXJXmffR40HCnx+cjNdExI3xkkneN1ldhHM1VUu091TZGuJNXETct04n0+zhCH6bYjI/9gxUQnJLxHehR49FIGCT4MNq9+Fc2XWuX8hnWDjex7kiHpPzU14+UFL4qTf5/EUc9koQv7Xu900kAGCPUSAbRSO1TVWWM= Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:b98b:e4f8:58e3:c2f]) by smtp.gmail.com with ESMTPSA id z70-20020a814c49000000b006042345d3e2sm630696ywa.141.2024.02.02.14.05.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 Feb 2024 14:05:21 -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: [RFC bpf-next v4 2/6] bpf: Extend PTR_TO_BTF_ID to handle pointers to scalar and array types. Date: Fri, 2 Feb 2024 14:05:12 -0800 Message-Id: <20240202220516.1165466-3-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240202220516.1165466-1-thinker.li@gmail.com> References: <20240202220516.1165466-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 X-Patchwork-State: RFC From: Kui-Feng Lee The verifier calls btf_struct_access() to check the access for PTR_TO_BTF_ID. btf_struct_access() supported only pointer to struct types (including union). We add the support of scalar types and array types. btf_reloc_array_access() is responsible for relocating the access from the whole array to an element in the array. That means to adjust the offset relatively to the start of an element and change the type to the type of the element. With this relocation, we can check the access against the element type instead of the array type itself. After relocation, the struct types, including union types, will continue the loop of btf_struct_walk(). Other types are treated as scalar types, including pointers, and return from btf_struct_access(). Signed-off-by: Kui-Feng Lee --- kernel/bpf/btf.c | 61 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c index 0847035bba99..d3f94d04c69d 100644 --- a/kernel/bpf/btf.c +++ b/kernel/bpf/btf.c @@ -6590,6 +6590,61 @@ static int btf_struct_walk(struct bpf_verifier_log *log, const struct btf *btf, return -EINVAL; } +/* Relocate the access relatively to the beginning of an element in an + * array. + * + * The offset is adjusted relatively to the beginning of the element and the + * type is adjusted to the type of the element. + * + * Return NULL for scalar, enum, and pointer type. + * Return a btf_type pointer for struct and union. + */ +static const struct btf_type * +btf_reloc_array_access(struct bpf_verifier_log *log, const struct btf *btf, + const struct btf_type *t, int *off, int size) +{ + const struct btf_type *rt, *elem_type; + u32 rt_size, elem_id, total_nelems, rt_id, elem_size; + u32 elem_idx; + + rt = __btf_resolve_size(btf, t, &rt_size, &elem_type, &elem_id, + &total_nelems, &rt_id); + if (IS_ERR(rt)) + return rt; + if (btf_type_is_array(rt)) { + if (*off >= rt_size) { + bpf_log(log, "access out of range of type %s with offset %d and size %u\n", + __btf_name_by_offset(btf, t->name_off), *off, rt_size); + return ERR_PTR(-EACCES); + } + + /* Multi-dimensional arrays are flattened by + * __btf_resolve_size(). Check the comment in + * btf_struct_walk(). + */ + elem_size = rt_size / total_nelems; + elem_idx = *off / elem_size; + /* Relocate the offset relatively to the start of the + * element at elem_idx. + */ + *off -= elem_idx * elem_size; + rt = elem_type; + rt_size = elem_size; + } + + if (btf_type_is_struct(rt)) + return rt; + + if (*off + size > rt_size) { + bpf_log(log, "access beyond the range of type %s with offset %d and size %d\n", + __btf_name_by_offset(btf, rt->name_off), *off, size); + return ERR_PTR(-EACCES); + } + + /* The access is accepted as a scalar. */ + return NULL; +} + int btf_struct_access(struct bpf_verifier_log *log, const struct bpf_reg_state *reg, int off, int size, enum bpf_access_type atype __maybe_unused, @@ -6625,6 +6680,12 @@ int btf_struct_access(struct bpf_verifier_log *log, } t = btf_type_by_id(btf, id); + t = btf_reloc_array_access(log, btf, t, &off, size); + if (IS_ERR(t)) + return PTR_ERR(t); + if (!t) + return SCALAR_VALUE; + do { err = btf_struct_walk(log, btf, t, off, size, &id, &tmp_flag, field_name); From patchwork Fri Feb 2 22:05:13 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: 13543491 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-yb1-f178.google.com (mail-yb1-f178.google.com [209.85.219.178]) (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 8E2EC85930 for ; Fri, 2 Feb 2024 22:05:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706911526; cv=none; b=jgaWlZhrLczQPRo0V538GnK5Xcq8tUQmi/NT3X7+WGSr8skkhdyO53n25MBIQ/2YE6ZDr9fp+MMxd3Zjcvm8ggHiRqAWGUC66SC7bwLXMTi+zzdjgxWgyyQkN6DxhOebFw7Vla2OYvIt2Ju2gSTfgQtZ7nfhaY5OF2BAlL45wzU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706911526; c=relaxed/simple; bh=Fyrmq2ifvYeFH/iZKNZAZQpf3RUi27W7kBT61PEABpE=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=I5RxIvoD3ORRS6QaeUtJ9e8bNBRC3mIKeI6Waa8MM/3tugTEKxnvQTNyjoT+n7LoNcn0ykevng9QFBuoxO9YCqQ9pXcAA1RZUJumc8c5L01sWOuAfvnXinhAv0/cDSQUznhqGaVLcTbLi2U6GQ1B35R/8NPu0Grb84RFmAyKhEY= 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=b7rpDH60; arc=none smtp.client-ip=209.85.219.178 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="b7rpDH60" Received: by mail-yb1-f178.google.com with SMTP id 3f1490d57ef6-d9b9adaf291so2305507276.1 for ; Fri, 02 Feb 2024 14:05:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1706911523; x=1707516323; 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=bc9EbQP4KhvlO8qubUZVLEubty2koZN9aeQrs7M1L1g=; b=b7rpDH60Qam8awkPlrrIith1I6Jazf7jBHFhst2eS4Sa12TcbkL8EsmAO++AtqQXvN Gc6OR17OZiPU6pQm5Nzdvlt+UndVqg4bgoQn2ULH+sMw2g2M67g3jSygGpbB7zLv6d7u Iy7VBYlkjqbS74fQiAsf3Ou5pw7+tGRpgiVwE8ZlFznAVKSNxkRQdEVA8UmSyvLm3Pa+ QfAE7Rpj/raecX59RDcwhN6BwpGxm2V22uCjB2rpWqM3J/hMg5BlxYzcgpxtemEz1i8e 5Fdyq2/0Y4MnoehgTBD7I30g8jyoVWq2VyJDFMycVZZCszKnJCe2WRGSQ7lEIOMsyw2s rLcQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706911523; x=1707516323; 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=bc9EbQP4KhvlO8qubUZVLEubty2koZN9aeQrs7M1L1g=; b=mRwi9ujM/BKAfk6mz6bwlD9Gv/YMlqvgqAL+zcZCV9K+KdbpH9w1Ipz6g3c0DG1GPk K6CLIQBdX2aktGqbg/bOCpAbemaiveGvp2qBtg4XyVMxkiQwk/JkT/Qw9ZNPqJxYh0XS KbKzQ2/o80HRiBvsWvHrJtOA487vtkXiemyubwuGxzLS4+VmeksX9yJcVBfr0NUNPjFR orhSY+0dV8BX6VEbMOjXPENv0P2h9zp90HdbnQrVmkFcKngQlsX2t6n3I14UX/TztU/u CIAz8NI5rRd9B6IA/6ZvhU7VB/sGWgA6Ds87hj7XTtwzpLQ1kyvzfNxKZPjWrnlWqsjk E0WQ== X-Gm-Message-State: AOJu0Yz9TADlhk4Sc8IKjyVRQZFTFEqoG9kHPkejI2m+Z2qag7ZxLsX5 3FjiOkdHGbP3P9jdLQdF3k2hlrUr4vBT1LOZ84ywGhDwqg4xzNphR3ZuVcpqPEo= X-Google-Smtp-Source: AGHT+IHvOAiiW3uhYSWy2OCcWSfbIKPRIyVvvlGYjgtmjEJcqiNy4+Y4YCr4B+VjHbMs3r5v+0kQnw== X-Received: by 2002:a81:b613:0:b0:604:ea3:6525 with SMTP id u19-20020a81b613000000b006040ea36525mr7367747ywh.0.1706911523003; Fri, 02 Feb 2024 14:05:23 -0800 (PST) X-Forwarded-Encrypted: i=0; AJvYcCXnMJ/+hRElNUhzsn3PjTh/keDZh9YyuBlxQi+WnaodKsJUomP4kVXXHqJyWt3IVqPKMcNK/poKgRN5Eu6bt0R4avdTlep4w/t0ngEpevYQN+g/FhOJJJpYsD7isWUcU40JRQ8wJ0lZD/aXLhWsuBj8BlRKpc4+oR6r/DLRLweopi6BzdidcnvlTlzH+hO0aHWI7NQ8shEcRxGvKnFEA21AQNAk5AVsXqxO84sZ5Vg03rU33c1ASaKnV9Zk4Qb+LsSsf5VaqQfwFDhHjkDiWCf8apRccR95ikg1la6qFdMFlPc= Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:b98b:e4f8:58e3:c2f]) by smtp.gmail.com with ESMTPSA id z70-20020a814c49000000b006042345d3e2sm630696ywa.141.2024.02.02.14.05.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 Feb 2024 14:05:22 -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: [RFC bpf-next v4 3/6] bpf: Remove an unnecessary check. Date: Fri, 2 Feb 2024 14:05:13 -0800 Message-Id: <20240202220516.1165466-4-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240202220516.1165466-1-thinker.li@gmail.com> References: <20240202220516.1165466-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 X-Patchwork-State: RFC From: Kui-Feng Lee The "i" here is always equal to "btf_type_vlen(t)" since the "for_each_member()" loop never breaks. Signed-off-by: Kui-Feng Lee --- kernel/bpf/bpf_struct_ops.c | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/kernel/bpf/bpf_struct_ops.c b/kernel/bpf/bpf_struct_ops.c index 0decd862dfe0..f98f580de77a 100644 --- a/kernel/bpf/bpf_struct_ops.c +++ b/kernel/bpf/bpf_struct_ops.c @@ -189,20 +189,17 @@ int bpf_struct_ops_desc_init(struct bpf_struct_ops_desc *st_ops_desc, } } - if (i == btf_type_vlen(t)) { - if (st_ops->init(btf)) { - pr_warn("Error in init bpf_struct_ops %s\n", - st_ops->name); - return -EINVAL; - } else { - st_ops_desc->type_id = type_id; - st_ops_desc->type = t; - st_ops_desc->value_id = value_id; - st_ops_desc->value_type = btf_type_by_id(btf, - value_id); - } + if (st_ops->init(btf)) { + pr_warn("Error in init bpf_struct_ops %s\n", + st_ops->name); + return -EINVAL; } + st_ops_desc->type_id = type_id; + st_ops_desc->type = t; + st_ops_desc->value_id = value_id; + st_ops_desc->value_type = btf_type_by_id(btf, value_id); + return 0; } From patchwork Fri Feb 2 22:05:14 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: 13543492 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-ot1-f47.google.com (mail-ot1-f47.google.com [209.85.210.47]) (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 83AEE12F38D for ; Fri, 2 Feb 2024 22:05:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.47 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706911527; cv=none; b=GaDn6gm6OCNFHrMuQDAfnB118pdMRvdxo6Amz9V4zUc7z3wyzLpegqRmnnBW/BXlFdIWkN4+KTrjxzC3W0DdQfwfY9aOTpfr1p0e1mz36qRUk6Wwr4czBXIlajvPIxT4q+4lJ3SfFHlMZAn5Hi+mm/thl/2yVcWMXc7TWvAITkg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706911527; c=relaxed/simple; bh=UoSvv2aGkNFX5PGmKIuAZzsWgScztNCtv54+o/yoBA4=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=NjJscnEfkk43+lH+LACEbbYOduwd3BWPHD7FtLyLuodf8gWisxlW+nCupj5EKlc/GHaUxvlRm3oTwMJ4Ab65/3hl0z+HYoEn8FyfCNYFji3gnJa66BXo3H8mNNWWwl9J+KbEBGIAgee1IG3qi/ACywQgWQkAepC0DNbteIGdo+s= 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=AKpwe5qZ; arc=none smtp.client-ip=209.85.210.47 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="AKpwe5qZ" Received: by mail-ot1-f47.google.com with SMTP id 46e09a7af769-6e114e247a2so1300269a34.0 for ; Fri, 02 Feb 2024 14:05:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1706911524; x=1707516324; 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=LN+COAn3MMdA18FMRuBoVqtzaYV8O7FmS+fJ8fCiMLo=; b=AKpwe5qZeltSSsSJqqdctgJH3XLNgVCuRHFwKwfjYvpCblv/bQe5FD5K38fDcwPjcz Vbj9pS0YD3kHCxTURlFdTIR6qHJyVayNr9RL2Jxz7I5YA29cEmL03dwaF1CU4A0leMv1 j8jiU5IfybWZzQDucvFFw4YL4fyGz9mosZkXtE6tdKTwONKL5RvxrtLmuL5+m4YLv1UL lqjnxA43vp+r5NxEP5QBV1gPRO4KeRwOTtgyqZ4qlI6pcYSbfdIACYt8j58J7Gnq9CuE qsExf5ZHeEwPXgseGZdBx3TDjgDV0uCxKuLQKVDEqUog2hzwTdFJ+Ity6LVjwQV71ZaY tsgg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706911524; x=1707516324; 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=LN+COAn3MMdA18FMRuBoVqtzaYV8O7FmS+fJ8fCiMLo=; b=B7oyD/xOZNpX9S417KYPnqQxd8VBwR/omDBysURcog+bNtlpURZvbIIQRYbgn6yaVP NPXsDyS4UPUZf2H9BjlyK2nigqeyfrSmnfS09zWlldQd535D8g1sIPnX7ThKwO4XVa0t D5CNHmrWYeD+tEUebBFIzk6/co8IGbSZbUHf2yL58Eji8R8/LfK2j8PNNq7HTaFH8Rz1 s7lFyEVOjvhMEMo3RgdQW01miPBJoSuCaiEVtpuCgzDQ4UkWJcXkBVqmWmedshqelw6R rPBn2plIxczK5Qag8vrFLhK/VgmzIvXVVCty6sejjpBKMFw1WZC5bWu6XXXgMPeFmW9l EztA== X-Gm-Message-State: AOJu0YzQTDMuJ+Z9ZrrM9TRUd0Q6rsL6ydowot3NNeHlzV5zRNkk7R6f HRDIWJpEta+DD/T4OxBF9yxrNhZYaxTV1E1EO5ZEI4IiuA1D8vQ5wOpJAgN/6Sc= X-Google-Smtp-Source: AGHT+IH/fBkzmNBzfwjJvORGbbBnXtcc9jj3hPwUQVmsLxNAeXl065nFv+YRMddrHa/q26/AhfKr/g== X-Received: by 2002:a05:6358:2606:b0:178:ba22:3cfd with SMTP id l6-20020a056358260600b00178ba223cfdmr8724169rwc.17.1706911524182; Fri, 02 Feb 2024 14:05:24 -0800 (PST) X-Forwarded-Encrypted: i=0; AJvYcCXuOXYx/fx9CM8MEWx42qALsfqESYE0A2K/D63IIYo4fLEZ0KrHcJPzQU0z6Zgu6zy7kAUa2PRlvbwRoWan+yZM04AQ4WX/tjF8yLTKSjmB3PXuF+/rwFsCu08pAfVcz7mK1y8O/BZ6URCvcTuMfhqhQls4LlkO6iw0mE4aqszDKJy0uVEaSYDQdjElxiKBHnQlgC5ITKNRMu4MjK2cHHWckocCINh3k5CO5wAVhcB0uS5Lvxu5QETBiZ31Tt68Rw8tt7bjfcMpq4yNILQts/ClSFh0hUzN1KYEl8dTR9tACqA= Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:b98b:e4f8:58e3:c2f]) by smtp.gmail.com with ESMTPSA id z70-20020a814c49000000b006042345d3e2sm630696ywa.141.2024.02.02.14.05.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 Feb 2024 14:05:23 -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: [RFC bpf-next v4 4/6] bpf: add btf pointer to struct bpf_ctx_arg_aux. Date: Fri, 2 Feb 2024 14:05:14 -0800 Message-Id: <20240202220516.1165466-5-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240202220516.1165466-1-thinker.li@gmail.com> References: <20240202220516.1165466-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 X-Patchwork-State: RFC 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 d3f94d04c69d..20d2160b3db5 100644 --- a/kernel/bpf/btf.c +++ b/kernel/bpf/btf.c @@ -6263,7 +6263,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 Fri Feb 2 22:05:15 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: 13543493 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 32C1C1332B9 for ; Fri, 2 Feb 2024 22:05:26 +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=1706911529; cv=none; b=XQJyQYZWfJ+pT6MPXhNUY4Ee9fNGu4rNuyERAyHso2ZVFZMx9Ouf9jG6O4JpXEHjE6W+6deA9xNI2PKzO30DEw8qcidFO8jiGoUuBQHjaF5ANwldB+VtBo0ty8g8C3JOcZs3UrMd9S1lr1kObsRcxFstyzBZU3booo9xGYLf3Rc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706911529; c=relaxed/simple; bh=/7Ho3TRxPV8XjZUr5rSuIMZAtg2tGDqN8LRgh17y82U=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=D/mdj2XpMM71xoJAVVOnuomdQMTZEWqqmyMcpXgfu/hMMMMtApFfpfn8KJHjpRUve+VidyHgiRSemu5fd9qHnsm1PGuLMlKOdkQFLdLZ0f1jUD6o3U8kcCyCGKGlJeDiI8sIZFC/Hr0NCxhreRZYfSWyrOfwt3xrtDLHm52PJX0= 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=lVtzq38Q; 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="lVtzq38Q" Received: by mail-yw1-f171.google.com with SMTP id 00721157ae682-604123a7499so25960097b3.3 for ; Fri, 02 Feb 2024 14:05:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1706911525; x=1707516325; 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=JeVpuuTTBYqimzLK3aKrYuDQuVv5st0y794mGeHOTzI=; b=lVtzq38QoOZFQRGUJvmHut/LNj0h+wzrHrHRXd0HXhYvQVFKXYKgNjHSjPJX2VogkA ryWNS1SwfA8HgZkdhW7emriHqbdS78slNxRnxTM1bmk2BHA5SMi4I+j3ncHtoJgPbUkK OxFM/ppYoIm1km/bjEVXuslsuyRuz4njG5Ok9FsgdEvZ+dkZ/S3PdflbFWApCrOzbw5+ exvptL4eVKbPneED+R06/PDRhomVK9GUWM79+DtNb+47vI7MHzfZdPj44V9rYPc+pxC3 yBUPzTSHx99aVfmh5Qro4yEHhMI1zm7AkXYd5ggoWTKSpPXT3ghKC1R0RNfQrFmMJMtD RsZw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706911525; x=1707516325; 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=JeVpuuTTBYqimzLK3aKrYuDQuVv5st0y794mGeHOTzI=; b=SBYvd+GbfiXeocOkmHwQ+o/twQQeArR0+b0qRnfZpLyWEK102bP8qfxzQe4JtNE4/M 6+sPsBJTXD8JZUtXSAxsWFz7hskrrUV6BWSs3VXVrEtQxefEqbdNgp0UpoGMI88Yk9eE j4gg73WiXPDs1g9A6++BdbaX9vjMJpJXxEW12NlzokH3sQxmPcojtdTkPxQdZL0l1tEy wjR+eQy84P9G2gCRfdBCKzUb3Z7IO/j27uJ/pS8GPJ1RdU3BFdY03o0ckqa//STPj/ww 62yCQ5Qv6d9DRSXLj4vTMj9t6kZnoRZscYMbuCC3JlmQKxzgPydLWVCxR/D3UisvrB07 /K+g== X-Gm-Message-State: AOJu0YxcNa96l5b7bvufAZAsDUos1p1Xw3vvotMOteRjeO3SHbTP/1vM a0qjX/a1mYAx5Szi2XjBGERaU7/KTqQPkP5gl9G7MZBxwltZ8gN9rHIqbfRNyds= X-Google-Smtp-Source: AGHT+IGee8CjxHuE+fTKMsjU8D0cCXBoWCrLT+rxy5LlJoqjMBE2arB+jybuQDw4G2PPVrxlLI6BiQ== X-Received: by 2002:a0d:ddcc:0:b0:5ff:58f1:9944 with SMTP id g195-20020a0dddcc000000b005ff58f19944mr3605767ywe.30.1706911525553; Fri, 02 Feb 2024 14:05:25 -0800 (PST) X-Forwarded-Encrypted: i=0; AJvYcCXlyIctVXlcb+ASGLD7BTS+b2xv8yQBL1Y/zoMzzRiWhSnMSxWf1GoN15quZxU3sL2JZHr9/EUBgUC24IPYktPViHmntBTQz+QZzC1zVQFzvRVYZ/YIx3od+OLzYKxfHqSF/upifSeuWaY0trB3j5wQ+7qJQ2p6AIDhGEt12AGvoWW6hvUctmk6v2kBnib3VVcfJfi4Etxaz14bSbPicO0OJU5us58fkC7Ec/7261hp2kwTTw7kWv3Fp7IMeWdRzy+YYUs3XKrXZfqRRKCjLEPTJDMLqOx/M651Ur0yMrqzXwQ= Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:b98b:e4f8:58e3:c2f]) by smtp.gmail.com with ESMTPSA id z70-20020a814c49000000b006042345d3e2sm630696ywa.141.2024.02.02.14.05.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 Feb 2024 14:05:25 -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: [RFC bpf-next v4 5/6] bpf: Create argument information for nullable arguments. Date: Fri, 2 Feb 2024 14:05:15 -0800 Message-Id: <20240202220516.1165466-6-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240202220516.1165466-1-thinker.li@gmail.com> References: <20240202220516.1165466-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 X-Patchwork-State: RFC 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 | 17 ++++ kernel/bpf/bpf_struct_ops.c | 166 ++++++++++++++++++++++++++++++++++-- kernel/bpf/btf.c | 14 +++ kernel/bpf/verifier.c | 6 ++ 4 files changed, 198 insertions(+), 5 deletions(-) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index 9a2ee9456989..63ef5cbfd213 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,10 @@ 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; + u32 member_arg_info_cnt; }; enum bpf_struct_ops_state { diff --git a/kernel/bpf/bpf_struct_ops.c b/kernel/bpf/bpf_struct_ops.c index f98f580de77a..313f6ceabcf4 100644 --- a/kernel/bpf/bpf_struct_ops.c +++ b/kernel/bpf/bpf_struct_ops.c @@ -116,17 +116,148 @@ 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 int match_nullable_suffix(const char *name) +{ + int suffix_len, len; + + if (!name) + return 0; + + suffix_len = sizeof(MAYBE_NULL_SUFFIX) - 1; + len = strlen(name); + if (len < suffix_len) + return 0; + + 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. + * + * Create and initialize a list of struct bpf_struct_ops_member_arg_info + * according to type info of the arguments of the stub functions. (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()) + */ +static int prepare_arg_info(struct btf *btf, + const char *st_ops_name, + const char *member_name, + struct bpf_struct_ops_member_arg_info *member_arg_info) +{ + const struct btf_type *stub_func_proto, *ptr_type; + struct bpf_ctx_arg_aux *arg_info, *ai_buf = NULL; + const struct btf_param *args; + u32 nargs, arg_no = 0; + const char *arg_name; + s32 arg_btf_id; + + stub_func_proto = find_stub_func_proto(btf, st_ops_name, member_name); + if (!stub_func_proto) + return 0; + + nargs = btf_type_vlen(stub_func_proto); + if (nargs > MAX_BPF_FUNC_REG_ARGS) { + pr_warn("Cannot support #%u args in stub func %s_stub_%s\n", + nargs, st_ops_name, member_name); + return -EINVAL; + } + + ai_buf = kcalloc(nargs, sizeof(*ai_buf), GFP_KERNEL); + if (!ai_buf) + return -ENOMEM; + + args = btf_params(stub_func_proto); + 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 */ + ptr_type = btf_type_resolve_ptr(btf, args[arg_no].type, + &arg_btf_id); + if (!ptr_type || + (!btf_type_is_struct(ptr_type) && + !btf_type_is_array(ptr_type) && + !btf_type_is_scalar(ptr_type) && + !btf_is_any_enum(ptr_type))) { + kfree(ai_buf); + return -EINVAL; + } + + /* Fill the information of the new argument */ + arg_info = ai_buf + member_arg_info->arg_info_cnt++; + arg_info->reg_type = + PTR_TRUSTED | PTR_MAYBE_NULL | PTR_TO_BTF_ID; + arg_info->btf_id = arg_btf_id; + arg_info->btf = btf; + arg_info->offset = arg_no * sizeof(u64); + } + + if (!member_arg_info->arg_info_cnt) + kfree(ai_buf); + else + member_arg_info->arg_info = ai_buf; + + return 0; +} + 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 +291,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,13 +303,15 @@ 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, @@ -185,14 +323,24 @@ int bpf_struct_ops_desc_init(struct bpf_struct_ops_desc *st_ops_desc, &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, + member_arg_info + i); + if (err) + goto errout; } + st_ops_desc->member_arg_info = member_arg_info; + st_ops_desc->member_arg_info_cnt = btf_type_vlen(t); + 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; @@ -201,6 +349,14 @@ int bpf_struct_ops_desc_init(struct bpf_struct_ops_desc *st_ops_desc, st_ops_desc->value_type = btf_type_by_id(btf, value_id); return 0; + +errout: + while (i > 0) + kfree(member_arg_info[--i].arg_info); + kfree(member_arg_info); + st_ops_desc->member_arg_info = NULL; + + 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 20d2160b3db5..fd192f69eb78 100644 --- a/kernel/bpf/btf.c +++ b/kernel/bpf/btf.c @@ -1699,6 +1699,20 @@ 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; + u32 cnt; + + if (tab) + for (i = 0; i < tab->cnt; i++) { + ma_info = tab->ops[i].member_arg_info; + if (ma_info) { + cnt = tab->ops[i].member_arg_info_cnt; + for (j = 0; j < cnt; j++) + kfree(ma_info[j].arg_info); + } + kfree(ma_info); + } kfree(tab); btf->struct_ops_tab = NULL; diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index cd4d780e5400..d1d1c2836bc2 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -20373,6 +20373,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 Fri Feb 2 22:05:16 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: 13543494 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-yw1-f170.google.com (mail-yw1-f170.google.com [209.85.128.170]) (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 74A2685942 for ; Fri, 2 Feb 2024 22:05:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.170 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706911530; cv=none; b=WYlw8JpfjEVmJ1CKcJBIkWibOOHuDIG5x4uFFi/GfNKcjLpqv8DHlijVYnuHS0lMM/8/LEwEVIA3+sjAb9a7C4FrD47VCan4FAiSau/VrNqk8Sz+HHNWVyNv+Aq8OwWGLsw5cM6l6U095KW/pKsel4vStOgHJK8fRXQWAE9dEvA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706911530; c=relaxed/simple; bh=n0jQDMiHhkaJHMER7xewQ6IioWI2nnoh35A5eCEQkbU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=XcpTddVQUlbTm/2g5jV6SKFyorUqG+ldt5tU0XU/wXgmrUBaW6U5li0j7o3joemEjOiiMCkK3xOnOHkkPnRifKCI08U5Oeq8vBjL0WgllidT6WQziX0eANfzi1YBtHRYLcWNet1lmaIH3zApBNAwMU2m5SFUwmPxwPKUBrUZ6Uw= 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=bHbs29K5; arc=none smtp.client-ip=209.85.128.170 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="bHbs29K5" Received: by mail-yw1-f170.google.com with SMTP id 00721157ae682-6040e7ebf33so10375917b3.0 for ; Fri, 02 Feb 2024 14:05:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1706911527; x=1707516327; 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=XscpGOHaSk/IH5vxlGZdmaBmsn3qpqFTK/IOBMmHW5o=; b=bHbs29K5rvizK6Av2dqiJW8yq+Hrgq+KYmccslWcNAzrmwxgoC4PwzVa1k2xbUhsH0 Hb/Nuj5i7vKEA1T49AH/V1+NC7806xUtHHrDZmdzIJG3UDh+jvMiEfLzzJC0GKg6mRds mOJ07yZDaMC49bjwxJx7qZXi2ZZNOxMv3hnBbsB36Zk2Zb3Iggw16KsuVWxmQw21Jt4R kUcj2RjgBfE9bGVXNqb0Y5yeqp9Olkdjl+B2mXPdqp8RTruaJz8Fp4KtOsSUmiqpDYPH sE1ho3BXnBuMSZdNfYcPEkcxno7aELpEnaMSBiOcJW4W/SD/ch4Fk59maqWTcnljm38X H0yg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706911527; x=1707516327; 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=XscpGOHaSk/IH5vxlGZdmaBmsn3qpqFTK/IOBMmHW5o=; b=iWvhEWm8fg6MzXYyE9sRFSB0+yi621oGey0h2yS4YR1gCir2JNtPmOfpjEPLheYCMu HmEiGwpB84EihazzPMTz1UVhddcSaZ85GD5Eq9h0vLqWVVDndBiAFRHsjOLCVHByot27 tDw+TGaa/5Ym9xEx/N54R7HqNFpAAVkOylABO/IOJzI/B6SJPxALrUZ0rTODT4te5a5+ 1yBKO3jv+yNzCq8GZBJsIywQ9+U74HLMQ6/6m5F7Fe2zURqIAG5JRFRk+y/CzcEV3/jK 4S7QWR410wmibAuPehO/YkcqVYz9xM4YkQLUW5UdS/DphDCQNLVVg6kGoV+hUuuh1497 /iBw== X-Gm-Message-State: AOJu0Yy/N+9vbgXwuLm+jA2S+nsU8f2FAUFp5UYDeuXFLBd2KsRaxg8F 1VDMZdJmuy/2d/bbLBj0bAgyVdU2/MRnjlyoPZDU5hJOG36w2ulE+op1y+htAvg= X-Google-Smtp-Source: AGHT+IE3kgE9KIO0kJuq+8tZOosZ+zefFP9b3EObvz4VRYyNYlsXcZymM6MBKVLlIAV4x1f5cvugXQ== X-Received: by 2002:a05:690c:360a:b0:604:31e:2b1f with SMTP id ft10-20020a05690c360a00b00604031e2b1fmr3663113ywb.12.1706911526881; Fri, 02 Feb 2024 14:05:26 -0800 (PST) X-Forwarded-Encrypted: i=0; AJvYcCVrB9k8mW/xEdm8t6LIlq4ufSOeZ/seG88ZF7W0PiajpNL6a1IqWCeYNjwdPHC79ySbz/tWIvUF+i14CMqwX+6QzZk9NwBIBJX2v3LA/BI5X1+TrIpXI6aA0+gGvaIOg7C04iCOYpYuplBPyJ+JBmHD9UYLaKSahyNXLs4DrDS/5txuaCeVPCxkZgtAdYw3u6uqhmbi4/2x2PEl8IIPsUxU+4lvLkVrea8ugx3CovhekYTVgCcpTUxCX+tBooPL5tmCKPxvqEvEf0XrnIwLveBbgmiOJEupGmtDgchVmH+5Dhg= Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:b98b:e4f8:58e3:c2f]) by smtp.gmail.com with ESMTPSA id z70-20020a814c49000000b006042345d3e2sm630696ywa.141.2024.02.02.14.05.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 Feb 2024 14:05:26 -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: [RFC bpf-next v4 6/6] selftests/bpf: Test PTR_MAYBE_NULL arguments of struct_ops operators. Date: Fri, 2 Feb 2024 14:05:16 -0800 Message-Id: <20240202220516.1165466-7-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240202220516.1165466-1-thinker.li@gmail.com> References: <20240202220516.1165466-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 X-Patchwork-State: RFC 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 | 23 ++++- .../selftests/bpf/bpf_testmod/bpf_testmod.h | 10 ++ .../prog_tests/test_struct_ops_maybe_null.c | 61 ++++++++++++ .../bpf/progs/struct_ops_maybe_null.c | 41 ++++++++ .../bpf/progs/struct_ops_maybe_null_fail.c | 98 +++++++++++++++++++ 5 files changed, 228 insertions(+), 5 deletions(-) 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 4754c662b39f..a81ca9ccf8aa 100644 --- a/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c +++ b/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c @@ -556,7 +556,10 @@ static int bpf_dummy_reg(void *kdata) struct bpf_testmod_ops *ops = kdata; int r; - r = ops->test_2(4, 3); + if (ops->test_maybe_null) + r = ops->test_maybe_null(0, NULL, NULL, NULL, NULL); + else + r = ops->test_2(4, 3); return 0; } @@ -565,19 +568,29 @@ static void bpf_dummy_unreg(void *kdata) { } -static int bpf_testmod_test_1(void) +static int bpf_testmod_ops__test_1(void) { return 0; } -static int bpf_testmod_test_2(int a, int b) +static int bpf_testmod_ops__test_2(int a, int b) +{ + return 0; +} + +static int bpf_testmod_ops__test_maybe_null(int dummy, + struct task_struct *task__nullable, + u32 *scalar__nullable, + u32 (*ar__nullable)[2], + u32 (*ar2__nullable)[]) { return 0; } static struct bpf_testmod_ops __bpf_testmod_ops = { - .test_1 = bpf_testmod_test_1, - .test_2 = bpf_testmod_test_2, + .test_1 = bpf_testmod_ops__test_1, + .test_2 = bpf_testmod_ops__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 ca5435751c79..845c04ba66c1 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,17 @@ struct bpf_iter_testmod_seq { int cnt; }; +typedef u32 (*ar_t)[2]; +typedef u32 (*ar2_t)[]; + struct bpf_testmod_ops { int (*test_1)(void); int (*test_2)(int a, int b); + /* Used to test nullable arguments. */ + int (*test_maybe_null)(int dummy, struct task_struct *task, + u32 *scalar, + ar_t ar, + ar2_t ar2); }; #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..10f5f4fee407 --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/test_struct_ops_maybe_null.c @@ -0,0 +1,61 @@ +// 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 bpf_link *link_1 = NULL, *link_2 = NULL, + *link_3 = NULL, *link_4 = NULL; + struct struct_ops_maybe_null_fail *skel; + + skel = struct_ops_maybe_null_fail__open(); + if (!ASSERT_OK_PTR(skel, "struct_ops_module_fail__open")) + return; + + link_1 = bpf_map__attach_struct_ops(skel->maps.testmod_struct_ptr); + ASSERT_ERR_PTR(link_1, "bpf_map__attach_struct_ops struct_ptr"); + + link_2 = bpf_map__attach_struct_ops(skel->maps.testmod_scalar_ptr); + ASSERT_ERR_PTR(link_2, "bpf_map__attach_struct_ops scalar_ptr"); + + link_3 = bpf_map__attach_struct_ops(skel->maps.testmod_array_ptr); + ASSERT_ERR_PTR(link_3, "bpf_map__attach_struct_ops array_ptr"); + + link_4 = bpf_map__attach_struct_ops(skel->maps.testmod_var_array_ptr); + ASSERT_ERR_PTR(link_4, "bpf_map__attach_struct_ops var_array_ptr"); + + bpf_link__destroy(link_1); + bpf_link__destroy(link_2); + bpf_link__destroy(link_3); + bpf_link__destroy(link_4); + struct_ops_maybe_null_fail__destroy(skel); +} + +void test_struct_ops_maybe_null(void) +{ + 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..9025570f574c --- /dev/null +++ b/tools/testing/selftests/bpf/progs/struct_ops_maybe_null.c @@ -0,0 +1,41 @@ +// 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; +u32 scalar_value = 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, + u32 *scalar, + u32 (*ar)[2], + u32 (*ar2)[]) +{ + if (task) + tgid = task->tgid; + + if (scalar) + scalar_value = *scalar; + + if (*ar) + scalar_value += (*ar)[0]; + + 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..cbb46fc9f02f --- /dev/null +++ b/tools/testing/selftests/bpf/progs/struct_ops_maybe_null_fail.c @@ -0,0 +1,98 @@ +// 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; +u32 scalar_value = 0; + +/* These are test BPF struct_ops programs that demonstrates the access of + * an argument that may be NULL. These test programs are used to ensure + * that the verifier correctly catches the case where a pointer is not + * checked for NULL before dereferencing it. + */ + +/* Test for pointer to a struct type. */ +SEC("struct_ops/test_maybe_null_struct_ptr") +int BPF_PROG(test_maybe_null_struct_ptr, int dummy, + struct task_struct *task, + u32 *scalar, + u32 (*ar)[2], + u32 (*ar2)[]) +{ + tgid = task->tgid; + + return 0; +} + +/* Test for pointer to a scalar type. */ +SEC("struct_ops/test_maybe_null_scalar_ptr") +int BPF_PROG(test_maybe_null_scalar_ptr, int dummy, + struct task_struct *task, + u32 *scalar, + u32 (*ar)[2], + u32 (*ar2)[]) +{ + scalar_value = *scalar; + + return 0; +} + +/* Test for pointer to an array type. */ +SEC("struct_ops/test_maybe_null_array_ptr") +int BPF_PROG(test_maybe_null_array_ptr, int dummy, + struct task_struct *task, + u32 *scalar, + u32 (*ar)[2], + u32 (*ar2)[]) +{ + scalar_value += (*ar)[0]; + scalar_value += (*ar)[1]; + + return 0; +} + +/* Test for pointer to a variable length array type. + * + * This test program is used to ensure that the verifier correctly rejects + * the case that access the content of a variable length array even + * checking the pointer for NULL beforhand since the verifier doesn't know + * the exact size of the array. + */ +SEC("struct_ops/test_maybe_null_var_array_ptr") +int BPF_PROG(test_maybe_null_var_array_ptr, int dummy, + struct task_struct *task, + u32 *scalar, + u32 (*ar)[2], + u32 (*ar2)[]) +{ + if (ar2) + scalar_value += (*ar2)[0]; + + return 0; +} + +SEC(".struct_ops.link") +struct bpf_testmod_ops testmod_struct_ptr = { + .test_maybe_null = (void *)test_maybe_null_struct_ptr, +}; + +SEC(".struct_ops.link") +struct bpf_testmod_ops testmod_scalar_ptr = { + .test_maybe_null = (void *)test_maybe_null_scalar_ptr, +}; + +SEC(".struct_ops.link") +struct bpf_testmod_ops testmod_array_ptr = { + .test_maybe_null = (void *)test_maybe_null_array_ptr, +}; + +SEC(".struct_ops.link") +struct bpf_testmod_ops testmod_var_array_ptr = { + .test_maybe_null = (void *)test_maybe_null_var_array_ptr, +}; +