Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp4302019pxu; Mon, 21 Dec 2020 09:02:27 -0800 (PST) X-Google-Smtp-Source: ABdhPJzyK5zHt0rEgT1sDeff4Tt86YkLE6XjelEv563juyfNYs117otsGGXHpK1xrDHb2Ckuu/te X-Received: by 2002:a1c:9d8b:: with SMTP id g133mr17633963wme.189.1608570146856; Mon, 21 Dec 2020 09:02:26 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1608570146; cv=none; d=google.com; s=arc-20160816; b=T3T5lD4ilLbIvxg/QdvMMUl+1ieH6R3Ke2uaoWd6sgrvsdW2MG1q0Wf52CUaweU5rS EaI0HkRt1UWi7309UazvWC1g1Pe91vmRLTUn6rgKffCZx0i5YX9ggjqAIHCWt6/pAmMf jNNE2G0VG/xlBgT/ATy3Vnmu4+lZUqhUhoM2PHEvGpVWcYe4iuOZsnRt7KhJ26tuCgWk SxqROa3NCEouIYTwDa3hjbI9q8A9flvFhBP+gNgBPh84Wooz6zLOCRspCJeFPR6y5S++ pVnIWLcN+Hka7eEtNaMlM8iryIFr48XLrOiclOA2wCmBLm6+Je45a6hAj4EnYoFIm8qT 3ZBQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:references:in-reply-to:message-id:date:subject :cc:to:from:dkim-signature; bh=yPgYcoP9VP6fa7Mr7AH/bm+8Wq47T8v4BZfABnwLLcw=; b=x/zYP3l4mxoQYx78ryF5NHUEUVKecpxWiGQdHK7MRP8GoGu+fZdyLtfOn8+0qSx9Nf 8AJ0R+yZ1cH1FD+MS6ZMSOl7+8KLqjvbU0HA555bDxrW7Qv/9gdN3ppax2bVvq6P1r/6 /Ibxi8+WzRZZpY5AdrpNtfMUWnspnU+c4nKedV0aFS8olGrty+CswoL+k+1H2tQUQblm ryqceq0VK1KYoeUJJfokWGcobwNnLogXcLQAY0QfVTf7mUty6673k4uoxLpgptDhekUa 1wxu4HlX8jkq4pw1XiYUlOyI7tP3ILUyLxzaTNR/RqKuShl5ca/yByfIt6KFNc3eTDBX 5qhA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=QlkGznBr; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id v24si10915124edy.573.2020.12.21.09.02.03; Mon, 21 Dec 2020 09:02:26 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=QlkGznBr; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726656AbgLURAG (ORCPT + 99 others); Mon, 21 Dec 2020 12:00:06 -0500 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:45783 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725850AbgLURAF (ORCPT ); Mon, 21 Dec 2020 12:00:05 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1608569918; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:in-reply-to:in-reply-to:references:references; bh=yPgYcoP9VP6fa7Mr7AH/bm+8Wq47T8v4BZfABnwLLcw=; b=QlkGznBrnzAxRm3MKcYS727lSrrZMRZmRgzdkiR3r95q0jHzMZCqXCzfnWyUd0cz9YJl2o lu4Aamt/3oeADEpzXk/ZcxjnHbUhaCvhm8GXPiMtuF+QuBSCLa0DoUxVCZ+Wa46a5PQ3TL QqOeUjdDeTPgBG7YZHVVE1yfe6dv1l4= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-173-XfR89AuENdmL3ZH03qxN6A-1; Mon, 21 Dec 2020 11:58:34 -0500 X-MC-Unique: XfR89AuENdmL3ZH03qxN6A-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 91CEF801817; Mon, 21 Dec 2020 16:58:32 +0000 (UTC) Received: from madcap2.tricolour.ca (unknown [10.10.110.9]) by smtp.corp.redhat.com (Postfix) with ESMTP id D514860C61; Mon, 21 Dec 2020 16:58:15 +0000 (UTC) From: Richard Guy Briggs To: Linux Containers List , Linux API , Linux-Audit Mailing List , Linux FSdevel , LKML , Linux NetDev Upstream Mailing List , Netfilter Devel List Cc: Neil Horman , David Howells , "Eric W. Biederman" , Simo Sorce , Eric Paris , mpatel@redhat.com, Serge Hallyn , Kees Cook , Jens Axboe , Christian Brauner , Richard Guy Briggs Subject: [PATCH ghak90 v10 10/11] audit: track container nesting Date: Mon, 21 Dec 2020 11:55:44 -0500 Message-Id: In-Reply-To: References: X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Track the parent container of a container to be able to filter and report nesting. Now that we have a way to track and check the parent container of a container, modify the contid field format to be able to report that nesting using a carrat ("^") modifier to indicate nesting. The original field format was "contid=" for task-associated records and "contid=[,[...]]" for network-namespace-associated records. The new field format is "contid=[,^[...]][,[...]]". For task event example, an orchestrator in contid 1 spawns tasks in contid 2 and contid 3, then the task in contid 2 spawns a task in contid 4. An event happens in the task in contid 4: type=SYSCALL ... type=CONTAINER_ID msg=audit(:): contid=4,^2,^1 For a network namespace event example, an orchestrator in contid 1 in network namespace A spawns peer tasks 2 and 3 in network namespace B. An event happens in network namespace B: type=NETFILTER_PKT ... type=CONTAINER_ID msg=audit(:): contid=2,^1,3,^1 Signed-off-by: Richard Guy Briggs --- kernel/audit.c | 75 +++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 62 insertions(+), 13 deletions(-) diff --git a/kernel/audit.c b/kernel/audit.c index 6eed8ed0cc8e..46ddf49f731f 100644 --- a/kernel/audit.c +++ b/kernel/audit.c @@ -231,6 +231,7 @@ struct audit_contobj { refcount_t refcount; refcount_t sigflag; struct rcu_head rcu; + struct audit_contobj *parent; }; struct audit_task_info { @@ -253,6 +254,7 @@ struct audit_contobj_netns { static void audit_netns_contid_add(struct net *net, struct audit_contobj *cont); static void audit_netns_contid_del(struct net *net, struct audit_contobj *cont); +static void audit_log_contid(struct audit_buffer *ab, struct audit_contobj *cont); void __init audit_task_init(void) { @@ -378,6 +380,7 @@ static void _audit_contobj_put_sig(struct audit_contobj *cont) refcount_set(&cont->sigflag, 0); if (!refcount_read(&cont->refcount)) { put_task_struct(cont->owner); + _audit_contobj_put(cont->parent); list_del_rcu(&cont->list); kfree_rcu(cont, rcu); } @@ -721,11 +724,11 @@ int audit_log_netns_contid_list(struct net *net, struct audit_context *context) audit_log_lost("out of memory in audit_log_netns_contid_list"); goto out; } - audit_log_format(ab, "record=1 contid=%llu", - cont->obj->id); + audit_log_format(ab, "record=1 contid="); } else { - audit_log_format(ab, ",%llu", cont->obj->id); + audit_log_format(ab, ","); } + audit_log_contid(ab, cont->obj); } audit_log_end(ab); out: @@ -1905,6 +1908,7 @@ static int audit_receive_msg(struct sk_buff *skb, struct nlmsghdr *nlh) case AUDIT_SIGNAL_INFO2: { char *contidstr = NULL; unsigned int contidstrlen = 0; + struct audit_contobj *cont = audit_sig_cid; len = 0; if (audit_sig_sid) { @@ -1914,13 +1918,27 @@ static int audit_receive_msg(struct sk_buff *skb, struct nlmsghdr *nlh) return err; } if (audit_sig_cid) { - contidstr = kmalloc(21, GFP_KERNEL); + contidstr = kmalloc(AUDIT_MESSAGE_TEXT_MAX, GFP_KERNEL); if (!contidstr) { if (audit_sig_sid) security_release_secctx(ctx, len); return -ENOMEM; } - contidstrlen = scnprintf(contidstr, 20, "%llu", audit_sig_cid->id); + rcu_read_lock(); + while (cont) { + if (cont->parent) + contidstrlen += scnprintf(contidstr, + AUDIT_MESSAGE_TEXT_MAX - + contidstrlen, + "%llu,^", cont->id); + else + contidstrlen += scnprintf(contidstr, + AUDIT_MESSAGE_TEXT_MAX - + contidstrlen, + "%llu", cont->id); + cont = cont->parent; + } + rcu_read_unlock(); } sig_data2 = kmalloc(sizeof(*sig_data2) + contidstrlen + len, GFP_KERNEL); if (!sig_data2) { @@ -2607,6 +2625,23 @@ void audit_log_session_info(struct audit_buffer *ab) audit_log_format(ab, "auid=%u ses=%u", auid, sessionid); } +static void audit_log_contid(struct audit_buffer *ab, struct audit_contobj *cont) +{ + if (!cont) { + audit_log_format(ab, "-1"); + return; + } + rcu_read_lock(); + while (cont) { + if (cont->parent) + audit_log_format(ab, "%llu,^", cont->id); + else + audit_log_format(ab, "%llu", cont->id); + cont = cont->parent; + } + rcu_read_unlock(); +} + /* * _audit_log_container_id - report container info * @context: task or local context for record @@ -2626,8 +2661,9 @@ static int _audit_log_container_id(struct audit_context *context, ab = audit_log_start(context, GFP_KERNEL, AUDIT_CONTAINER_ID); if (!ab) return 0; - audit_log_format(ab, "record=%d contid=%llu", - record = ++context->contid_records, contobj->id); + audit_log_format(ab, "record=%d contid=", + record = ++context->contid_records); + audit_log_contid(ab, contobj); audit_log_end(ab); return record; } @@ -2663,7 +2699,18 @@ int audit_log_container_id_ctx(struct audit_context *context) int audit_contid_comparator(struct task_struct *tsk, u32 op, u64 right) { - return audit_comparator64(audit_get_contid(tsk), op, right); + struct audit_contobj *cont = NULL; + int h; + int result = 0; + u64 left = audit_get_contid(tsk); + + h = audit_hash_contid(left); + list_for_each_entry_rcu(cont, &audit_contid_hash[h], list) { + result = audit_comparator64(cont->id, op, right); + if (result) + break; + } + return result; } void audit_log_key(struct audit_buffer *ab, char *key) @@ -3018,6 +3065,7 @@ int audit_set_contid(struct task_struct *tsk, u64 contid) INIT_LIST_HEAD(&newcont->list); newcont->id = contid; newcont->owner = get_task_struct(current); + newcont->parent = _audit_contobj_get_bytask(newcont->owner); refcount_set(&newcont->refcount, 1); list_add_rcu(&newcont->list, &audit_contid_hash[h]); @@ -3046,9 +3094,9 @@ int audit_set_contid(struct task_struct *tsk, u64 contid) if (!ab) return rc; - audit_log_format(ab, - "op=set opid=%d contid=%llu old-contid=%llu", - task_tgid_nr(tsk), contid, oldcont ? oldcont->id : -1); + audit_log_format(ab, "op=set opid=%d contid=%llu old-contid=", + task_tgid_nr(tsk), contid); + audit_log_contid(ab, oldcont); spin_lock_irqsave(&_audit_contobj_list_lock, flags); _audit_contobj_put(oldcont); spin_unlock_irqrestore(&_audit_contobj_list_lock, flags); @@ -3087,8 +3135,9 @@ void audit_log_container_drop(void) ab = audit_log_start(audit_context(), GFP_KERNEL, AUDIT_CONTAINER_OP); if (!ab) goto out; - audit_log_format(ab, "op=drop opid=%d contid=-1 old-contid=%llu", - task_tgid_nr(current), cont->id); + audit_log_format(ab, "op=drop opid=%d contid=-1 old-contid=", + task_tgid_nr(current)); + audit_log_contid(ab, cont); audit_log_end(ab); out: spin_lock_irqsave(&_audit_contobj_list_lock, flags); -- 2.18.4