Tag fmod_ret functions that return ERR_PTR()
Subsystems (4)
v2 cover Tag fmod_ret functions that return ERR_PTR()
With CONFIG_BPF_FMODRET_SCAN=y the build tags the functions that return errors, so that an fmod_ret program can attach to them. But it looks only at the functions that return an integer. This set tags the functions that return ERR_PTR() too.
The verifier part of v1 (patches 1, 2, 3 and 5) is in bpf-next now. This is the resolve_btfids part.
Patch 1 fixes a problem which is shared with the integer functions. A function may test a value with IS_ERR() and return it only if it is not an error:
return IS_ERR(prev) ? NULL : prev;
Such a function got a tag though it never returns an error pointer, and its callers check only for NULL. Now an error does not count once its register is compared with -MAX_ERRNO on the way to the return. This is a simple check and it loses a few real errors too:
"if (IS_ERR(p)) return p;" when a constant error and another value meet in p before the test. In an arm64 vmlinux it untags 17 of the new functions and 16 integer functions. In an x86-64 vmlinux it untags 24 functions. Evaluating the branch on the compare would keep them, this can be done in a follow-up, but adds ~300-400 lines of code, so I've dropped it.
Patch 2 tags the functions that return a pointer. Patches 3 and 4 skip functions which have the right tag, but whose callers break when a program skips them. Patch 3 skips the seq_file operations:seq_file calls ->stop() after ->start() failed, so ->stop() unlocks what a skipped ->start() did not lock. Patch 4 skips eight functions by name.
Patch 5 tests the series.
In an arm64 vmlinux built with clang this series tags 1263 functions which return a pointer: 14864 functions are tagged instead of 13617.
v1 -> v2:
- Drop the test of a declared function that returns a pointer (Alexei)
- Do not count errors tested with IS_ERR() (Alexei)
- Count any -EINPROGRESS which a function loads, so that a lost one does not make its -EBUSY a failure. 3 functions affected.
- Do not tag the seq_file operations (Alexei)
- Filter out a few functions by name (Alexei)
v1: https://lore.kernel.org/bpf/[email protected]/
Anton Protopopov (5):
resolve_btfids: Do not count errors translated to NULL
resolve_btfids: Tag the functions that return ERR_PTR()
resolve_btfids: Do not tag seq_file operations
resolve_btfids: Do not tag functions whose callers break on a skip selftests/bpf: Test fmod_ret on functions that return a pointer
tools/bpf/resolve_btfids/fmodret.c | 62 +++++++++++++++---- tools/bpf/resolve_btfids/fmodret_scan.c | 5 +- tools/bpf/resolve_btfids/fmodret_scan.h | 5 +- tools/bpf/resolve_btfids/text_flow.c | 12 +++- tools/bpf/resolve_btfids/text_flow.h | 3 + tools/bpf/resolve_btfids/text_insn.h | 1 + tools/bpf/resolve_btfids/text_mc.cpp | 9 ++- tools/bpf/resolve_btfids/text_mc.h | 4 ++ tools/bpf/resolve_btfids/text_mc_arm64.c | 6 ++ tools/bpf/resolve_btfids/text_mc_x86.c | 6 ++ .../selftests/bpf/prog_tests/fmodret_retval.c | 2 +- .../bpf/prog_tests/fmodret_untagged.c | 2 + tools/testing/selftests/bpf/progs/err.h | 5 ++ .../selftests/bpf/progs/fmodret_retval.c | 10 +++ .../bpf/test_kmods/bpf_testmod_retval.c | 2 +- 15 files changed, 113 insertions(+), 21 deletions(-)
Signature · 1 line
2.43.0
PATCH v2 1/5 resolve_btfids: Do not count errors translated to NULL
A function may test a value with IS_ERR() and return it only if it is not an error:
return IS_ERR(prev) ? NULL : prev;
The scanner does not evaluate conditions and counts the errors which the value may hold. For a function which returns a pointer the tag would let a program return an error which the callers do not expect. They test only for NULL and dereference the error pointer. inet_frag_find() on x86-64 is an example:
mov $-12, %rbp; ...; cmp $-0xfff, %rbp mov %rbp, %rax; cmovae %r14, %rax
Describe a compare of a register with an immediate on x86-64 and arm64. A 64-bit compare with -MAX_ERRNO or -MAX_ERRNO - 1 is the test of IS_ERR(). The errors which the register holds there do not count.
Losing an error is safe, but for -EINPROGRESS: with it -EBUSY is no failure, see means_failure(). So count -EINPROGRESS if the function loads it anywhere, whether it is left out or not.
--- tools/bpf/resolve_btfids/fmodret.c | 18 +++++++++--------- tools/bpf/resolve_btfids/fmodret_scan.c | 5 ++++- tools/bpf/resolve_btfids/fmodret_scan.h | 5 ++++- tools/bpf/resolve_btfids/text_flow.c | 12 ++++++++++-- tools/bpf/resolve_btfids/text_flow.h | 3 +++ tools/bpf/resolve_btfids/text_insn.h | 1 + tools/bpf/resolve_btfids/text_mc.cpp | 9 ++++++--- tools/bpf/resolve_btfids/text_mc.h | 4 ++++ tools/bpf/resolve_btfids/text_mc_arm64.c | 6 ++++++ tools/bpf/resolve_btfids/text_mc_x86.c | 6 ++++++ 10 files changed, 53 insertions(+), 16 deletions(-)
tools/bpf/resolve_btfids/fmodret.c +9 -9
@@ -140,9 +140,10 @@ static bool *find_bpf_funcs(const struct btf *btf)* wait, not that the function failed. Their numbers are those of the kernel, in * asm-generic/errno.h. The errno.h of the build host can have other numbers. * -* ASYNC: the function returns -EINPROGRESS too. Then its -EBUSY is no failure +* ASYNC: the function may return -EINPROGRESS too. Then its -EBUSY is no failure * either. In the crypto API it says that the request is queued on the backlog: -* crypto_wait_req() waits after both. +* crypto_wait_req() waits after both. scan_func() may leave out an -EINPROGRESS +* which the function returns, so any one which it loads counts. */ static bool means_failure (int error,bool async) {
@@ -158,20 +159,18 @@ static int cmp_error(const void *va, const void *vb)/* * Write the tag for a function which returns SIZE bytes, and in them one of the -* NR VALUES of the return value register. In 8 bytes an error is sign extended. +* NR VALUES of the return value register. For ASYNC see means_failure(). In 8 +* bytes an error is sign extended. * Return false if there is nothing to tag. * * The machine code cannot tell the -EPERM of a permission check from the -1 of a * comparator or of "no such index": a function whose only error is -1 has none. */ -static bool format_tag (char *tag,const int64_t *values,size_t nr,__u32 size) +static bool format_tag (char *tag,const int64_t *values,size_t nr,__u32 size,bool async) { -bool async = false;size_t i, n =0 ;int errors[nr]; -for (i =0 ; i < nr; i++) - async |= (int )values[i] ==-115 ;for (i =0 ; i < nr; i++) {int error = values[i];
@@ -332,6 +331,7 @@ int fmodret_tag(Elf *elf, struct btf *btf, const char *deny_path, int verbose)const struct btf_type *t =btf__type_by_id (btf, id);const struct image_func *func;const int64_t *values; +bool async;const char *name;__u32 size;
@@ -360,7 +360,7 @@ int fmodret_tag(Elf *elf, struct btf *btf, const char *deny_path, int verbose)if (denied && denied[func - img.funcs])continue ; nr_scanned++; - err =scan_func (scan, func, &values); + err =scan_func (scan, func, &values, &async);if (err == -EINVAL ) { nr_undecodable++; err =0 ;
@@ -368,7 +368,7 @@ int fmodret_tag(Elf *elf, struct btf *btf, const char *deny_path, int verbose)if (err >0 ) {char tag[sizeof (TAG ) +6 * err]; -if (!format_tag (tag, values, err, size)) +if (!format_tag (tag, values, err, size, async))continue ;if (verbose >1 )fprintf (stderr,"fmodret: %s %s\n" , name, tag +strlen (TAG ));
tools/bpf/resolve_btfids/fmodret_scan.c +4 -1
@@ -201,7 +201,7 @@ static int add_value(struct scan *s, int64_t v)return 0 ; } -int scan_func (struct scan *s,const struct image_func *f,const int64_t **values) +int scan_func (struct scan *s,const struct image_func *f,const int64_t **values,bool *async) {const struct text_flow *flow = &s->flow;int err =decode_func (s, f);
@@ -217,6 +217,7 @@ int scan_func(struct scan *s, const struct image_func *f, const int64_t **valuestext_flow_solve (&s->flow); s->nr_values =0 ; + *async = false;for (i =0 ; i < flow->nr_insns && !err; i++) {const struct text_insn *insn = &flow->insns[i];uint64_t imm[2 ] = { insn->imm, insn->imm2 };
@@ -225,6 +226,8 @@ int scan_func(struct scan *s, const struct image_func *f, const int64_t **valuesif (insn->kind !=TEXT_MOV_IMM && insn->kind !=TEXT_SELECT_IMM )continue ; +/* -EINPROGRESS of the kernel, not of the build host */ + *async |= (int32_t )imm[0 ] ==-115 || (int32_t )imm[1 ] ==-115 ;if (flow->reach[i].path ==TEXT_EFFECT_PATH )continue ; out =text_flow_out (flow, i);
tools/bpf/resolve_btfids/fmodret_scan.h +4 -1
@@ -24,8 +24,11 @@ void scan_set_proto(struct scan *scan, const struct image_func *func, unsigned i* Leave out a constant if each path from the entry to its return has an effect * on an object which FUNC gets a pointer to, see text_flow.h. * A function which ftrace cannot patch has none: no program can attach to it. +* Set ASYNC if any instruction of FUNC loads -EINPROGRESS, whether it is left +* out or not. * Return how many there are, or -EINVAL if the code cannot be decoded. */ -int scan_func (struct scan *scan,const struct image_func *func,const int64_t **values); +int scan_func (struct scan *scan,const struct image_func *func,const int64_t **values, +bool *async);#endif
tools/bpf/resolve_btfids/text_flow.c +10 -2
@@ -5,6 +5,7 @@#include <linux/bits.h> #include <linux/compiler.h> +#include <linux/err.h> #include "text_flow.h"
@@ -56,7 +57,8 @@ void text_flow_fold(struct text_flow *flow) defs =TEXT_ALL_REGS ; else if (insn->kind == TEXT_CALL) defs |= flow->arch->caller_saved; - else if (insn->kind != TEXT_OTHER && insn->kind != TEXT_COND_BRANCH) + else if (insn->kind != TEXT_OTHER && insn->kind != TEXT_CMP_IMM && + insn->kind != TEXT_COND_BRANCH) defs |= BIT(insn->dst);for (r =0 ; defs && r <TEXT_MAX_REGS ; r++)if (defs &BIT (r))
@@ -156,6 +158,12 @@ static struct text_facts transfer(const struct text_flow *flow, u32 i)if (insn->kind ==TEXT_SELECT )copy (&in, &out, insn->dst, insn->src2, cast);break ; +case TEXT_CMP_IMM : +/* IS_ERR(src) ? NULL : src. Count no error which the function tests itself */ +if (!insn->is32 && + (insn->imm == -(u64 )MAX_ERRNO || insn->imm == -(u64 )MAX_ERRNO -1 )) +kill (&in,BIT (insn->src)); +break ; }return in; }
@@ -170,7 +178,7 @@ static u32 transfer_based(const struct text_flow *flow, const struct text_insn *return based;if (insn->kind ==TEXT_CALL )return based & ~(defs | flow->arch->caller_saved); -if (insn->kind !=TEXT_OTHER && insn->kind <TEXT_BRANCH ) +if (insn->kind !=TEXT_OTHER && insn->kind !=TEXT_CMP_IMM && insn->kind <TEXT_BRANCH ) defs |=BIT (insn->dst);if (insn->kind ==TEXT_MOV_IMM || insn->load || !(insn->uses & based))return based & ~defs;
tools/bpf/resolve_btfids/text_flow.h +3 -0
@@ -14,6 +14,9 @@* join is the union. Branches are not evaluated, calls write what the ABI lets * them write, memory is not tracked. * +* Only one condition counts: a register which is compared with -MAX_ERRNO, as +* by IS_ERR(), leaves the set. The function returns no error which it tests. +* * A copy may convert the value, and only the last conversion counts. So there * are three sets. */
tools/bpf/resolve_btfids/text_insn.h +1 -0
@@ -23,6 +23,7 @@ enum text_insn_kind {TEXT_SEXT32 ,/* dst = (s32)src */ TEXT_SELECT ,/* dst = src or src2 */ TEXT_SELECT_IMM ,/* dst = src or imm */ +TEXT_CMP_IMM ,/* the flags = src compared with imm, dst is not written */ TEXT_BRANCH ,/* to imm, 0 if it is computed */ TEXT_COND_BRANCH ,/* to imm or to the next instruction */ TEXT_CALL, /* of imm, 0 if it is computed */
tools/bpf/resolve_btfids/text_mc.cpp +6 -3
@@ -250,8 +250,8 @@ bool holds(const MCInst &inst, const mc_predicate &p) /* Describe INST as PATTERN says. Return false, and leave TI alone, if it does not match */ bool match(const MCInst &inst, const Opcode &o, const mc_pattern &pattern, text_insn *ti) { - const Reg &dst = reg_of(inst, o, pattern.dst, true); u8 kind = pattern.kind, src = 0, src2 = 0; + const Reg &dst = reg_of(inst, o, pattern.dst, kind != TEXT_CMP_IMM); bool is32 = dst.bits == 32; u64 imm = pattern.value;
@@ -271,6 +271,8 @@ bool match(const MCInst &inst, const Opcode &o, const mc_pattern &pattern, text_ imm <<= inst.getOperand(pattern.shift).getImm(); if (pattern.invert) imm = ~imm; + if (pattern.negate) + imm = -imm; if (is32) imm = (u32)imm; }
@@ -288,7 +290,7 @@ bool match(const MCInst &inst, const Opcode &o, const mc_pattern &pattern, text_ } ti->kind = kind; ti->is32 = is32; - ti->dst = dst.nr; + ti->dst = kind == TEXT_CMP_IMM ? 0 : dst.nr; ti->src = src; ti->src2 = src2; ti->op = pattern.op;
@@ -370,7 +372,8 @@ int decode(const void *code, unsigned int len, u64 addr, text_insn *ti) ti->load = d.MII->get(inst.getOpcode()).mayLoad(); for (const mc_pattern *pattern : o.patterns) { if (match(inst, o, *pattern, ti)) { - ti->defs &= ~(1U << ti->dst); + if (ti->kind != TEXT_CMP_IMM) + ti->defs &= ~(1U << ti->dst); return 0; } }
tools/bpf/resolve_btfids/text_mc.h +4 -0
@@ -55,6 +55,9 @@ struct mc_predicate {* from another one in some release, while the pattern of the old one stays * for the releases before. The tests show that one of the two matches. * +* A pattern of kind TEXT_CMP_IMM writes no register: DST is the register which +* gives the width of the compare. +* * A pattern of kind TEXT_RETURN names a return to the caller. isReturn() holds * for a return from an exception as well, which does not hand the return value * register to a caller. Without such a pattern it is an opcode like any other,
@@ -71,6 +74,7 @@ struct mc_pattern {u8 shift;/* the operand which says how far the immediate is shifted left */ u8 op;/* of TEXT_ALU_IMM */ bool invert;/* the immediate */ +bool negate;/* the immediate */ bool or_zero;/* TEXT_MOV_IMM: the immediate or 0 */ bool fixed;/* the immediate is VALUE, not an operand */ s64 value;
tools/bpf/resolve_btfids/text_mc_arm64.c +6 -0
@@ -22,6 +22,9 @@ static const struct mc_pattern arm64_patterns[] = { { "CSINCWr", TEXT_SELECT, .src = 1, .src2 = 1 },/* and rd, rn, rm, asr #31: rn or 0 */ { "ANDWrs", TEXT_SELECT, .src = 1, .src2 = 1, .predicates = { IMM(3, 2 << 6 | 31) } }, +/* cmn of IS_ERR() */ + { "ADDSXri", TEXT_CMP_IMM, .dst = 1, .src = 1, .imm = 2, .shift = 3, .negate = true, + .predicates = { ZERO_REG(0) } },/* HINT: nop, bti, yield, wfe ... */ { "HINT", TEXT_OTHER }, { "AUTIASP", TEXT_OTHER }, { "PRFMui", TEXT_OTHER }, { "HINT", TEXT_OTHER, .nop = true, .predicates = { IMM(0, 0) } }, /* nop */
@@ -59,6 +62,9 @@ static const struct mc_test arm64_tests[] = { T("00 05 89 1a", TEXT_SELECT, .is32 = true, SRC(8)), /* csinc w0, w8, w9, eq */ T("00 7d 89 0a", TEXT_SELECT, .is32 = true, SRC(8)), /* and w0, w8, w9, asr #31 */ T("00 01 09 0a", TEXT_OTHER, .defs = BIT(0)), /* and w0, w8, w9 */ +T ("7f fe 3f b1" ,TEXT_CMP_IMM ,SRC (19 ),IMMS (-4095 ,-4095 )),/* cmn x19, #4095 */ +T ("1f 04 40 b1" ,TEXT_CMP_IMM ,IMMS (-4096 ,-4096 )),/* cmn x0, #1, lsl #12 */ + T("60 fe 3f b1", TEXT_OTHER, .defs = BIT(0)), /* adds x0, x19, #4095 */ T("1f 20 03 d5", TEXT_OTHER, .nop = true), /* nop */T ("5f 24 03 d5" ,TEXT_OTHER ),/* bti c */ T("bf 23 03 d5", TEXT_OTHER, .defs = BIT(30)), /* autiasp */
tools/bpf/resolve_btfids/text_mc_x86.c +6 -0
@@ -32,6 +32,9 @@ static const struct mc_pattern x86_patterns[] = { { "SUB32ri8", TEXT_ALU_IMM, .src = 1, .imm = 2, .op = TEXT_SUB }, { "SUB64ri8", TEXT_ALU_IMM, .src = 1, .imm = 2, .op = TEXT_SUB }, { "NOT32r", TEXT_ALU_IMM, .src = 1, .op = TEXT_XOR, VALUE(-1) }, +/* The compares of IS_ERR() */ + { "CMP64ri32", TEXT_CMP_IMM, .imm = 1 }, + { "CMP64i32", TEXT_CMP_IMM, .dst = I, .src = I }, { "LOCK_PREFIX", TEXT_OTHER }, { "NOOPL", TEXT_OTHER, .nop = true }, /* A rep prefix is not a part of the MCInst */
@@ -70,6 +73,9 @@ static const struct mc_test x86_tests[] = { T("83 eb 0e", TEXT_ALU_IMM, .is32 = true, .dst = RBX, SRC(RBX), .op = TEXT_SUB, IMMS(14, 14)), /* sub $14, %ebx */ T("48 83 eb 0e", TEXT_ALU_IMM, .dst = RBX, SRC(RBX), .op = TEXT_SUB, IMMS(14, 14)), /* sub $14, %rbx */ T("f7 d0", TEXT_ALU_IMM, .is32 = true, .op = TEXT_XOR, IMMS(0xffffffff, 0xffffffff)), /* not %eax */ +T ("48 81 fd 01 f0 ff ff" ,TEXT_CMP_IMM ,SRC (RBP ),IMMS (-4095 ,-4095 )),/* cmp $-4095, %rbp */ +T ("48 3d 00 f0 ff ff" ,TEXT_CMP_IMM ,IMMS (-4096 ,-4096 )),/* cmp $-4096, %rax */ +T ("3d 01 f0 ff ff" ,TEXT_OTHER ),/* cmp $-4095, %eax */ T ("f0" ,TEXT_OTHER ),/* lock */ T("0f 1f 44 00 00", TEXT_OTHER, .nop = true), /* nopl 0x0(%rax,%rax,1) */ T("f3 48 ab", TEXT_OTHER, .defs = BIT(RCX) | BIT(RDI)), /* rep stosq */
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH v2 bpf-next 1/5] resolve_btfids: Do not count errors translated to NULL
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] resolve_btfids: transfer() drops errors returned via IS_ERR() and PTR_ERR() --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] resolve_btfids: transfer() drops errors returned via IS_ERR() and PTR_ERR()
--
commit e1c82f8bb3175c33c159c6be7643d5ac46f3da7d
Author: Anton Protopopov <[email protected]>
resolve_btfids: Do not count errors translated to NULL
This commit decodes 64-bit immediate comparisons on x86-64 and arm64 so the fmodret scanner can ignore error values in a register tested by IS_ERR(), while still tracking -EINPROGRESS across the whole function.
Quoted 4 lines
tools/bpf/resolve_btfids/text_flow.c +0
-0
[ ... ]
tools/bpf/resolve_btfids/text_flow.c static struct text_facts transfer(const struct text_flow *flow, u32 i) · line 169
Quoted 13 lines
tools/bpf/resolve_btfids/text_flow.c +6
-0@@ -156,6 +158,12 @@ static struct text_facts transfer(const struct text_flow *flow, u32 i)if (insn->kind ==TEXT_SELECT )copy (&in, &out, insn->dst, insn->src2, cast);break ; +case TEXT_CMP_IMM : +/* IS_ERR(src) ? NULL : src. Count no error which the function tests itself */ +if (!insn->is32 && + (insn->imm == -(u64 )MAX_ERRNO || insn->imm == -(u64 )MAX_ERRNO -1 )) +kill (&in,BIT (insn->src)); +break ; }return in; }
[Severity: Medium]
Does killing insn->src on every 64-bit -MAX_ERRNO comparison also drop errors that the function actually returns when the error branch returns PTR_ERR(src) instead of NULL?
tools/testing/selftests/bpf/test_kmods/bpf_testmod_retval.c:bpf_testmod_retval_err_ptr() {
int *slot = bpf_testmod_retval_slot(buf, idx);
if (IS_ERR(slot))
return PTR_ERR(slot);
*slot = 1;
return 0;
}When built with Clang, the inlined ternary in bpf_testmod_retval_slot() (idx > 8 ? ERR_PTR(-EFAULT) : &buf[idx]) is lowered to a 64-bit conditional select into slot, followed by a 64-bit compare against -4096 for IS_ERR(slot). Killing insn->src at TEXT_CMP_IMM drops -EFAULT (-14) from bpf_testmod_retval_err_ptr's tag, causing the fmodret_retval selftest to fail.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Re: [PATCH v2 bpf-next 1/5] resolve_btfids: Do not count errors translated to NULL
Quoted 49 lines · On Mon, Nov 30, 2026 at 7:06 PM <[email protected]> wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium]resolve_btfids:transfer()drops errors returned viaIS_ERR()andPTR_ERR()
--commit
e1c82f8bb3175c33c159c6be7643d5ac46f3da7d
Author: Anton Protopopov <[email protected]>
resolve_btfids: Do not count errors translated to NULLThis commit decodes 64-bit immediate comparisons on x86-64 and arm64 so the fmodret scanner can ignore error values in a register tested by
IS_ERR(), while still tracking-EINPROGRESSacross the whole function.Quoted 4 lines
tools/bpf/resolve_btfids/text_flow.c +0
-0[ ... ]
tools/bpf/resolve_btfids/text_flow.c static struct text_facts transfer(const struct text_flow *flow, u32 i) · line 169
Quoted 13 lines
tools/bpf/resolve_btfids/text_flow.c +6
-0@@ -156,6 +158,12 @@ static struct text_facts transfer(const struct text_flow *flow, u32 i)if (insn->kind ==TEXT_SELECT )copy (&in, &out, insn->dst, insn->src2, cast);break ; +case TEXT_CMP_IMM : +/* IS_ERR(src) ? NULL : src. Count no error which the function tests itself */ +if (!insn->is32 && + (insn->imm == -(u64 )MAX_ERRNO || insn->imm == -(u64 )MAX_ERRNO -1 )) +kill (&in,BIT (insn->src)); +break ; }return in; }[Severity: Medium]
Does killinginsn->srcon every 64-bit -MAX_ERRNO comparison also drop errors that the function actually returns when the error branch returnsPTR_ERR(src)instead of NULL?tools/testing/selftests/bpf/test_kmods/bpf_testmod_retval.c:bpf_testmod_retval_err_ptr() { int *slot = bpf_testmod_retval_slot(buf, idx); if (IS_ERR(slot)) return PTR_ERR(slot); *slot = 1; return 0; }
Quoted 6 lines
When built with Clang, the inlined ternary in
bpf_testmod_retval_slot()(idx > 8 ?ERR_PTR(-EFAULT): &buf[idx]) is lowered to a 64-bit conditional select into slot, followed by a 64-bit compare against -4096 forIS_ERR(slot). Killinginsn->srcatTEXT_CMP_IMMdrops-EFAULT(-14) from bpf_testmod_retval_err_ptr's tag, causing thefmodret_retvalselftest to fail.
Looks like false, until robot can demo how to actually fail the selftests...
Quoted 3 lines
--
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
PATCH v2 2/5 resolve_btfids: Tag the functions that return ERR_PTR()
In reply to Anton ProtopopovA function that returns ERR_PTR(-EINVAL) fails in the same way as one that returns -EINVAL. Its callers check the pointer with IS_ERR() and get the error with PTR_ERR(). But resolve_btfids looks only at the functions that return an integer.
Look at the functions that return a pointer too. A pointer has 8 bytes and its errors are sign extended as for a function that returns long. NULL is not listed: a program that returns 0 lets the trampoline call the function.
The verifier checks all 64 bits of the return value of a program on such a function and requires the JIT for it, as for a function that returns long.
--- tools/bpf/resolve_btfids/fmodret.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-)
tools/bpf/resolve_btfids/fmodret.c +7 -4
@@ -29,8 +29,8 @@/* More than any arch has argument registers */ #define MAX_PARAMS 16 -/* Return the size of the integer which FUNC returns, or 0 */ -static __u32 int_return_size (const struct btf *btf,const struct btf_type *func) +/* Return the size of the integer or of the pointer which FUNC returns, or 0 */ +static __u32 return_size (const struct btf *btf,const struct btf_type *func) {const struct btf_type *t =btf__type_by_id (btf, func->type);int ret = t->type ?btf__resolve_type (btf, t->type) :-1 ;
@@ -38,6 +38,8 @@ static __u32 int_return_size(const struct btf *btf, const struct btf_type *func)if (ret <0 )return 0 ; t =btf__type_by_id (btf, ret); +if (btf_is_ptr (t)) +return 8 ;return btf_is_int (t) ||btf_is_any_enum (t) ? t->size :0 ; }
@@ -160,7 +162,8 @@ static int cmp_error(const void *va, const void *vb)/* * Write the tag for a function which returns SIZE bytes, and in them one of the * NR VALUES of the return value register. For ASYNC see means_failure(). In 8 -* bytes an error is sign extended. +* bytes an error is sign extended, as in ERR_PTR(). NULL is 0: no program can +* return it instead of the function. * Return false if there is nothing to tag. * * The machine code cannot tell the -EPERM of a permission check from the -1 of a
@@ -338,7 +341,7 @@ int fmodret_tag(Elf *elf, struct btf *btf, const char *deny_path, int verbose)if (!btf_is_func (t))continue ; nr_funcs++; - size =int_return_size (btf, t); + size =return_size (btf, t);if ((size !=4 && size !=8 ) || !fits_trampoline (btf, t))continue ;/* Skip functions which take or return BPF objects */
Signature · 1 line
2.43.0
PATCH v2 3/5 resolve_btfids: Do not tag seq_file operations
In reply to Anton Protopopovseq_file calls ->stop() after ->start() failed. A program which skips ->start() with an error leaves ->stop() to undo what was not done. kernfs_seq_start() returns -ENODEV with of->mutex held. After a skip kernfs_seq_stop() unlocks a mutex which nobody took. ipmr_vif_seq_start() does the same with RCU and warns.
Skip the functions which take a struct seq_file * and return void *.
--- tools/bpf/resolve_btfids/fmodret.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+)
tools/bpf/resolve_btfids/fmodret.c +26 -0
@@ -89,6 +89,30 @@ static bool has_bpf_types(const struct btf *btf, const struct btf_type *func)return false; } +/* +* seq_file calls ->stop() after ->start() failed. Its ->start() and ->next() take +* a seq_file and return void *. A skip of ->start() leaves ->stop() to unlock +* what was not locked. +*/ +static bool is_seq_op (const struct btf *btf,const struct btf_type *func) +{ +const struct btf_type *proto =btf__type_by_id (btf, func->type); +const struct btf_type *t =btf__type_by_id (btf, proto->type); +unsigned int i; + +if (!btf_is_ptr (t) || t->type) +return false; +for (i =0 ; i <btf_vlen (proto); i++) { + t =btf__type_by_id (btf,btf_params (proto)[i].type); +if (!btf_is_ptr (t)) +continue ; + t =btf__type_by_id (btf, t->type); +if (btf_is_struct (t) && !strcmp (btf__name_by_offset (btf, t->name_off),"seq_file" )) +return true; + } +return false; +} +static bool excluded_by_name (const char *name) {static const char *const names[] = {
@@ -347,6 +371,8 @@ int fmodret_tag(Elf *elf, struct btf *btf, const char *deny_path, int verbose)/* Skip functions which take or return BPF objects */ if (has_bpf_types (btf, t))continue ; +if (is_seq_op (btf, t)) +continue ;/* Skip kfuncs and helpers which escaped the previous check */ if (bpf_funcs[id])continue ;
Signature · 1 line
2.43.0
PATCH v2 4/5 resolve_btfids: Do not tag functions whose callers break on a skip
In reply to Anton ProtopopovThese functions have the right tag, but a program which skips one breaks its caller:
vfs_mkdir() error path unlocks the parent hang lock_two_directories() -EXDEV drops s_vfs_rename_mutex hang btrfs_lookup_csum() caller uses path after -ENOENT oops ipc_obtain_object_idr() do_shmat() doesn't check 2nd lookup oops idle_thread_get() cpu.c checks it only once oops pidfd_pid() the pidfs file ops don't check it oops ksm_get_folio() most callers don't check it oops __d_obtain_alias() a skip leaks the inode reference leak
Add them to excluded_by_name().
--- tools/bpf/resolve_btfids/fmodret.c | 9 +++++++++ 1 file changed, 9 insertions(+)
tools/bpf/resolve_btfids/fmodret.c +9 -0
@@ -122,6 +122,15 @@ static bool excluded_by_name(const char *name)"aarch64_insn_write" ,"aarch64_insn_patch_text_nosync" ,"tracepoint_probe_unregister" , +/* The error is right, but a skip breaks the caller */ +"vfs_mkdir" , +"lock_two_directories" , +"btrfs_lookup_csum" , +"ipc_obtain_object_idr" , +"idle_thread_get" , +"pidfd_pid" , +"ksm_get_folio" , + "__d_obtain_alias", };unsigned int i;
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH v2 bpf-next 4/5] resolve_btfids: Do not tag functions whose callers break on a skip
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] resolve_btfids: excluded_by_name() omits d_obtain_alias() and d_obtain_root() --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] resolve_btfids: excluded_by_name() omits d_obtain_alias() and d_obtain_root()
--
commit 479ac467ac3a9800596af92b0a8c1b53839d7fc5
Author: Anton Protopopov <[email protected]>
resolve_btfids: Do not tag functions whose callers break on a skip
This commit adds functions to excluded_by_name() in resolve_btfids so they are not tagged for fmod_ret when skipping them breaks their callers.
Quoted 7 lines
tools/bpf/resolve_btfids/fmodret.c +0
-0@@ -122,6 +122,15 @@ static bool excluded_by_name(const char *name)"aarch64_insn_write" , "aarch64_insn_patch_text_nosync",
tools/bpf/resolve_btfids/fmodret.c static bool excluded_by_name(const char *name) · line 133
Quoted 10 lines
tools/bpf/resolve_btfids/fmodret.c +9
-0"tracepoint_probe_unregister" , +/* The error is right, but a skip breaks the caller */ +"vfs_mkdir" , +"lock_two_directories" , +"btrfs_lookup_csum" , +"ipc_obtain_object_idr" , +"idle_thread_get" , +"pidfd_pid" , +"ksm_get_folio" , + "__d_obtain_alias",
[Severity: Medium]
Should d_obtain_alias() and d_obtain_root() also be excluded here?
__d_obtain_alias() is a static helper in fs/dcache.c whose only callers are the exported one-line wrappers d_obtain_alias() and d_obtain_root(), both of which consume the caller's struct inode reference on entry. When the compiler inlines __d_obtain_alias() into d_obtain_alias() and d_obtain_root(), its clean ERR_PTR(-ESTALE) return for a NULL inode is emitted directly in the wrappers, causing resolve_btfids to tag them and allowing an fmod_ret skip to leak the caller's inode reference.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
Re: [PATCH v2 bpf-next 4/5] resolve_btfids: Do not tag functions whose callers break on a skip
Quoted 36 lines · On Mon, Nov 30, 2026 at 7:02 PM <[email protected]> wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium]resolve_btfids:excluded_by_name()omitsd_obtain_alias()andd_obtain_root()
--commit
479ac467ac3a9800596af92b0a8c1b53839d7fc5
Author: Anton Protopopov <[email protected]>
resolve_btfids: Do not tag functions whose callers break on a skipThis commit adds functions to
excluded_by_name()inresolve_btfidsso they are not tagged forfmod_retwhen skipping them breaks their callers.Quoted 7 lines
tools/bpf/resolve_btfids/fmodret.c +0
-0@@ -122,6 +122,15 @@ static bool excluded_by_name(const char *name)"aarch64_insn_write" , "aarch64_insn_patch_text_nosync",tools/bpf/resolve_btfids/fmodret.c static bool excluded_by_name(const char *name) · line 133
Quoted 10 lines
tools/bpf/resolve_btfids/fmodret.c +9
-0"tracepoint_probe_unregister" , +/* The error is right, but a skip breaks the caller */ +"vfs_mkdir" , +"lock_two_directories" , +"btrfs_lookup_csum" , +"ipc_obtain_object_idr" , +"idle_thread_get" , +"pidfd_pid" , +"ksm_get_folio" , + "__d_obtain_alias",[Severity: Medium]
Shouldd_obtain_alias()andd_obtain_root()also be excluded here?
Quoted 7 lines
__d_obtain_alias()is a static helper infs/dcache.cwhose only callers are the exported one-line wrappersd_obtain_alias()andd_obtain_root(), both of which consume the caller's struct inode reference on entry. When the compiler inlines__d_obtain_alias()intod_obtain_alias()andd_obtain_root(), its cleanERR_PTR(-ESTALE)return for a NULL inode is emitted directly in the wrappers, causingresolve_btfidsto tag them and allowing anfmod_retskip to leak the caller's inode reference.
This doesn't look realistic that it will be inlined... I would ignore this.
Quoted 2 lines
--
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
PATCH v2 5/5 selftests/bpf: Test fmod_ret on functions that return a pointer
In reply to Anton Protopopovbpf_testmod_retval_pointer() returns ERR_PTR(-EINVAL) or NULL. Expect the tag "bpf_fmodret_retval:-22" for it and check that a program on it may return ERR_PTR(-EINVAL).
Check that vmstat_start() has no tag: it takes a seq_file, returns void * and may return ERR_PTR(-ENOMEM). It needs only CONFIG_PROC_FS, which test_progs needs anyway.
--- .../testing/selftests/bpf/prog_tests/fmodret_retval.c | 2 +- .../selftests/bpf/prog_tests/fmodret_untagged.c | 2 ++ tools/testing/selftests/bpf/progs/err.h | 5 +++++ tools/testing/selftests/bpf/progs/fmodret_retval.c | 10 ++++++++++ .../selftests/bpf/test_kmods/bpf_testmod_retval.c | 2 +- 5 files changed, 19 insertions(+), 2 deletions(-)
tools/testing/selftests/bpf/prog_tests/fmodret_retval.c +1 -1
@@ -24,7 +24,7 @@ static const struct { { "bpf_testmod_retval_no_effect", "-28,-22,-16" }, { "bpf_testmod_retval_err_ptr", "-34,-14" }, { "bpf_testmod_retval_no_branch", "-14" }, - { "bpf_testmod_retval_pointer", "" }, + { "bpf_testmod_retval_pointer", "-22" }, { "bpf_testmod_retval_argument", "" }, { "bpf_testmod_retval_notrace", "" }, { "bpf_testmod_retval_variadic", "" },
tools/testing/selftests/bpf/prog_tests/fmodret_untagged.c +2 -0
@@ -11,6 +11,8 @@ static const char * const funcs[] = {"sock_map_delete_elem" ,/* A helper */ "sk_skb_adjust_room" , +/* Takes a seq_file and returns void * */ +"vmstat_start" ,/* By name */ "copy_from_kernel_nofault",/* By prefix */
tools/testing/selftests/bpf/progs/err.h +5 -0
@@ -25,4 +25,9 @@ static inline long PTR_ERR(const void *ptr) return (long) ptr; } +static inline void *ERR_PTR (long error) +{ +return (void *) error; +} +#endif /* __ERR_H__ */
tools/testing/selftests/bpf/progs/fmodret_retval.c +10 -0
@@ -10,6 +10,7 @@#include <bpf/bpf_tracing.h> #include "bpf_misc.h" #include "bpf_experimental.h" +#include "err.h" char _license[] SEC("license") = "GPL";
@@ -108,6 +109,15 @@ __naked void zero_extended(void) ::: __clobber_all); } +/* NULL calls the function */ +SEC("fmod_ret/bpf_testmod_retval_pointer") +__load_if_JITed() +__success +void *BPF_PROG (err_ptr ,int value) +{ +return value ?ERR_PTR (-EINVAL ) :NULL ; +} +/* A privileged program may return a pointer, but not here */ SEC("fmod_ret/bpf_testmod_retval_called") __failure __msg("R0 is not a known value (ctx)")
tools/testing/selftests/bpf/test_kmods/bpf_testmod_retval.c +1 -1
@@ -131,7 +131,7 @@ static __used noinline int bpf_testmod_retval_no_branch(unsigned long left)return left ? -EFAULT :0 ; } -/* Not an integer, and an error which is not returned */ +/* ERR_PTR() is an error, NULL is not */ static __used noinline void *bpf_testmod_retval_pointer(int value) {return value ?ERR_PTR (-EINVAL ) :NULL ;
Signature · 1 line
2.43.0
This series was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:
Hello:
This series was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:
Quoted 9 lines · On Mon, 30 Nov 2026 18:04:17 +0000 you wrote:
With
CONFIG_BPF_FMODRET_SCAN=ythe build tags the functions that return errors, so that anfmod_retprogram can attach to them. But it looks only at the functions that return an integer. This set tags the functions that returnERR_PTR()too.The verifier part of v1 (patches 1, 2, 3 and 5) is in bpf-next now. This is the
resolve_btfidspart.[...]
Here is the summary with links:
- [v2,bpf-next,1/5] resolve_btfids: Do not count errors translated to NULL
https://git.kernel.org/bpf/bpf-next/c/91f67df92a9f - [v2,bpf-next,2/5] resolve_btfids: Tag the functions that return ERR_PTR() https://git.kernel.org/bpf/bpf-next/c/d0936abacd09 - [v2,bpf-next,3/5] resolve_btfids: Do not tag seq_file operations https://git.kernel.org/bpf/bpf-next/c/97daa13a8539 - [v2,bpf-next,4/5] resolve_btfids: Do not tag functions whose callers break on a skip https://git.kernel.org/bpf/bpf-next/c/71536a9cb741 - [v2,bpf-next,5/5] selftests/bpf: Test fmod_ret on functions that return a pointer https://git.kernel.org/bpf/bpf-next/c/464d2a9f025a
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html