Received: by 2002:ac0:bc90:0:0:0:0:0 with SMTP id a16csp3759629img; Mon, 25 Mar 2019 17:45:35 -0700 (PDT) X-Google-Smtp-Source: APXvYqwSHMQapCW9CQZWgh2iT2bZuc/oQNWVInv3HQHA3cDcOzk0X1yF9quUkZXMsNC5epcLvVJb X-Received: by 2002:a17:902:e091:: with SMTP id cb17mr5077338plb.222.1553561135778; Mon, 25 Mar 2019 17:45:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1553561135; cv=none; d=google.com; s=arc-20160816; b=FIHj8vteaMuhvYDYrUXomVyjklmNQh1YXhyGT6QkIMBQStoBUBjsHSVc6opaTiIK7v fP5sB1Qhqmg7FIrteVJ5TNxrGpw59i/LNd+Ul67GdLbLQ5GnA/h6loEoVwM7SDj3W1PV WLu5Px8DIWM3niE35UVmBmla1vXLPemrZXp3ikFJVceOQEhE42+/ReC1Om1suSYb9pTX 7f+IcnxzuyUnN1t6LttjekK3YZrn5fjVfgKr+Jqtd2Z2AZ3s9tA44CndOLGG6seu14OM xVHJbpk+MbISCBpgT+hRx3C7LGx8AB6zQdlfCMBHZIarjy2/+E+YQZTKZiqI8YIJRflq lgSQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:message-id:date:subject :smtp-origin-cluster:cc:to:smtp-origin-hostname:from :smtp-origin-hostprefix:dkim-signature; bh=1xljpDs4Esn3bwjsb7POMN+Fwk3bdiYCEBlhGBz12lY=; b=J2G6ZcEjEgByyr/1Sr8d+vc/vGy8M/ctOllvOzZKEg1hEfcdPhs/MgNVUyBECkoUyX WA2BtYktRK13vvPuuFUwQkmlSFUG8M3DZx+TCHfXtLRvp3bshpe94rkjA2Gb+2TGKR5X ZumsgAss2KiiFEUdCh30X0ae6D4Y/n86TzMkdRkogQCcN2g2ag9VrHryGkwk9RDcAwpc S7o4GchWBsGEczlyJjkqZCYIudvV+bN+ort5tw3cyyPHcEpO9ZqnJYBnCVvYiLOiJytt AA32DqF8g4rqQg8ZUKDgFhxzWT9z5RUO1Ojj+G2LhgmtyfT1NrpW5OxT9ztyVsTSEHrb Hp9w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@fb.com header.s=facebook header.b=JLNccMwf; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=fb.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id o14si14516831pgv.310.2019.03.25.17.45.20; Mon, 25 Mar 2019 17:45:35 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@fb.com header.s=facebook header.b=JLNccMwf; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=fb.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730556AbfCZAoX (ORCPT + 99 others); Mon, 25 Mar 2019 20:44:23 -0400 Received: from mx0a-00082601.pphosted.com ([67.231.145.42]:53868 "EHLO mx0a-00082601.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730515AbfCZAoW (ORCPT ); Mon, 25 Mar 2019 20:44:22 -0400 Received: from pps.filterd (m0044010.ppops.net [127.0.0.1]) by mx0a-00082601.pphosted.com (8.16.0.27/8.16.0.27) with SMTP id x2Q0aDAe023561 for ; Mon, 25 Mar 2019 17:44:21 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fb.com; h=from : to : cc : subject : date : message-id : mime-version : content-type; s=facebook; bh=1xljpDs4Esn3bwjsb7POMN+Fwk3bdiYCEBlhGBz12lY=; b=JLNccMwfrDVrqRpPL4ydKJwYeLV66uk0QfcpHWs4g+eE0/kUSukH1ZMOoE8K3hrYuq6S kHyYLDx0MmlQbKItDFYD8e2axRV8inZMPuNfcwtYJcbG8eXs82FAZi0eukote/HVLJxd 0R9gCSzs13gfpDk1FuW8kjeGLTl+1eUk5zA= Received: from mail.thefacebook.com ([199.201.64.23]) by mx0a-00082601.pphosted.com with ESMTP id 2rf47k9de3-4 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-SHA384 bits=256 verify=NOT) for ; Mon, 25 Mar 2019 17:44:21 -0700 Received: from mx-out.facebook.com (2620:10d:c081:10::13) by mail.thefacebook.com (2620:10d:c081:35::129) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA) id 15.1.1713.5; Mon, 25 Mar 2019 17:44:20 -0700 Received: by dev082.prn2.facebook.com (Postfix, from userid 572249) id 686553703672; Mon, 25 Mar 2019 17:44:18 -0700 (PDT) Smtp-Origin-Hostprefix: dev From: Andrey Ignatov Smtp-Origin-Hostname: dev082.prn2.facebook.com To: CC: Andrey Ignatov , , , , , Luis Chamberlain , Kees Cook , Alexey Dobriyan , , Smtp-Origin-Cluster: prn2c23 Subject: [PATCH v2 bpf-next 00/21] bpf: Sysctl hook Date: Mon, 25 Mar 2019 17:43:26 -0700 Message-ID: X-Mailer: git-send-email 2.17.1 X-FB-Internal: Safe MIME-Version: 1.0 Content-Type: text/plain X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:,, definitions=2019-03-26_01:,, signatures=0 X-Proofpoint-Spam-Reason: safe X-FB-Internal: Safe Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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. Andrey Ignatov (21): bpf: Add base proto function for cgroup-bpf programs bpf: Sysctl hook bpf: Introduce bpf_sysctl_get_name helper bpf: Introduce bpf_sysctl_get_current_value helper bpf: Introduce bpf_sysctl_{get,set}_new_value helpers bpf: Add file_pos field to bpf_sysctl ctx bpf: Sync bpf.h to tools/ libbpf: Support sysctl hook selftests/bpf: Test sysctl section name selftests/bpf: Test BPF_CGROUP_SYSCTL selftests/bpf: Test bpf_sysctl_get_name helper selftests/bpf: Test sysctl_get_current_value helper selftests/bpf: Test bpf_sysctl_{get,set}_new_value helpers selftests/bpf: Test file_pos field in bpf_sysctl ctx bpf: Introduce ARG_PTR_TO_{INT,LONG} arg types bpf: Introduce bpf_strtol and bpf_strtoul helpers bpf: Sync bpf.h to tools/ selftests/bpf: Add sysctl and strtoX helpers to bpf_helpers.h selftests/bpf: Test ARG_PTR_TO_LONG arg type selftests/bpf: Test bpf_strtol and bpf_strtoul helpers selftests/bpf: C based test for sysctl and strtoX fs/proc/proc_sysctl.c | 25 +- include/linux/bpf-cgroup.h | 21 + include/linux/bpf.h | 4 + include/linux/bpf_types.h | 1 + include/linux/filter.h | 16 + include/uapi/linux/bpf.h | 139 +- kernel/bpf/cgroup.c | 364 +++- kernel/bpf/helpers.c | 131 ++ kernel/bpf/syscall.c | 7 + kernel/bpf/verifier.c | 30 + tools/include/uapi/linux/bpf.h | 139 +- tools/lib/bpf/libbpf.c | 3 + tools/lib/bpf/libbpf_probes.c | 1 + tools/testing/selftests/bpf/Makefile | 3 +- tools/testing/selftests/bpf/bpf_helpers.h | 19 + .../selftests/bpf/progs/test_sysctl_prog.c | 89 + .../selftests/bpf/test_section_names.c | 5 + tools/testing/selftests/bpf/test_sysctl.c | 1567 +++++++++++++++++ .../testing/selftests/bpf/verifier/int_ptr.c | 160 ++ 19 files changed, 2716 insertions(+), 8 deletions(-) create mode 100644 tools/testing/selftests/bpf/progs/test_sysctl_prog.c create mode 100644 tools/testing/selftests/bpf/test_sysctl.c create mode 100644 tools/testing/selftests/bpf/verifier/int_ptr.c -- 2.17.1