From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 59D13C4332F for ; Mon, 9 May 2022 23:43:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233405AbiEIXrq (ORCPT ); Mon, 9 May 2022 19:47:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44974 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235065AbiEIXrP (ORCPT ); Mon, 9 May 2022 19:47:15 -0400 Received: from mail-wr1-x42a.google.com (mail-wr1-x42a.google.com [IPv6:2a00:1450:4864:20::42a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 721BB2CDEEC for ; Mon, 9 May 2022 16:38:42 -0700 (PDT) Received: by mail-wr1-x42a.google.com with SMTP id q23so21470105wra.1 for ; Mon, 09 May 2022 16:38:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=sEOoNgA37n+oAtmDFXjXYHfK9txRG9g+pJQQojjD46Y=; b=XXu0vAfOWN76zZocMQkxmDXWgIDzGo83KaDB7NAjCJ2ILPSOZDv0fZ9UdN0P6frAwD AwXmeS7Rjlnggyn135GHPcSKeRHe2VNehubW44MTeZXWk2lkSEXJj4O3w1Z7KdWO2z2i rb4jQWCREBTTEZ5zOr+dOOdPArOAEFO8+5k3q34jtMoCvjMwbxBSb+2koRsh4rQ6PdQ1 ljjj6Vg9Fr5JqIVLgXyudWIV2JLHAqAjywTvH1eyhrm6EOVsWCzu9HRt07AwHjdRP4zh ax5hKw2OL6a3O4+GmVpAwvm1YAtsAFkgroO1E7PjyDlvSutFIBZ7AA0axajHwjcEJd3n SfBw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=sEOoNgA37n+oAtmDFXjXYHfK9txRG9g+pJQQojjD46Y=; b=J3cmq+k1ueQ1M7V6ElwFGoFHmBSU0Qng2JpliN8xDxEZC4LjNOSUuQHP/8XaXq43iQ Ifxl+Nag5/m9SQuPziHBH8+s8meiUIxgdGeHHSdfTtq2r/AGesNXDcH+8llzyjYc9f6W OljnJWvQmRTGh9NfcFBTP3SVJo+t1QJ9cU1hdKjs7rRPzkY8yJYHMy4F1Z53+Jnuuwit U9BBNeqXLbHOZdkpV5FUs6FX0r0rIVv9sd3g/covBRJ6RmvJk5UN4GGJ6wWIC7e8gmHK BlDO1lzTWue/P/QZkv3Gc/XzgFdS7QLuC4vLFZ2rLycT/fz6MgGxdzhK6Io2QFAKYN9J Y+hw== X-Gm-Message-State: AOAM532tg4GQWpL/bHir4K6EeVw+yUTSCnQLeNtjE/Y13WPw1fKEoM4C nThxoD5PNvZigozZ2jorsyhCUrNWOlVLCQi+gZb1Mw== X-Google-Smtp-Source: ABdhPJzbOltZZaR3MgHCrXXhLNCDJBGfgjqHZbfMUBDfydO6J+pREiZbXz7ZkG+gqvomBwxUyol5h4urwh1GGFmxqEQ= X-Received: by 2002:adf:dc91:0:b0:20c:cb51:4160 with SMTP id r17-20020adfdc91000000b0020ccb514160mr5366046wrj.568.1652139520845; Mon, 09 May 2022 16:38:40 -0700 (PDT) MIME-Version: 1.0 References: <20220429211540.715151-1-sdf@google.com> <20220429211540.715151-11-sdf@google.com> In-Reply-To: From: Stanislav Fomichev Date: Mon, 9 May 2022 16:38:29 -0700 Message-ID: Subject: Re: [PATCH bpf-next v6 10/10] selftests/bpf: verify lsm_cgroup struct sock access To: Andrii Nakryiko Cc: Networking , bpf , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org On Mon, May 9, 2022 at 2:54 PM Andrii Nakryiko wrote: > > On Fri, Apr 29, 2022 at 2:16 PM Stanislav Fomichev wrote: > > > > sk_priority & sk_mark are writable, the rest is readonly. > > > > Add new ldx_offset fixups to lookup the offset of struct field. > > Allow using test.kfunc regardless of prog_type. > > > > One interesting thing here is that the verifier doesn't > > really force me to add NULL checks anywhere :-/ > > > > Signed-off-by: Stanislav Fomichev > > --- > > tools/testing/selftests/bpf/test_verifier.c | 54 ++++++++++++++++++- > > .../selftests/bpf/verifier/lsm_cgroup.c | 34 ++++++++++++ > > 2 files changed, 87 insertions(+), 1 deletion(-) > > create mode 100644 tools/testing/selftests/bpf/verifier/lsm_cgroup.c > > > > [...] > > > diff --git a/tools/testing/selftests/bpf/verifier/lsm_cgroup.c b/tools/testing/selftests/bpf/verifier/lsm_cgroup.c > > new file mode 100644 > > index 000000000000..af0efe783511 > > --- /dev/null > > +++ b/tools/testing/selftests/bpf/verifier/lsm_cgroup.c > > @@ -0,0 +1,34 @@ > > +#define SK_WRITABLE_FIELD(tp, field, size, res) \ > > +{ \ > > + .descr = field, \ > > + .insns = { \ > > + /* r1 = *(u64 *)(r1 + 0) */ \ > > + BPF_LDX_MEM(BPF_DW, BPF_REG_1, BPF_REG_1, 0), \ > > + /* r1 = *(u64 *)(r1 + offsetof(struct socket, sk)) */ \ > > + BPF_LDX_MEM(BPF_DW, BPF_REG_1, BPF_REG_1, 0), \ > > + /* r2 = *(u64 *)(r1 + offsetof(struct sock, )) */ \ > > + BPF_LDX_MEM(size, BPF_REG_2, BPF_REG_1, 0), \ > > + /* *(u64 *)(r1 + offsetof(struct sock, )) = r2 */ \ > > + BPF_STX_MEM(size, BPF_REG_1, BPF_REG_2, 0), \ > > + BPF_MOV64_IMM(BPF_REG_0, 1), \ > > + BPF_EXIT_INSN(), \ > > + }, \ > > + .result = res, \ > > + .errstr = res ? "no write support to 'struct sock' at off" : "", \ > > + .prog_type = BPF_PROG_TYPE_LSM, \ > > + .expected_attach_type = BPF_LSM_CGROUP, \ > > + .kfunc = "socket_post_create", \ > > + .fixup_ldx = { \ > > + { "socket", "sk", 1 }, \ > > + { tp, field, 2 }, \ > > + { tp, field, 3 }, \ > > + }, \ > > +} > > + > > +SK_WRITABLE_FIELD("sock_common", "skc_family", BPF_H, REJECT), > > +SK_WRITABLE_FIELD("sock", "sk_sndtimeo", BPF_DW, REJECT), > > +SK_WRITABLE_FIELD("sock", "sk_priority", BPF_W, ACCEPT), > > +SK_WRITABLE_FIELD("sock", "sk_mark", BPF_W, ACCEPT), > > +SK_WRITABLE_FIELD("sock", "sk_pacing_rate", BPF_DW, REJECT), > > + > > have you tried writing it as C program and adding the test to > test_progs? Does something not work there? Seems like it should work, I don't see any issues with writing 5 programs to test each field. But test_verified still feels like a better fit? Any reason in particular you'd prefer test_progs over test_verifier?