Received: by 10.192.165.156 with SMTP id m28csp130483imm; Wed, 18 Apr 2018 18:48:16 -0700 (PDT) X-Google-Smtp-Source: AIpwx49VU/i6QBD1N89BKD3GKS+WeTVXs7l80SJzxMKS0I46+VTrEL40EiLtMsQjEhrDA+tRi0jt X-Received: by 2002:a17:902:7291:: with SMTP id d17-v6mr4211321pll.218.1524102496674; Wed, 18 Apr 2018 18:48:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1524102496; cv=none; d=google.com; s=arc-20160816; b=FSMnJ6cPFkBcMWz4vamNaTqy4Abs2qMvAMjwNZ8qHa7jdzkYORvfx5zIXjxIip3jjd V6eILwsGO0pakqKTUWoe2PwTyicneS+BcO6C+sqE+mUiXK3p2stjVEd7D2cYEVTHT5AU guVplUuylVcBiaJuS7L2vId8vHAREo04M+H+yOipXHYyc+xagaQiCms4jAIHcNkJk1Ut 9cCFU7iLGi63iucYnZ4EhIgnxPFeZXHBWSVeE+4c2GYvxbIJ54ofRedNujVLyWNVKgd9 EjNTFDWV4YBL6MHYBXO20vZZaL2jbnOg01Rnmmul+UA5jwclqnTFT3EhO58O9cCDcW1a WEnw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :references:in-reply-to:mime-version:dkim-signature :arc-authentication-results; bh=KJX8E6IsOSWnsZk3Va4V81Mht8oZ9fXCUn8xpUkxf2A=; b=gbLc1aDOyQYr6uoiILWx/QAiZtcbuOPw51jH1uYhKkRH7FZHSwzTvDZ4hjMl9RxIax vWeQ8aTNCDnvwoH2+It8uRweWRmNFtVmo1ULWQi2CbIVFMNcq2KE0u4l/ZhO0hicv3XF tuFo86ZTGv9wPyOjOMUYH0PiVP1FpdgDft79DHqi7I//7KQftKiNJN0fglHLBeAHVWYn GEZiWChK9sbl9FIlE0vPG8epfd7VhAvip5rY3dIrkxc5JY74NQtbehYCysYJF71oiVmP J7uBIzd1fzDuWQFZ8cdfl5MkK/PMwOzQWt8TkbpVDuxfha4/+NNOuhy2URrEKz1lPvTf BbOQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@paul-moore-com.20150623.gappssmtp.com header.s=20150623 header.b=E+AwJVaW; 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 y2si2125412pgb.422.2018.04.18.18.48.02; Wed, 18 Apr 2018 18:48:16 -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=@paul-moore-com.20150623.gappssmtp.com header.s=20150623 header.b=E+AwJVaW; 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 S1752809AbeDSBqz (ORCPT + 99 others); Wed, 18 Apr 2018 21:46:55 -0400 Received: from mail-lf0-f66.google.com ([209.85.215.66]:40648 "EHLO mail-lf0-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752499AbeDSBqw (ORCPT ); Wed, 18 Apr 2018 21:46:52 -0400 Received: by mail-lf0-f66.google.com with SMTP id i18-v6so5393831lfc.7 for ; Wed, 18 Apr 2018 18:46:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=paul-moore-com.20150623.gappssmtp.com; s=20150623; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=KJX8E6IsOSWnsZk3Va4V81Mht8oZ9fXCUn8xpUkxf2A=; b=E+AwJVaW8LcI4MLqfgSzgFDpmS9dODyn+Pl8EE7gH807+CAfaqmeqZt/lY3mqzzVEV v9uIWksRbTY+GBa/97hEC137P7T+Taz2inTJalBy8gdb6Og7TJb40ZuVR3Y0aAsBBUsx LwcvI2SwdfFfHXfSFRYAWnoHZiFJOG6MKjsNxRyIf19ogkWpcsguEsII3srB6vNw9yXd pYaWwOJBwTrJGza5EbA8LZSGyywfUBDYTZsCSQf8N88NpU3MY66K3b1IprkDGJR4Bv7r rBX/cjYCWnytfYxegdzr3jFGVmNVsifPQHTeuNCeRLWH+rfeBXdYRFE/AYe9heb4zHnh SAfQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=KJX8E6IsOSWnsZk3Va4V81Mht8oZ9fXCUn8xpUkxf2A=; b=pqdHo8Ni/WMOX+5aU6qjTH3sB+xJmBr7TK9xxFxZB095EB7Ir/IobkruRFqDXTPqfd xVqkYSgTsWZ9m4bfgldVQxq8Qrrf8+XkpmnFRTRM+vCjVW0MGp9HitqEDzmwSSGWxoRd 7N/jAmNp1xPOC7cQOaqyOnlc6jywqIQ0AMJ5FhIdAYqGFWOw1YDj82Fkc1gW3kEkoyyI Rp/7rgA7gsyxRTDnSOPLdIcGD5l6+/QxJl33eYuqnzgTjN16r1LT5R7v6dhaZLrN2zcV MLjMVu5u9UKc2j2UvPEvtDqljruepNJgdJM/uIPD2THLKuX7F0nKgOw/uRPz83JLaUUc Q+mA== X-Gm-Message-State: ALQs6tA5m7jPmmOSsy8+j/MrH+DiScvq+EIpZ/bec05zfJ6e4hvYuaeZ NsWQZzpWuzNDYHHsy24Y2LClWQNkaoV2+SR2SmzE X-Received: by 10.46.135.7 with SMTP id m7mr2907099lji.106.1524102410545; Wed, 18 Apr 2018 18:46:50 -0700 (PDT) MIME-Version: 1.0 Received: by 2002:a19:a5c3:0:0:0:0:0 with HTTP; Wed, 18 Apr 2018 18:46:49 -0700 (PDT) X-Originating-IP: [108.20.156.165] In-Reply-To: <11b43a498e768a14764594c808a96b34d52be0af.1521179281.git.rgb@redhat.com> References: <11b43a498e768a14764594c808a96b34d52be0af.1521179281.git.rgb@redhat.com> From: Paul Moore Date: Wed, 18 Apr 2018 21:46:49 -0400 Message-ID: Subject: Re: [RFC PATCH ghak32 V2 11/13] audit: add support for containerid to network namespaces To: Richard Guy Briggs Cc: cgroups@vger.kernel.org, containers@lists.linux-foundation.org, linux-api@vger.kernel.org, Linux-Audit Mailing List , linux-fsdevel@vger.kernel.org, LKML , netdev@vger.kernel.org, ebiederm@xmission.com, luto@kernel.org, jlayton@redhat.com, carlos@redhat.com, dhowells@redhat.com, viro@zeniv.linux.org.uk, simo@redhat.com, Eric Paris , serge@hallyn.com Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Mar 16, 2018 at 5:00 AM, Richard Guy Briggs wrote: > Audit events could happen in a network namespace outside of a task > context due to packets received from the net that trigger an auditing > rule prior to being associated with a running task. The network > namespace could in use by multiple containers by association to the > tasks in that network namespace. We still want a way to attribute > these events to any potential containers. Keep a list per network > namespace to track these container identifiiers. > > Add/increment the container identifier on: > - initial setting of the container id via /proc > - clone/fork call that inherits a container identifier > - unshare call that inherits a container identifier > - setns call that inherits a container identifier > Delete/decrement the container identifier on: > - an inherited container id dropped when child set > - process exit > - unshare call that drops a net namespace > - setns call that drops a net namespace > > See: https://github.com/linux-audit/audit-kernel/issues/32 > See: https://github.com/linux-audit/audit-testsuite/issues/64 > Signed-off-by: Richard Guy Briggs > --- > include/linux/audit.h | 7 +++++++ > include/net/net_namespace.h | 12 ++++++++++++ > kernel/auditsc.c | 9 ++++++--- > kernel/nsproxy.c | 6 ++++++ > net/core/net_namespace.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ > 5 files changed, 76 insertions(+), 3 deletions(-) ... > diff --git a/include/net/net_namespace.h b/include/net/net_namespace.h > index 0490084..343a428 100644 > --- a/include/net/net_namespace.h > +++ b/include/net/net_namespace.h > @@ -33,6 +33,7 @@ > #include > #include > #include > +#include > > struct user_namespace; > struct proc_dir_entry; > @@ -150,6 +151,7 @@ struct net { > #endif > struct sock *diag_nlsk; > atomic_t fnhe_genid; > + struct list_head audit_containerid; > } __randomize_layout; We talked about this briefly off-list, you should be using audit_net and the net_generic mechanism instead of this. > #include > @@ -301,6 +303,16 @@ static inline struct net *read_pnet(const possible_net_t *pnet) > #define __net_initconst __initconst > #endif > > +#ifdef CONFIG_NET_NS > +void net_add_audit_containerid(struct net *net, u64 containerid); > +void net_del_audit_containerid(struct net *net, u64 containerid); > +#else > +static inline void net_add_audit_containerid(struct net *, u64) > +{ } > +static inline void net_del_audit_containerid(struct net *, u64) > +{ } > +#endif > + > int peernet2id_alloc(struct net *net, struct net *peer); > int peernet2id(struct net *net, struct net *peer); > bool peernet_has_id(struct net *net, struct net *peer); > diff --git a/kernel/auditsc.c b/kernel/auditsc.c > index 2f02ed9..208da962 100644 > --- a/kernel/auditsc.c > +++ b/kernel/auditsc.c > @@ -75,6 +75,7 @@ > #include > #include > #include > +#include > > #include "audit.h" > > @@ -2175,16 +2176,18 @@ static void audit_log_set_containerid(struct task_struct *task, u64 oldcontainer > */ > int audit_set_containerid(struct task_struct *task, u64 containerid) > { > - u64 oldcontainerid; > + u64 oldcontainerid = audit_get_containerid(task); > int rc; > - > - oldcontainerid = audit_get_containerid(task); > + struct net *net = task->nsproxy->net_ns; > > rc = audit_set_containerid_perm(task, containerid); > if (!rc) { > + if (cid_valid(oldcontainerid)) > + net_del_audit_containerid(net, oldcontainerid); Using audit_net we can handle this internal to audit, which is a Good Thing. > task_lock(task); > task->containerid = containerid; > task_unlock(task); > + net_add_audit_containerid(net, containerid); Same. > } > > audit_log_set_containerid(task, oldcontainerid, containerid, rc); > diff --git a/kernel/nsproxy.c b/kernel/nsproxy.c > index f6c5d33..d9f1090 100644 > --- a/kernel/nsproxy.c > +++ b/kernel/nsproxy.c > @@ -140,6 +140,7 @@ int copy_namespaces(unsigned long flags, struct task_struct *tsk) > struct nsproxy *old_ns = tsk->nsproxy; > struct user_namespace *user_ns = task_cred_xxx(tsk, user_ns); > struct nsproxy *new_ns; > + u64 containerid = audit_get_containerid(tsk); > > if (likely(!(flags & (CLONE_NEWNS | CLONE_NEWUTS | CLONE_NEWIPC | > CLONE_NEWPID | CLONE_NEWNET | > @@ -167,6 +168,7 @@ int copy_namespaces(unsigned long flags, struct task_struct *tsk) > return PTR_ERR(new_ns); > > tsk->nsproxy = new_ns; > + net_add_audit_containerid(new_ns->net_ns, containerid); > return 0; > } Hopefully we can handle this in audit_net_init(), we just need to figure out where we can get the correct task_struct for the audit container ID (some backpointer in the net struct?). > @@ -217,6 +219,7 @@ int unshare_nsproxy_namespaces(unsigned long unshare_flags, > void switch_task_namespaces(struct task_struct *p, struct nsproxy *new) > { > struct nsproxy *ns; > + u64 containerid = audit_get_containerid(p); > > might_sleep(); > > @@ -224,6 +227,9 @@ void switch_task_namespaces(struct task_struct *p, struct nsproxy *new) > ns = p->nsproxy; > p->nsproxy = new; > task_unlock(p); > + net_del_audit_containerid(ns->net_ns, containerid); > + if (new) > + net_add_audit_containerid(new->net_ns, containerid); Okay, we might need a hook here for switching namespaces, but I would much rather it be a generic audit hook that calls directly into audit. -- paul moore www.paul-moore.com