Received: by 10.223.164.202 with SMTP id h10csp1106557wrb; Thu, 9 Nov 2017 21:38:55 -0800 (PST) X-Google-Smtp-Source: ABhQp+TfngYGO+xAZRoyWu6XytYvp3heD7pijlv4KPNEioW3Iy8oYSOsrwSCt3AkxlSrl6wfGBbg X-Received: by 10.84.232.202 with SMTP id x10mr2918172plm.101.1510292335480; Thu, 09 Nov 2017 21:38:55 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1510292335; cv=none; d=google.com; s=arc-20160816; b=pt+4qf1/mMZ0HxgjP+jvFbuo5F4iL3nv9bfgULB30K/NTFrW2NWM3EYO9jN0fJWmd5 xDWEtKSax5qRhg06e8xt7lmc+IW/EbJnNrC/gilClrbfHIAQjO15xmft+V82fB5aZD7U Wv8efbNbNo2wbnNS52mDqtOvIUmmLMjaxfJEJwu7dOD/iEHrSjqu6VkTxPSEIqInlKYY RqgHLws/i/MPbVbVoXd5i5AdsynDgl+oCExmcqDLPZOZLJuxqTNqLzuiA+9fKbMiSP65 7wRiDiMli0cJtFRZK61Rn8N4OwMmtJ5MdPemnU0APN7mISAwB018+D/ofPBOdcgoL8KO 77ig== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from :dkim-signature:arc-authentication-results; bh=TX+dvm+YSNktTBbynQpVR95yE+ooI3MJyBYVej2/vtM=; b=GCF9Tarl/aQ8q7Glt3SHtp7BLvj3do50qX4PGGs4/h58wy+wwqW7+lTAxWlOP4GUXv xk+o5VtV2Rg4MdAYj7j5wwjf16QqcQQS6p3kWF6k2Pd5khpMfKxrfBZBTTQjbjCMRW3E t4jOUNBKA409JF8bwApw06+ymugICAPLCmzF00PJZE5+d9qPbrgACjmZIkoyznEeB/Pd 9NKJgRTWu7p9JY4q8sClhzY8igqqutxi9YsjMc6rQ148LCJ8g3CO33ufIArg3smvIS4+ H75JCtne9QhNUm2J48pSoHIWFceGFKsUc5GfucSNxKahmhE1dO/vmJe8Io/xUDAsXB5e RHAQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bandewar-net.20150623.gappssmtp.com header.s=20150623 header.b=ul7n0FZx; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id p2si8653477pfd.273.2017.11.09.21.38.43; Thu, 09 Nov 2017 21:38:55 -0800 (PST) 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=@bandewar-net.20150623.gappssmtp.com header.s=20150623 header.b=ul7n0FZx; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755398AbdKJFiA (ORCPT + 83 others); Fri, 10 Nov 2017 00:38:00 -0500 Received: from mail-io0-f195.google.com ([209.85.223.195]:43827 "EHLO mail-io0-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751371AbdKJFh6 (ORCPT ); Fri, 10 Nov 2017 00:37:58 -0500 Received: by mail-io0-f195.google.com with SMTP id 134so12360384ioo.0 for ; Thu, 09 Nov 2017 21:37:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bandewar-net.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id; bh=TX+dvm+YSNktTBbynQpVR95yE+ooI3MJyBYVej2/vtM=; b=ul7n0FZxM/inFnBWClXQjBbTKz7CMAvSuFx51vqXaR5BWW6a9fnPmQUkReEEE4uX5C 1MTO+jz1nX9WmFibzcjnyfZXIh5aqorpZnsLYuwFq5AR/UfgnDOIBarVpJx0hWxHZZxm IKAALQiTz2EbCsZTgPR8tR6ej5np3hie1AeudTPF0T9pUE9jD3QQ9Zuq/SWh4Yu9Dxji mailV0JX+C3prX6/GVO83Ed8vLPSSRV6tzM7rHjOmTicKp7YRXiJL4NK8Hw55WrY1cn9 VgVIJdE6Bpa9yhIUIB0/DDfM6Mx8oOemu4IBkVqLtfMdha5wf27Y5Ppjb4AFAb9zUmfR ezpA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=TX+dvm+YSNktTBbynQpVR95yE+ooI3MJyBYVej2/vtM=; b=a4XRQ/cbxwzso3XAWPlPEOtdfboa4a1RntscETWYAy2h6utsvzj8NI+xVrO4NdaDtK h9WhL1/hEqSIkqQ1ZeLt5VaCBSU4mwaNWo/8BJIGGwsY/yJ8XuXM6qZwzbrDx/+hMUVJ jAyWm/Pgb/7yS2rPmAQBmJf7BXkw7idxhs8LxZ1Xioll8Dxk+Q1GWoaZddQXQfoE8rR7 f9i/QBjpG5aBy43E5JXGF2hmC45uI3j5qqxiZ9IS8pjFLhkOcNv8rWR3Ng7l1U0rlGMw r6E0MTj3H2NDijz5NTvnPMP/UI+PsXt8NGedOkRgprA1DbDxuJSXTF52n1h2+Y3/1+25 4m7Q== X-Gm-Message-State: AJaThX7T7QVjy/1/wytpjUWg99ovcqQe7WvKEjfSjcv571Z3c+eTHyJZ IfAn5Ar8GHrjx1D8JSTaRuWGxdITHQc= X-Received: by 10.107.168.82 with SMTP id r79mr812330ioe.158.1510292277396; Thu, 09 Nov 2017 21:37:57 -0800 (PST) Received: from localhost ([2620:15c:2c4:201:1505:9ec4:1586:9751]) by smtp.gmail.com with ESMTPSA id 200sm378785itm.13.2017.11.09.21.37.56 (version=TLS1_2 cipher=AES128-SHA bits=128/128); Thu, 09 Nov 2017 21:37:56 -0800 (PST) From: Mahesh Bandewar To: LKML , Netdev Cc: Kernel-hardening , Linux API , Kees Cook , Serge Hallyn , "Eric W . Biederman" , Eric Dumazet , David Miller , Mahesh Bandewar , Mahesh Bandewar Subject: [PATCHv2 1/2] capability: introduce sysctl for controlled user-ns capability whitelist Date: Thu, 9 Nov 2017 21:37:47 -0800 Message-Id: <20171110053747.21093-1-mahesh@bandewar.net> X-Mailer: git-send-email 2.15.0.448.gf294e3d99a-goog Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Mahesh Bandewar Add a sysctl variable kernel.controlled_userns_caps_whitelist. This takes input as capability mask expressed as two comma separated hex u32 words. The mask, however, is stored in kernel as kernel_cap_t type. Any capabilities that are not part of this mask will be controlled and will not be allowed to processes in controlled user-ns. Signed-off-by: Mahesh Bandewar --- v2: Rebase v1: Initial submission Documentation/sysctl/kernel.txt | 21 ++++++++++++++++++ include/linux/capability.h | 3 +++ kernel/capability.c | 47 +++++++++++++++++++++++++++++++++++++++++ kernel/sysctl.c | 5 +++++ 4 files changed, 76 insertions(+) diff --git a/Documentation/sysctl/kernel.txt b/Documentation/sysctl/kernel.txt index 694968c7523c..a1d39dbae847 100644 --- a/Documentation/sysctl/kernel.txt +++ b/Documentation/sysctl/kernel.txt @@ -25,6 +25,7 @@ show up in /proc/sys/kernel: - bootloader_version [ X86 only ] - callhome [ S390 only ] - cap_last_cap +- controlled_userns_caps_whitelist - core_pattern - core_pipe_limit - core_uses_pid @@ -187,6 +188,26 @@ CAP_LAST_CAP from the kernel. ============================================================== +controlled_userns_caps_whitelist + +Capability mask that is whitelisted for "controlled" user namespaces. +Any capability that is missing from this mask will not be allowed to +any process that is attached to a controlled-userns. e.g. if CAP_NET_RAW +is not part of this mask, then processes running inside any controlled +userns's will not be allowed to perform action that needs CAP_NET_RAW +capability. However, processes that are attached to a parent user-ns +hierarchy that is *not* controlled and has CAP_NET_RAW can continue +performing those actions. User-namespaces are marked "controlled" at +the time of their creation based on the capabilities of the creator. +A process that does not have CAP_SYS_ADMIN will create user-namespaces +that are controlled. + +The value is expressed as two comma separated hex words (u32). This +sysctl is avaialble in init-ns and users with CAP_SYS_ADMIN in init-ns +are allowed to make changes. + +============================================================== + core_pattern: core_pattern is used to specify a core dumpfile pattern name. diff --git a/include/linux/capability.h b/include/linux/capability.h index f640dcbc880c..7d79a4689625 100644 --- a/include/linux/capability.h +++ b/include/linux/capability.h @@ -14,6 +14,7 @@ #define _LINUX_CAPABILITY_H #include +#include #define _KERNEL_CAPABILITY_VERSION _LINUX_CAPABILITY_VERSION_3 @@ -248,6 +249,8 @@ extern bool ptracer_capable(struct task_struct *tsk, struct user_namespace *ns); /* audit system wants to get cap info from files as well */ extern int get_vfs_caps_from_disk(const struct dentry *dentry, struct cpu_vfs_cap_data *cpu_caps); +int proc_douserns_caps_whitelist(struct ctl_table *table, int write, + void __user *buff, size_t *lenp, loff_t *ppos); extern int cap_convert_nscap(struct dentry *dentry, void **ivalue, size_t size); diff --git a/kernel/capability.c b/kernel/capability.c index 1e1c0236f55b..4a859b7d4902 100644 --- a/kernel/capability.c +++ b/kernel/capability.c @@ -29,6 +29,8 @@ EXPORT_SYMBOL(__cap_empty_set); int file_caps_enabled = 1; +kernel_cap_t controlled_userns_caps_whitelist = CAP_FULL_SET; + static int __init file_caps_disable(char *str) { file_caps_enabled = 0; @@ -507,3 +509,48 @@ bool ptracer_capable(struct task_struct *tsk, struct user_namespace *ns) rcu_read_unlock(); return (ret == 0); } + +/* Controlled-userns capabilities routines */ +#ifdef CONFIG_SYSCTL +int proc_douserns_caps_whitelist(struct ctl_table *table, int write, + void __user *buff, size_t *lenp, loff_t *ppos) +{ + DECLARE_BITMAP(caps_bitmap, CAP_LAST_CAP); + struct ctl_table caps_table; + char tbuf[NAME_MAX]; + int ret; + + ret = bitmap_from_u32array(caps_bitmap, CAP_LAST_CAP, + controlled_userns_caps_whitelist.cap, + _KERNEL_CAPABILITY_U32S); + if (ret != CAP_LAST_CAP) + return -1; + + scnprintf(tbuf, NAME_MAX, "%*pb", CAP_LAST_CAP, caps_bitmap); + + caps_table.data = tbuf; + caps_table.maxlen = NAME_MAX; + caps_table.mode = table->mode; + ret = proc_dostring(&caps_table, write, buff, lenp, ppos); + if (ret) + return ret; + if (write) { + kernel_cap_t tmp; + + if (!capable(CAP_SYS_ADMIN)) + return -EPERM; + + ret = bitmap_parse_user(buff, *lenp, caps_bitmap, CAP_LAST_CAP); + if (ret) + return ret; + + ret = bitmap_to_u32array(tmp.cap, _KERNEL_CAPABILITY_U32S, + caps_bitmap, CAP_LAST_CAP); + if (ret != CAP_LAST_CAP) + return -1; + + controlled_userns_caps_whitelist = tmp; + } + return 0; +} +#endif /* CONFIG_SYSCTL */ diff --git a/kernel/sysctl.c b/kernel/sysctl.c index d9c31bc2eaea..25c3f7b76ece 100644 --- a/kernel/sysctl.c +++ b/kernel/sysctl.c @@ -1226,6 +1226,11 @@ static struct ctl_table kern_table[] = { .extra2 = &one, }, #endif + { + .procname = "controlled_userns_caps_whitelist", + .mode = 0644, + .proc_handler = proc_douserns_caps_whitelist, + }, { } }; -- 2.15.0.448.gf294e3d99a-goog From 1585228917799205609@xxx Mon Nov 27 14:15:04 +0000 2017 X-GM-THRID: 1585224131925117207 X-Gmail-Labels: Inbox,Category Forums,HistoricalUnread