Skip to content

Commit 9d3a1b2

Browse files
jrfastabkernel-patches-bot
authored andcommitted
bpf: Add comment to document BTF type PTR_TO_BTF_ID_OR_NULL
The meaning of PTR_TO_BTF_ID_OR_NULL differs slightly from other types denoted with the *_OR_NULL type. For example the types PTR_TO_SOCKET and PTR_TO_SOCKET_OR_NULL can be used for branch analysis because the type PTR_TO_SOCKET is guaranteed to _not_ have a null value. In contrast PTR_TO_BTF_ID and BTF_TO_BTF_ID_OR_NULL have slightly different meanings. A PTR_TO_BTF_TO_ID may be a pointer to NULL value, but it is safe to read this pointer in the program context because the program context will handle any faults. The fallout is for PTR_TO_BTF_ID the verifier can assume reads are safe, but can not use the type in branch analysis. Additionally, authors need to be extra careful when passing PTR_TO_BTF_ID into helpers. In general helpers consuming type PTR_TO_BTF_ID will need to assume it may be null. Seeing the above is not obvious to readers without the back knowledge lets add a comment in the type definition. Editorial comment, as networking and tracing programs get closer and more tightly merged we may need to consider a new type that we can ensure is non-null for branch analysis and also passing into helpers. Signed-off-by: John Fastabend <[email protected]> Acked-by: Lorenz Bauer <[email protected]>
1 parent 7c584e1 commit 9d3a1b2

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

include/linux/bpf.h

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -383,8 +383,22 @@ enum bpf_reg_type {
383383
PTR_TO_TCP_SOCK_OR_NULL, /* reg points to struct tcp_sock or NULL */
384384
PTR_TO_TP_BUFFER, /* reg points to a writable raw tp's buffer */
385385
PTR_TO_XDP_SOCK, /* reg points to struct xdp_sock */
386-
PTR_TO_BTF_ID, /* reg points to kernel struct */
387-
PTR_TO_BTF_ID_OR_NULL, /* reg points to kernel struct or NULL */
386+
/* PTR_TO_BTF_ID points to a kernel struct that does not need
387+
* to be null checked by the BPF program. This does not imply the
388+
* pointer is _not_ null and in practice this can easily be a null
389+
* pointer when reading pointer chains. The assumption is program
390+
* context will handle null pointer dereference typically via fault
391+
* handling. The verifier must keep this in mind and can make no
392+
* assumptions about null or non-null when doing branch analysis.
393+
* Further, when passed into helpers the helpers can not, without
394+
* additional context, assume the value is non-null.
395+
*/
396+
PTR_TO_BTF_ID,
397+
/* PTR_TO_BTF_ID_OR_NULL points to a kernel struct that has not
398+
* been checked for null. Used primarily to inform the verifier
399+
* an explicit null check is required for this struct.
400+
*/
401+
PTR_TO_BTF_ID_OR_NULL,
388402
PTR_TO_MEM, /* reg points to valid memory region */
389403
PTR_TO_MEM_OR_NULL, /* reg points to valid memory region or NULL */
390404
PTR_TO_RDONLY_BUF, /* reg points to a readonly buffer */

0 commit comments

Comments
 (0)