1. 17 Apr, 2019 12 commits
  2. 16 Apr, 2019 16 commits
  3. 13 Apr, 2019 4 commits
  4. 12 Apr, 2019 8 commits
    • Andrey Ignatov's avatar
      bpf: Fix distinct pointer types warning for ARCH=i386 · 51356ac8
      Andrey Ignatov authored
      Fix a new warning reported by kbuild for make ARCH=i386:
      
         In file included from kernel/bpf/cgroup.c:11:0:
         kernel/bpf/cgroup.c: In function '__cgroup_bpf_run_filter_sysctl':
         include/linux/kernel.h:827:29: warning: comparison of distinct pointer types lacks a cast
            (!!(sizeof((typeof(x) *)1 == (typeof(y) *)1)))
                                      ^
         include/linux/kernel.h:841:4: note: in expansion of macro '__typecheck'
            (__typecheck(x, y) && __no_side_effects(x, y))
             ^~~~~~~~~~~
         include/linux/kernel.h:851:24: note: in expansion of macro '__safe_cmp'
           __builtin_choose_expr(__safe_cmp(x, y), \
                                 ^~~~~~~~~~
         include/linux/kernel.h:860:19: note: in expansion of macro '__careful_cmp'
          #define min(x, y) __careful_cmp(x, y, <)
                            ^~~~~~~~~~~~~
      >> kernel/bpf/cgroup.c:837:17: note: in expansion of macro 'min'
            ctx.new_len = min(PAGE_SIZE, *pcount);
                          ^~~
      
      Fixes: 4e63acdf ("bpf: Introduce bpf_sysctl_{get,set}_new_value helpers")
      Signed-off-by: default avatarAndrey Ignatov <rdna@fb.com>
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      51356ac8
    • Alexei Starovoitov's avatar
      Merge branch 'bpf-sysctl-hook' · a43d0508
      Alexei Starovoitov authored
      Andrey Ignatov says:
      
      ====================
      v2->v3:
      - simplify C based selftests by relying on variable offset stack access.
      
      v1->v2:
      - add fs/proc/proc_sysctl.c mainteners to Cc:.
      
      The patch set introduces new BPF hook for sysctl.
      
      It adds new program type BPF_PROG_TYPE_CGROUP_SYSCTL and attach type
      BPF_CGROUP_SYSCTL.
      
      BPF_CGROUP_SYSCTL hook is placed before calling to sysctl's proc_handler so
      that accesses (read/write) to sysctl can be controlled for specific cgroup
      and either allowed or denied, or traced.
      
      The hook has access to sysctl name, current sysctl value and (on write
      only) to new sysctl value via corresponding helpers. New sysctl value can
      be overridden by program. Both name and values (current/new) are
      represented as strings same way they're visible in /proc/sys/. It is up to
      program to parse these strings.
      
      To help with parsing the most common kind of sysctl value, vector of
      integers, two new helpers are provided: bpf_strtol and bpf_strtoul with
      semantic similar to user space strtol(3) and strtoul(3).
      
      The hook also provides bpf_sysctl context with two fields:
      * @write indicates whether sysctl is being read (= 0) or written (= 1);
      * @file_pos is sysctl file position to read from or write to, can be
        overridden.
      
      The hook allows to make better isolation for containerized applications
      that are run as root so that one container can't change a sysctl and affect
      all other containers on a host, make changes to allowed sysctl in a safer
      way and simplify sysctl tracing for cgroups.
      
      Patch 1 is preliminary refactoring.
      Patch 2 adds new program and attach types.
      Patches 3-5 implement helpers to access sysctl name and value.
      Patch 6 adds file_pos field to bpf_sysctl context.
      Patch 7 updates UAPI in tools.
      Patches 8-9 add support for the new hook to libbpf and corresponding test.
      Patches 10-14 add selftests for the new hook.
      Patch 15 adds support for new arg types to verifier: pointer to integer.
      Patch 16 adds bpf_strto{l,ul} helpers to parse integers from sysctl value.
      Patch 17 updates UAPI in tools.
      Patch 18 updates bpf_helpers.h.
      Patch 19 adds selftests for pointer to integer in verifier.
      Patches 20-21 add selftests for bpf_strto{l,ul}, including integration
                    C based test for sysctl value parsing.
      ====================
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      a43d0508
    • Andrey Ignatov's avatar
      selftests/bpf: C based test for sysctl and strtoX · 7568f4cb
      Andrey Ignatov authored
      Add C based test for a few bpf_sysctl_* helpers and bpf_strtoul.
      
      Make sure that sysctl can be identified by name and that multiple
      integers can be parsed from sysctl value with bpf_strtoul.
      
      net/ipv4/tcp_mem is chosen as a testing sysctl, it contains 3 unsigned
      longs, they all are parsed and compared (val[0] < val[1] < val[2]).
      
      Example of output:
        # ./test_sysctl
        ...
        Test case: C prog: deny all writes .. [PASS]
        Test case: C prog: deny access by name .. [PASS]
        Test case: C prog: read tcp_mem .. [PASS]
        Summary: 39 PASSED, 0 FAILED
      Signed-off-by: default avatarAndrey Ignatov <rdna@fb.com>
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      7568f4cb
    • Andrey Ignatov's avatar
      selftests/bpf: Test bpf_strtol and bpf_strtoul helpers · 8549ddc8
      Andrey Ignatov authored
      Test that bpf_strtol and  bpf_strtoul helpers can be used to convert
      provided buffer to long or unsigned long correspondingly and return both
      correct result and number of consumed bytes, or proper errno.
      
      Example of output:
        # ./test_sysctl
        ..
        Test case: bpf_strtoul one number string .. [PASS]
        Test case: bpf_strtoul multi number string .. [PASS]
        Test case: bpf_strtoul buf_len = 0, reject .. [PASS]
        Test case: bpf_strtoul supported base, ok .. [PASS]
        Test case: bpf_strtoul unsupported base, EINVAL .. [PASS]
        Test case: bpf_strtoul buf with spaces only, EINVAL .. [PASS]
        Test case: bpf_strtoul negative number, EINVAL .. [PASS]
        Test case: bpf_strtol negative number, ok .. [PASS]
        Test case: bpf_strtol hex number, ok .. [PASS]
        Test case: bpf_strtol max long .. [PASS]
        Test case: bpf_strtol overflow, ERANGE .. [PASS]
        Summary: 36 PASSED, 0 FAILED
      Signed-off-by: default avatarAndrey Ignatov <rdna@fb.com>
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      8549ddc8
    • Andrey Ignatov's avatar
      selftests/bpf: Test ARG_PTR_TO_LONG arg type · c2d5f12e
      Andrey Ignatov authored
      Test that verifier handles new argument types properly, including
      uninitialized or partially initialized value, misaligned stack access,
      etc.
      
      Example of output:
        #456/p ARG_PTR_TO_LONG uninitialized OK
        #457/p ARG_PTR_TO_LONG half-uninitialized OK
        #458/p ARG_PTR_TO_LONG misaligned OK
        #459/p ARG_PTR_TO_LONG size < sizeof(long) OK
        #460/p ARG_PTR_TO_LONG initialized OK
      Signed-off-by: default avatarAndrey Ignatov <rdna@fb.com>
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      c2d5f12e
    • Andrey Ignatov's avatar
      selftests/bpf: Add sysctl and strtoX helpers to bpf_helpers.h · 99f57973
      Andrey Ignatov authored
      Add bpf_sysctl_* and bpf_strtoX helpers to bpf_helpers.h.
      Signed-off-by: default avatarAndrey Ignatov <rdna@fb.com>
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      99f57973
    • Andrey Ignatov's avatar
      bpf: Sync bpf.h to tools/ · b457e553
      Andrey Ignatov authored
      Sync bpf_strtoX related bpf UAPI changes to tools/.
      Signed-off-by: default avatarAndrey Ignatov <rdna@fb.com>
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      b457e553
    • Andrey Ignatov's avatar
      bpf: Introduce bpf_strtol and bpf_strtoul helpers · d7a4cb9b
      Andrey Ignatov authored
      Add bpf_strtol and bpf_strtoul to convert a string to long and unsigned
      long correspondingly. It's similar to user space strtol(3) and
      strtoul(3) with a few changes to the API:
      
      * instead of NUL-terminated C string the helpers expect buffer and
        buffer length;
      
      * resulting long or unsigned long is returned in a separate
        result-argument;
      
      * return value is used to indicate success or failure, on success number
        of consumed bytes is returned that can be used to identify position to
        read next if the buffer is expected to contain multiple integers;
      
      * instead of *base* argument, *flags* is used that provides base in 5
        LSB, other bits are reserved for future use;
      
      * number of supported bases is limited.
      
      Documentation for the new helpers is provided in bpf.h UAPI.
      
      The helpers are made available to BPF_PROG_TYPE_CGROUP_SYSCTL programs to
      be able to convert string input to e.g. "ulongvec" output.
      
      E.g. "net/ipv4/tcp_mem" consists of three ulong integers. They can be
      parsed by calling to bpf_strtoul three times.
      
      Implementation notes:
      
      Implementation includes "../../lib/kstrtox.h" to reuse integer parsing
      functions. It's done exactly same way as fs/proc/base.c already does.
      
      Unfortunately existing kstrtoX function can't be used directly since
      they fail if any invalid character is present right after integer in the
      string. Existing simple_strtoX functions can't be used either since
      they're obsolete and don't handle overflow properly.
      Signed-off-by: default avatarAndrey Ignatov <rdna@fb.com>
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      d7a4cb9b