Received: by 10.213.65.68 with SMTP id h4csp1323550imn; Wed, 14 Mar 2018 17:13:41 -0700 (PDT) X-Google-Smtp-Source: AG47ELuP3+HJsH/or0Q8MYvF64QYAWPOw8lCWBkHo7c/Hgtc8Gu9V0YJcPfRRLvf/6hvOLVrrtQw X-Received: by 2002:a17:902:367:: with SMTP id 94-v6mr5953052pld.140.1521072821013; Wed, 14 Mar 2018 17:13:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1521072820; cv=none; d=google.com; s=arc-20160816; b=xuKOhg/b6wHlLQrG6CQHbWEnsLg1T10CTMzzcSzlz+jBWwxp6mPkKaHssLlcBRTyiR cGIyZMcSiqdd6OtawhbaNyJoVGmNmluqM6tdLhveGcZzBHXYNqPlWQLGdH92TZfCxThB zNprkMZtw2EAeA7ods2jMpVXhwO1zc8wL1OL7X2DCDfLpy9bWEmwZyNFIZlLZJ0xX3HB FO6+aCXWBgwzDGeWLYjy3lvFgwWDwdTSWwD21uS7vNp+xb5qOU5DPK4jE3zTAJ+uBQ71 d17ALZOZXwqTVn9P8/MK/QpBuwyvO7mP4Mf/5urhOVK8SJd25G4FZUgkdl4IHoHCdGmw W7hA== 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 :arc-authentication-results; bh=DvkbaM72HLYP6nI1NnrPrEUuFhKULRoKMw8+naZZNZ4=; b=YK/tn0/SV/i63SqGPndjl1kYSC+vRVLDcOAkD2xCi/tkbDYdiLe59vRunjY+gYfjY1 UuMz0ni3pMBMriHlKYeWzs/9faB8xwhZ759UUXGt1FQxJzZhsfv6ZfiS4WP75EXHK8ts 0ZUm8RSoXZkHzMSuTdz3fAlV+/CwSJb/fCUVlMw357hSWBq9BGuYN+udJA+dgrVsKus+ CIBm4VPJP20Xgu7fYBGVcRLa9JNbaURiHsQxON+wdJM9tX/EOyS15PbD1UTvvxkDEMMf tGtC4wslEI0Gg9x5uprQuAivowmE7tAFto3/dWfrZECB+zAa6I9kV59KfpWaK+9UDzVW huag== ARC-Authentication-Results: i=1; mx.google.com; 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 j3si2553579pgv.342.2018.03.14.17.13.26; Wed, 14 Mar 2018 17:13:40 -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; 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 S1751523AbeCOAMe (ORCPT + 99 others); Wed, 14 Mar 2018 20:12:34 -0400 Received: from mail-wm0-f41.google.com ([74.125.82.41]:39095 "EHLO mail-wm0-f41.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751131AbeCOAMc (ORCPT ); Wed, 14 Mar 2018 20:12:32 -0400 Received: by mail-wm0-f41.google.com with SMTP id u10so7097817wmu.4; Wed, 14 Mar 2018 17:12:31 -0700 (PDT) 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=DvkbaM72HLYP6nI1NnrPrEUuFhKULRoKMw8+naZZNZ4=; b=CseOaHCUd4M3CjqW0NDhSmwswz+a4zXOIayW8KvJFkoelETOf5d5sT7OnriiP9sn69 rYnpzaEVYxL3ZqlsxFvjMUIzCZxMxOhzuX2W5HVaKM0NrzmzKdyWL/yOYNd8sfzD17KQ kecuG4RYRRTFCrCJ22tw1r+DLe3iaokETq39B5556Aso8woQcQcCHwpDWb60n2u7/Ttx eRXuct7vzNxOqP1RWeOVP4SQNAx37v0k0+WUB2XFyslw35AHSa58EDQJnffizi1fuWaY 1SOHku+YKFRvY6QStRQ+deOJ+kEoQYty96ZOx/Rw0P+eTpstQV6baTTHXS8E0XGfFkzG rB/w== X-Gm-Message-State: AElRT7ECf6qvoTK+yiBMGVgroFYLdK8L9HBzqtwF4TQ4vfKEsW7SlPZD 6hKW5EKLKmAkdZD1rYxI62w= X-Received: by 10.28.191.90 with SMTP id p87mr2864906wmf.145.1521072750986; Wed, 14 Mar 2018 17:12:30 -0700 (PDT) Received: from localhost.localdomain ([2a02:8070:8895:9700:4c9d:d656:8952:c1b2]) by smtp.gmail.com with ESMTPSA id d12sm1606608wri.9.2018.03.14.17.12.28 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 14 Mar 2018 17:12:29 -0700 (PDT) From: Christian Brauner To: ebiederm@xmission.com, gregkh@linuxfoundation.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org Cc: serge@hallyn.com, Christian Brauner Subject: [PATCH] netns: send uevent messages Date: Thu, 15 Mar 2018 01:12:14 +0100 Message-Id: <20180315001214.19462-1-christian.brauner@ubuntu.com> X-Mailer: git-send-email 2.15.1 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This patch adds a receive method to NETLINK_KOBJECT_UEVENT netlink sockets to allow sending uevent messages into the network namespace the socket belongs to. Currently non-initial network namespaces are already isolated and don't receive uevents. There are a number of cases where it is beneficial for a sufficiently privileged userspace process to send a uevent into a network namespace. One such use case would be debugging and fuzzing of a piece of software which listens and reacts to uevents. By running a copy of that software inside a network namespace, specific uevents could then be presented to it. More concretely, this would allow for easy testing of udevd/ueventd. This will also allow some piece of software to run components inside a separate network namespace and then effectively filter what that software can receive. Some examples of software that do directly listen to uevents and that we have in the past attempted to run inside a network namespace are rbd (CEPH client) or the X server. Implementation: The implementation has been kept as simple as possible from the kernel's perspective. Specifically, a simple input method uevent_net_rcv() is added to NETLINK_KOBJECT_UEVENT sockets which completely reuses existing af_netlink infrastructure and does neither add an additional netlink family nor requires any user-visible changes. For example, by using netlink_rcv_skb() we can make use of existing netlink infrastructure to report back informative error messages to userspace. Furthermore, this implementation does not introduce any overhead for existing uevent generating codepaths. The struct netns gets a new uevent socket member that records the uevent socket associated with that network namespace. Since we record the uevent socket for each network namespace in struct net we don't have to walk the whole uevent socket list. Instead we can directly retrieve the relevant uevent socket and send the message. This keeps the codepath very performant without introducing needless overhead. Uevent sequence numbers are kept global. When a uevent message is sent to another network namespace the implementation will simply increment the global uevent sequence number and append it to the received uevent. This has the advantage that the kernel will never need to parse the received uevent message to replace any existing uevent sequence numbers. Instead it is up to the userspace process to remove any existing uevent sequence numbers in case the uevent message to be sent contains any. Security: In order for a caller to send uevent messages to a target network namespace the caller must have CAP_SYS_ADMIN in the owning user namespace of the target network namespace. Additionally, any received uevent message is verified to not exceed size UEVENT_BUFFER_SIZE. This includes the space needed to append the uevent sequence number. Testing: This patch has been tested and verified to work with the following udev implementations: 1. CentOS 6 with udevd version 147 2. Debian Sid with systemd-udevd version 237 3. Android 7.1.1 with ueventd Signed-off-by: Christian Brauner --- include/net/net_namespace.h | 1 + lib/kobject_uevent.c | 88 ++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 88 insertions(+), 1 deletion(-) diff --git a/include/net/net_namespace.h b/include/net/net_namespace.h index f306b2aa15a4..467bde763a9b 100644 --- a/include/net/net_namespace.h +++ b/include/net/net_namespace.h @@ -78,6 +78,7 @@ struct net { struct sock *rtnl; /* rtnetlink socket */ struct sock *genl_sock; + struct sock *uevent_sock; /* uevent socket */ struct list_head dev_base_head; struct hlist_head *dev_name_head; diff --git a/lib/kobject_uevent.c b/lib/kobject_uevent.c index 9fe6ec8fda28..10b2144b9fc3 100644 --- a/lib/kobject_uevent.c +++ b/lib/kobject_uevent.c @@ -25,6 +25,7 @@ #include #include #include +#include #include @@ -602,12 +603,94 @@ int add_uevent_var(struct kobj_uevent_env *env, const char *format, ...) EXPORT_SYMBOL_GPL(add_uevent_var); #if defined(CONFIG_NET) +static int uevent_net_send(struct sock *usk, struct sk_buff *skb, + struct netlink_ext_ack *extack) +{ + int ret; + u64 seqnum; + /* u64 to chars: 2^64 - 1 = 21 chars */ + char buf[sizeof("SEQNUM=") + 21]; + struct sk_buff *skbc; + + /* bump sequence number */ + mutex_lock(&uevent_sock_mutex); + seqnum = ++uevent_seqnum; + mutex_unlock(&uevent_sock_mutex); + + /* prepare sequence number */ + ret = snprintf(buf, sizeof(buf), "SEQNUM=%llu", seqnum); + if (ret < 0 || (size_t)ret >= sizeof(buf)) + return -ENOMEM; + ret++; + + /* verify message does not overflow */ + if ((skb->len + ret) > UEVENT_BUFFER_SIZE) { + NL_SET_ERR_MSG(extack, "uevent message too big"); + return -EINVAL; + } + + /* copy skb and extend to accommodate sequence number */ + skbc = skb_copy_expand(skb, 0, ret, GFP_KERNEL); + if (!skbc) + return -ENOMEM; + + /* append sequence number */ + skb_put_data(skbc, buf, ret); + + /* remove msg header */ + skb_pull(skbc, NLMSG_HDRLEN); + + /* set portid 0 to inform userspace message comes from kernel */ + NETLINK_CB(skbc).portid = 0; + NETLINK_CB(skbc).dst_group = 1; + + ret = netlink_broadcast(usk, skbc, 0, 1, GFP_KERNEL); + /* ENOBUFS should be handled in userspace */ + if (ret == -ENOBUFS || ret == -ESRCH) + ret = 0; + + return ret; +} + +static int uevent_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, + struct netlink_ext_ack *extack) +{ + void *msg; + struct net *target_net; + struct sock *target_sk; + + msg = nlmsg_data(nlh); + if (!msg) + return -EINVAL; + + target_net = sock_net(NETLINK_CB(skb).sk); + target_sk = target_net->uevent_sock; + + /* + * Verify that we are allowed to send messages to the target network + * namespace. The caller must have CAP_SYS_ADMIN in the owning user + * namespace of the target network namespace. + */ + if (!netlink_ns_capable(skb, target_net->user_ns, CAP_SYS_ADMIN)) { + NL_SET_ERR_MSG(extack, "missing CAP_SYS_ADMIN capability"); + return -EPERM; + } + + return uevent_net_send(target_sk, skb, extack); +} + +static void uevent_net_rcv(struct sk_buff *skb) +{ + netlink_rcv_skb(skb, &uevent_rcv_msg); +} + static int uevent_net_init(struct net *net) { struct uevent_sock *ue_sk; struct netlink_kernel_cfg cfg = { .groups = 1, - .flags = NL_CFG_F_NONROOT_RECV, + .input = uevent_net_rcv, + .flags = NL_CFG_F_NONROOT_RECV }; ue_sk = kzalloc(sizeof(*ue_sk), GFP_KERNEL); @@ -621,6 +704,9 @@ static int uevent_net_init(struct net *net) kfree(ue_sk); return -ENODEV; } + + net->uevent_sock = ue_sk->sk; + mutex_lock(&uevent_sock_mutex); list_add_tail(&ue_sk->list, &uevent_sock_list); mutex_unlock(&uevent_sock_mutex); -- 2.15.1