BPF:如何将跳跃值设置为累加器中存储的值?

BPF: How to set the jump value as the value stored in the accumulator?

我正在使用 seccomp BPF,需要设置跳转语句的跳转值 (jt/jf/k)(条件 jump/jump 总是)作为存储在累加器中的值。这可能吗?我有一种预感,它不是,因为 BPF 验证器无法在加载过滤器之前检查跳转值。如果没有,是否有任何解决方法?

struct sock_filter filter = 
  {
    BPF_STMT(BPF_LD | BPF_W | BPF_ABS, offsetof(struct seccomp_data, nr)),
    BPF_STMT(BPF_JMP | BPF_JA, /* Value stored in the accumulator */),
    ...
  }

我试着寻找 here 但我想不出任何办法。我对 BPF 的了解也相当初级,而且只在 seccomp 的范围内。你能帮助我吗?谢谢你的时间。

不,BPF 不支持间接分支指令。 seccomp-bpf 中使用的 cBPF 和 eBPF 都没有。


对于 cBPF,您可以在 the documentation 中查看。您会看到指令定义为:

struct sock_filter { /* Filter block */
    __u16   code;    /* Actual filter code */
    __u8    jt;      /* Jump true */
    __u8    jf;      /* Jump false */
    __u32   k;       /* Generic multiuse field */
};

其中 jtjfk 可以解释为跳转偏移量,具体取决于所使用的特定跳转指令。在所有情况下,它们都被解释为立即值而不是寄存器数字:

   6               L                    Jump label L
   7               #k,Lt,Lf             Jump to Lt if true, otherwise jump to Lf
   8               x/%x,Lt,Lf           Jump to Lt if true, otherwise jump to Lf
   9               #k,Lt                Jump to Lt if predicate is true
  10               x/%x,Lt              Jump to Lt if predicate is true