Skip to content

Commit 2145f4b

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]>
1 parent 0496054 commit 2145f4b

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

0 commit comments

Comments
 (0)