From patchwork Tue Sep 10 11:55:21 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: KP Singh X-Patchwork-Id: 11139297 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 379EB76 for ; Tue, 10 Sep 2019 11:57:26 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 0BDF821019 for ; Tue, 10 Sep 2019 11:57:26 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="lIcQYf/a" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731955AbfIJL5V (ORCPT ); Tue, 10 Sep 2019 07:57:21 -0400 Received: from mail-wr1-f68.google.com ([209.85.221.68]:40841 "EHLO mail-wr1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732563AbfIJL4i (ORCPT ); Tue, 10 Sep 2019 07:56:38 -0400 Received: by mail-wr1-f68.google.com with SMTP id w13so19614442wru.7 for ; Tue, 10 Sep 2019 04:56:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=oVQ7Tct8tU0DJx5s8h1k7biDl9I5KG0OFqo1Lpd86hc=; b=lIcQYf/amVBtQDfTxCxyvYoyuo7sxkZenk0YeowuFnzDH6o8iHr3Ldayw8ch8W5+Fa dRQYIkfrm5mFDKNUvGJOYCBnZmxaTxgckK4gIyg2wybSvUgyFr4Qyup1A5rGGxMnbdAf TujF0G6HNz/U56ZKm3MxmHZ9PCEw9y/TPRj+g= 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:in-reply-to :references:mime-version:content-transfer-encoding; bh=oVQ7Tct8tU0DJx5s8h1k7biDl9I5KG0OFqo1Lpd86hc=; b=M+rOsINcCiaj4iy8DLmWDeA0qW0Iox++CEm0NctCWZ/uvmpwnMq7dhXdC7Cdp6emDt Q3Gy0iaztzT/SRmfPYJp6lTtbBkW/jeVgZqbkzRo+SF5NcsI1deheCay2HXPPjBnTd0z aaW71Ug6QgDLzCrEqyfoFaH9UP+Eq9HG91/85AuZ2dGwcPjHmmzox2PEZU9MB5Ri2om2 HEz3GxV95mikVjrfGgC2W/nF9UVJMdBuME/F+CbUwbrYD+zAroq+kJB5S4oLVSECjkYQ ol2LH2k1d85FyBrtF9l9fja4C74SREjjlL/V6SsDG0jgMdDGHAxncE/j5993A85KzNQL lV7g== X-Gm-Message-State: APjAAAXFC0i79zv6rOjPahDA4TDG9JDQUQSbyjk/iJo2HAOpeuPvC4Ja Silbx3ia4t/CJF10mcG27IdKMw== X-Google-Smtp-Source: APXvYqxsMV7T7MYZDqZj7+zf1X42YUxNEluEUNSWA8evGhzEfC93r4n95G3TeHM62hd43tXfDYT1jw== X-Received: by 2002:adf:e947:: with SMTP id m7mr26846741wrn.178.1568116596374; Tue, 10 Sep 2019 04:56:36 -0700 (PDT) Received: from kpsingh-kernel.c.hoisthospitality.com (110.8.30.213.rev.vodafone.pt. [213.30.8.110]) by smtp.gmail.com with ESMTPSA id q19sm23732935wra.89.2019.09.10.04.56.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 10 Sep 2019 04:56:35 -0700 (PDT) From: KP Singh To: linux-kernel@vger.kernel.org, bpf@vger.kernel.org, linux-security-module@vger.kernel.org Cc: Alexei Starovoitov , Daniel Borkmann , James Morris , Kees Cook , Thomas Garnier , Michael Halcrow , Paul Turner , Brendan Gregg , Jann Horn , Matthew Garrett , Christian Brauner , =?utf-8?q?Micka=C3=ABl_Sala=C3=BCn?= , Florent Revest , Martin KaFai Lau , Song Liu , Yonghong Song , "Serge E. Hallyn" , Mauro Carvalho Chehab , "David S. Miller" , Greg Kroah-Hartman , Nicolas Ferre , Stanislav Fomichev , Quentin Monnet , Andrey Ignatov , Joe Stringer Subject: [RFC v1 08/14] krsi: Show attached program names in hook read handler. Date: Tue, 10 Sep 2019 13:55:21 +0200 Message-Id: <20190910115527.5235-9-kpsingh@chromium.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190910115527.5235-1-kpsingh@chromium.org> References: <20190910115527.5235-1-kpsingh@chromium.org> MIME-Version: 1.0 Sender: owner-linux-security-module@vger.kernel.org Precedence: bulk List-ID: From: KP Singh For inspectability the system administrator should be able to view the list of active KRSI programs: bash # cat /sys/kernel/security/krsi/process_execution bpf_prog1 Signed-off-by: KP Singh --- security/krsi/krsi_fs.c | 76 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 75 insertions(+), 1 deletion(-) diff --git a/security/krsi/krsi_fs.c b/security/krsi/krsi_fs.c index 3ba18b52ce85..0ebf4fabe935 100644 --- a/security/krsi/krsi_fs.c +++ b/security/krsi/krsi_fs.c @@ -6,6 +6,7 @@ #include #include #include +#include #include #include @@ -16,8 +17,81 @@ extern struct krsi_hook krsi_hooks_list[]; static struct dentry *krsi_dir; +static void *seq_start(struct seq_file *m, loff_t *pos) + __acquires(rcu) +{ + struct krsi_hook *h; + struct dentry *dentry; + struct bpf_prog_array *progs; + struct bpf_prog_array_item *item; + + /* + * rcu_read_lock() must be held before any return statement + * because the stop() will always be called and thus call + * rcu_read_unlock() + */ + rcu_read_lock(); + + dentry = file_dentry(m->file); + h = dentry->d_fsdata; + if (WARN_ON(!h)) + return ERR_PTR(-EFAULT); + + progs = rcu_dereference(h->progs); + if ((*pos) >= bpf_prog_array_length(progs)) + return NULL; + + item = progs->items + *pos; + if (!item->prog) + return NULL; + + return item; +} + +static void *seq_next(struct seq_file *m, void *v, loff_t *pos) +{ + struct bpf_prog_array_item *item = v; + + item++; + ++*pos; + + if (!item->prog) + return NULL; + + return item; +} + +static void seq_stop(struct seq_file *m, void *v) + __releases(rcu) +{ + rcu_read_unlock(); +} + +static int show_prog(struct seq_file *m, void *v) +{ + struct bpf_prog_array_item *item = v; + + seq_printf(m, "%s\n", item->prog->aux->name); + return 0; +} + +static const struct seq_operations seq_ops = { + .show = show_prog, + .start = seq_start, + .next = seq_next, + .stop = seq_stop, +}; + +static int hook_open(struct inode *inode, struct file *file) +{ + return seq_open(file, &seq_ops); +} + static const struct file_operations krsi_hook_ops = { - .llseek = generic_file_llseek, + .open = hook_open, + .read = seq_read, + .llseek = seq_lseek, + .release = seq_release, }; int krsi_fs_initialized;