Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp1400903pxa; Thu, 13 Aug 2020 07:52:39 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwiVoRFkvz+uqitCGGv8u9JHdl55g7+jDAlpqMMjwPiKfWgQNJ/ajmBuPAzjzIbsgj1jcsO X-Received: by 2002:a05:6402:1845:: with SMTP id v5mr5240423edy.66.1597330359570; Thu, 13 Aug 2020 07:52:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1597330359; cv=none; d=google.com; s=arc-20160816; b=UteGHjTfwgu6CnXV1DwHMXtdYG0U9I/Kou5/p8tIlJnpW0AQmlFJAx0xal2kiNMvyw fHvZoHunddyJFQ3k5PLm3SqA3981LMQQedKC1KQepUG2dHoQp/zkF+6sq41SWzt8+0x4 QFZvMJUDyHwot45wCxWkdZkQwUm4JwELzu4i+wQmqDbRnvulZ25zHJuxchS8OGz14oI5 Eq7x1D5dRX4zL0cjn97LOSGjsX95UMiz2vo7r8vZbYIELT3l6gSQhwbPO39mGopjguJq VA+6TlDQ4jPnV75Oj6r2hKXb+hp/nw/L1oY/vWP5TOHkqfx84gRsTVWHIUrtIJ9dqR5a WoFQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:cc:to:from :subject:mime-version:message-id:date:dkim-signature; bh=SEalPAaL/L400EFRhNDiBFOTi7pT/2kjH95anJxxgxI=; b=UE/ACttBH4vt1rbPMpCqp1JVWuRJONUXv+u6ehI6++l2EGpenHJDxl/dKQSd1H1ozD fHR48xSdRuUuzfDbOP2OJCfITaPA+OyzPbyN893tRKAB/Yn//t4+o9xv7aSeWiAHXrj+ eCm/vjXpnwFU559JWzYZhGHJ+VVweGzRS3Jay5CCrVtY+ImAQ3MgxgPMwqyWTgW/9BMA 0Dqui+ya0wig0fCntTFp2PdE4VaKjtsctbpA4YUUDPueHO/+B287K0CQQxtWq8p89Hdd 16/KjSdInTQ0+3QiYRIEvRvQtjTxmRuEvfU2SE73qAZb+/vOmD5SLgaI/9hYb6GA2WrU cVqA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=brQMO6gX; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id e24si3388610eds.212.2020.08.13.07.52.16; Thu, 13 Aug 2020 07:52:39 -0700 (PDT) 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=@google.com header.s=20161025 header.b=brQMO6gX; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726082AbgHMOti (ORCPT + 99 others); Thu, 13 Aug 2020 10:49:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45462 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726419AbgHMOtV (ORCPT ); Thu, 13 Aug 2020 10:49:21 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A335BC061757 for ; Thu, 13 Aug 2020 07:49:21 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id d26so6970747yba.20 for ; Thu, 13 Aug 2020 07:49:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:message-id:mime-version:subject:from:to:cc :content-transfer-encoding; bh=SEalPAaL/L400EFRhNDiBFOTi7pT/2kjH95anJxxgxI=; b=brQMO6gXvTrV2iIjuWTZ0a0xYUjqCYajx8AKbapPdz63kQ6krMyNmy5nRKklvp5bCn w41xOH4gOa0rGhWRgSSPP+zInZZipzm3Xqq+DqPQ7S6/3oroY0FUngVjJ9aQmrLaagsy OHY51yBqks31Yed941hnElUusZA5AB/Bs4X3me0ZHTrw+Zo6PKgEUeZdpdsnmCI96tKx i+4R3An/1umtigsoxZ5Tzx/ZAkI1K5WmIIv/E0C8amv1gcjVbo+CtTn6ZZtyB07QTzEj R49z2EfrHfvj5x3mCe4nJ33AYh0SEeiuvFmbeoKRsSas+zIFsnrflu0c8vsesRZNb54c byWA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:message-id:mime-version:subject:from:to:cc :content-transfer-encoding; bh=SEalPAaL/L400EFRhNDiBFOTi7pT/2kjH95anJxxgxI=; b=iomE1FhGhFA2GEiNRe+mElCIwC4f8HBwbCXdSR9GycYDsypmsCmzwbKcJH4f0Hz2N1 z5dBsiwIaSFFtp62LEfXyHKlt+8UYMsVx+m1+mACooEwiEVMPYvap6h006bWWvCg7+bO AJnS2ux79LOUWhJoOm0dN549M4hF402e06fK/h/A1ImoJM/aeb9MwzgCFQI3QQc/x/oY DphPFPf27UjDMMURwF6aXUgDCfmCU/WR6RIwLaxwmgqFF+pISOU0B6arIWW6vyTBR/ue 5ZBkxCCo5bHjQK3zaFwjFwktbZ7EKfDEe1NcH72Kw7ZHKtf9WygF0dMgI9V79Z5A3zVA oLoA== X-Gm-Message-State: AOAM532AY8OF9lbXiZmEUvQcveFiChZpIJfMnNMdi3IuLgrNn2+Y3Y8d rI4GfSdi2wJXl/7idyyV4Ty5LSw+gg== X-Received: by 2002:a25:8689:: with SMTP id z9mr7576775ybk.226.1597330160576; Thu, 13 Aug 2020 07:49:20 -0700 (PDT) Date: Thu, 13 Aug 2020 16:48:58 +0200 Message-Id: <20200813144914.737306-1-tweek@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.28.0.236.gb10cc79966-goog Subject: [PATCH v2 1/2] selinux: add tracepoint on denials From: "=?UTF-8?q?Thi=C3=A9baud=20Weksteen?=" To: Paul Moore Cc: Nick Kralevich , "=?UTF-8?q?Thi=C3=A9baud=20Weksteen?=" , Joel Fernandes , Peter Enderborg , Stephen Smalley , Eric Paris , Steven Rostedt , Ingo Molnar , Mauro Carvalho Chehab , "David S. Miller" , Rob Herring , Arnd Bergmann , linux-kernel@vger.kernel.org, selinux@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The audit data currently captures which process and which target is responsible for a denial. There is no data on where exactly in the process that call occurred. Debugging can be made easier by being able to reconstruct the unified kernel and userland stack traces [1]. Add a tracepoint on the SELinux denials which can then be used by userland (i.e. perf). Although this patch could manually be added by each OS developer to trouble shoot a denial, adding it to the kernel streamlines the developers workflow. It is possible to use perf for monitoring the event: # perf record -e avc:selinux_audited -g -a ^C # perf report -g [...] 6.40% 6.40% audited=3D800000 tclass=3D4 | __libc_start_main | |--4.60%--__GI___ioctl | entry_SYSCALL_64 | do_syscall_64 | __x64_sys_ioctl | ksys_ioctl | binder_ioctl | binder_set_nice | can_nice | capable | security_capable | cred_has_capability.isra.0 | slow_avc_audit | common_lsm_audit | avc_audit_post_callback | avc_audit_post_callback | It is also possible to use the ftrace interface: # echo 1 > /sys/kernel/debug/tracing/events/avc/selinux_audited/enable # cat /sys/kernel/debug/tracing/trace tracer: nop entries-in-buffer/entries-written: 1/1 #P:8 [...] dmesg-3624 [001] 13072.325358: selinux_denied: audited=3D800000 tclass= =3D4 [1] https://source.android.com/devices/tech/debug/native_stack_dump Signed-off-by: Thi=C3=A9baud Weksteen Suggested-by: Joel Fernandes Reviewed-by: Peter Enderborg --- v2 changes: - update changelog to include usage examples MAINTAINERS | 1 + include/trace/events/avc.h | 37 +++++++++++++++++++++++++++++++++++++ security/selinux/avc.c | 5 +++++ 3 files changed, 43 insertions(+) create mode 100644 include/trace/events/avc.h diff --git a/MAINTAINERS b/MAINTAINERS index c8e8232c65da..0efaea0e144c 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -15426,6 +15426,7 @@ T: git git://git.kernel.org/pub/scm/linux/kernel/gi= t/pcmoore/selinux.git F: Documentation/ABI/obsolete/sysfs-selinux-checkreqprot F: Documentation/ABI/obsolete/sysfs-selinux-disable F: Documentation/admin-guide/LSM/SELinux.rst +F: include/trace/events/avc.h F: include/uapi/linux/selinux_netlink.h F: scripts/selinux/ F: security/selinux/ diff --git a/include/trace/events/avc.h b/include/trace/events/avc.h new file mode 100644 index 000000000000..07c058a9bbcd --- /dev/null +++ b/include/trace/events/avc.h @@ -0,0 +1,37 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * Author: Thi=C3=A9baud Weksteen + */ +#undef TRACE_SYSTEM +#define TRACE_SYSTEM avc + +#if !defined(_TRACE_SELINUX_H) || defined(TRACE_HEADER_MULTI_READ) +#define _TRACE_SELINUX_H + +#include + +TRACE_EVENT(selinux_audited, + + TP_PROTO(struct selinux_audit_data *sad), + + TP_ARGS(sad), + + TP_STRUCT__entry( + __field(unsigned int, tclass) + __field(unsigned int, audited) + ), + + TP_fast_assign( + __entry->tclass =3D sad->tclass; + __entry->audited =3D sad->audited; + ), + + TP_printk("tclass=3D%u audited=3D%x", + __entry->tclass, + __entry->audited) +); + +#endif + +/* This part must be outside protection */ +#include diff --git a/security/selinux/avc.c b/security/selinux/avc.c index d18cb32a242a..b0a0af778b70 100644 --- a/security/selinux/avc.c +++ b/security/selinux/avc.c @@ -31,6 +31,9 @@ #include "avc_ss.h" #include "classmap.h" =20 +#define CREATE_TRACE_POINTS +#include + #define AVC_CACHE_SLOTS 512 #define AVC_DEF_CACHE_THRESHOLD 512 #define AVC_CACHE_RECLAIM 16 @@ -706,6 +709,8 @@ static void avc_audit_post_callback(struct audit_buffer= *ab, void *a) u32 scontext_len; int rc; =20 + trace_selinux_audited(sad); + rc =3D security_sid_to_context(sad->state, sad->ssid, &scontext, &scontext_len); if (rc) --=20 2.28.0.236.gb10cc79966-goog