Skip to contentSkip to conversation

security: add trace event for cap_capable

PATCH Superseded Jordan Rome 5 participants 9 messages Oct 25, 2024 – Oct 28, 2024

Subsystems (3)

v2 of 9 Superseded Latest is v9 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 security: add trace event for cap_capable

In cases where we want a stable way to observe/trace cap_capable (e.g. protection from inlining and API updates) add a tracepoint that passes:
- The credentials used
- The user namespace of the resource being accessed
- The user namespace that has the capability to access the targeted resource
- The capability to check for
- Bitmask of options defined in include/linux/security.h
- The return value of the check

---
 MAINTAINERS                       |  1 +
 include/trace/events/capability.h | 60 +++++++++++++++++++++++++++++++
 security/commoncap.c              | 31 +++++++++++-----
 3 files changed, 84 insertions(+), 8 deletions(-)
 create mode 100644 include/trace/events/capability.h
MAINTAINERS +1 -0
index cc40a9d9b8cd..210e9076c858 100644
@@ -4994,6 +4994,7 @@ M:	Serge Hallyn <[email protected]>
 L:	[email protected]
 S:	Supported
 F:	include/linux/capability.h
+F:	include/trace/events/capability.h
 F:	include/uapi/linux/capability.h
 F:	kernel/capability.c
 F:	security/commoncap.c
include/trace/events/capability.h new file +60 -0

60 lines not on this page · raw message

security/commoncap.c +23 -8
index 162d96b3a676..12c3ddfe0d6e 100644
@@ -27,6 +27,9 @@
 #include <linux/mnt_idmapping.h>
 #include <uapi/linux/lsm.h>
 
+#define CREATE_TRACE_POINTS
+#include <trace/events/capability.h>
+
 /*
  * If a non-root user executes a setuid-root binary in
  * !secure(SECURE_NOROOT) mode, then we raise capabilities.
@@ -52,7 +55,7 @@ static void warn_setuid_and_fcaps_mixed(const char *fname)
 /**
  * cap_capable - Determine whether a task has a particular effective capability
  * @cred: The credentials to use
- * @targ_ns:  The user namespace in which we need the capability
+ * @targ_ns:  The user namespace of the resource being accessed
  * @cap: The capability to check for
  * @opts: Bitmask of options defined in include/linux/security.h
  *
@@ -68,6 +71,7 @@ int cap_capable(const struct cred *cred, struct user_namespace *targ_ns,
 		int cap, unsigned int opts)
 {
 	struct user_namespace *ns = targ_ns;
+	int ret = -EPERM;
 
 	/* See if cred has the capability in the target user namespace
 	 * by examining the target user namespace and all of the target
@@ -75,22 +79,32 @@ int cap_capable(const struct cred *cred, struct user_namespace *targ_ns,
 	 */
 	for (;;) {
 		/* Do we have the necessary capabilities? */
-		if (ns == cred->user_ns)
-			return cap_raised(cred->cap_effective, cap) ? 0 : -EPERM;
+		if (ns == cred->user_ns) {
+			if (cap_raised(cred->cap_effective, cap))
+				ret = 0;
+			else
+				ns = NULL;
+			break;
+		}
 
 		/*
 		 * If we're already at a lower level than we're looking for,
 		 * we're done searching.
 		 */
-		if (ns->level <= cred->user_ns->level)
-			return -EPERM;
+		if (ns->level <= cred->user_ns->level) {
+			ns = NULL;
+			break;
+		}
 
 		/*
 		 * The owner of the user namespace in the parent of the
 		 * user namespace has all caps.
 		 */
-		if ((ns->parent == cred->user_ns) && uid_eq(ns->owner, cred->euid))
-			return 0;
+		if ((ns->parent == cred->user_ns) && uid_eq(ns->owner, cred->euid)) {
+			ns = ns->parent;
+			ret = 0;
+			break;
+		}
 
 		/*
 		 * If you have a capability in a parent user ns, then you have
@@ -99,7 +113,8 @@ int cap_capable(const struct cred *cred, struct user_namespace *targ_ns,
 		ns = ns->parent;
 	}
 
-	/* We never get here */
+	trace_cap_capable(cred, targ_ns, ns, cap, opts, ret);
+	return ret;
 }
 
 /**

--
2.43.5

Acked-by on v2 1/1
#1 lore raw
Quoted 130 lines · Jordan Rome

In cases where we want a stable way to observe/trace cap_capable (e.g. protection from inlining and API updates) add a tracepoint that passes:
- The credentials used
- The user namespace of the resource being accessed
- The user namespace that has the capability to access the targeted resource
- The capability to check for
- Bitmask of options defined in include/linux/security.h
- The return value of the check

---
 MAINTAINERS                       |  1 +
 include/trace/events/capability.h | 60 +++++++++++++++++++++++++++++++
 security/commoncap.c              | 31 +++++++++++-----
 3 files changed, 84 insertions(+), 8 deletions(-)
 create mode 100644 include/trace/events/capability.h
MAINTAINERS +1 -0
index cc40a9d9b8cd..210e9076c858 100644
@@ -4994,6 +4994,7 @@ M:        Serge Hallyn <[email protected]>
 L:     [email protected]
 S:     Supported
 F:     include/linux/capability.h
+F:     include/trace/events/capability.h
 F:     include/uapi/linux/capability.h
 F:     kernel/capability.c
 F:     security/commoncap.c
include/trace/events/capability.h new file +60 -0
new file mode 100644
index 000000000000..e706ce690c38
@@ -0,0 +1,60 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#undef TRACE_SYSTEM
+#define TRACE_SYSTEM capability
+
+#if !defined(_TRACE_CAPABILITY_H) || defined(TRACE_HEADER_MULTI_READ)
+#define _TRACE_CAPABILITY_H
+
+#include <linux/cred.h>
+#include <linux/tracepoint.h>
+#include <linux/user_namespace.h>
+
+/**
+ * cap_capable - called after it's determined if a task has a particular
+ * effective capability
+ *
+ * @cred: The credentials used
+ * @targ_ns: The user namespace of the resource being accessed
+ * @capable_ns: The user namespace in which the credential provides the
+ *              capability to access the targeted resource.
+ *              This will be NULL if ret is not 0.
+ * @cap: The capability to check for
+ * @opts: Bitmask of options defined in include/linux/security.h
+ * @ret: The return value of the check: 0 if it does, -ve if it does not
+ *
+ * Allows to trace calls to cap_capable in commoncap.c
+ */
+TRACE_EVENT(cap_capable,
+
+       TP_PROTO(const struct cred *cred, struct user_namespace *targ_ns,
+               struct user_namespace *capable_ns, int cap, unsigned int opts, int ret),
+
+       TP_ARGS(cred, targ_ns, capable_ns, cap, opts, ret),
+
+       TP_STRUCT__entry(
+               __field(const struct cred *, cred)
+               __field(struct user_namespace *, targ_ns)
+               __field(struct user_namespace *, capable_ns)
+               __field(int, cap)
+               __field(unsigned int, opts)
+               __field(int, ret)
+       ),
+
+       TP_fast_assign(
+               __entry->cred       = cred;
+               __entry->targ_ns    = targ_ns;
+               __entry->capable_ns = capable_ns;
+               __entry->cap        = cap;
+               __entry->opts       = opts;
+               __entry->ret        = ret;
+       ),
+
+       TP_printk("cred %p, targ_ns %p, capable_ns %p, cap %d, opts %u, ret %d",
+               __entry->cred, __entry->targ_ns, __entry->capable_ns, __entry->cap,
+               __entry->opts, __entry->ret)
+);
+
+#endif /* _TRACE_CAPABILITY_H */
+
+/* This part must be outside protection */
+#include <trace/define_trace.h>
security/commoncap.c +5 -1
index 162d96b3a676..12c3ddfe0d6e 100644
@@ -27,6 +27,9 @@
 #include <linux/mnt_idmapping.h>
 #include <uapi/linux/lsm.h>
 
+#define CREATE_TRACE_POINTS
+#include <trace/events/capability.h>
+
 /*
  * If a non-root user executes a setuid-root binary in
  * !secure(SECURE_NOROOT) mode, then we raise capabilities.
@@ -52,7 +55,7 @@ static void warn_setuid_and_fcaps_mixed(const char *fname)
 /**
  * cap_capable - Determine whether a task has a particular effective capability
  * @cred: The credentials to use
- * @targ_ns:  The user namespace in which we need the capability
+ * @targ_ns:  The user namespace of the resource being accessed
  * @cap: The capability to check for
  * @opts: Bitmask of options defined in include/linux/security.h
  *
@@ -68,6 +71,7 @@ int cap_capable(const struct cred *cred, struct user_namespace *targ_ns,
                int cap, unsigned int opts)
 {
        struct user_namespace *ns = targ_ns;
+       int ret = -EPERM;
 
        /* See if cred has the capability in the target user namespace
         * by examining the target user namespace and all of the target

security/commoncap.c int cap_capable(const struct cred *cred, struct user_namespace *targ_ns, · line 86

Quoted 10 lines
security/commoncap.c +5 -2
@@ -75,22 +79,32 @@ int cap_capable(const struct cred *cred, struct user_namespace *targ_ns,
         */
        for (;;) {
                /* Do we have the necessary capabilities? */
-               if (ns == cred->user_ns)
-                       return cap_raised(cred->cap_effective, cap) ? 0 : -EPERM;
+               if (ns == cred->user_ns) {
+                       if (cap_raised(cred->cap_effective, cap))
+                               ret = 0;
+                       else
+                               ns = NULL;

This is a bit unfortunate :( so maybe all we needed was ns = ns->parent for that one use case, and keep the original `ret ? NULL :
ns` inside trace_cap_capable().

But whatever security folks prefer, I'm fine with either.

Quoted 42 lines
security/commoncap.c +13 -6
+                       break;
+               }
 
                /*
                 * If we're already at a lower level than we're looking for,
                 * we're done searching.
                 */
-               if (ns->level <= cred->user_ns->level)
-                       return -EPERM;
+               if (ns->level <= cred->user_ns->level) {
+                       ns = NULL;
+                       break;
+               }
 
                /*
                 * The owner of the user namespace in the parent of the
                 * user namespace has all caps.
                 */
-               if ((ns->parent == cred->user_ns) && uid_eq(ns->owner, cred->euid))
-                       return 0;
+               if ((ns->parent == cred->user_ns) && uid_eq(ns->owner, cred->euid)) {
+                       ns = ns->parent;
+                       ret = 0;
+                       break;
+               }
 
                /*
                 * If you have a capability in a parent user ns, then you have
@@ -99,7 +113,8 @@ int cap_capable(const struct cred *cred, struct user_namespace *targ_ns,
                ns = ns->parent;
        }
 
-       /* We never get here */
+       trace_cap_capable(cred, targ_ns, ns, cap, opts, ret);
+       return ret;
 }
 
 /**
--

2.43.5

maintainer
#2 lore raw
Quoted 137 lines · Andrii Nakryiko
Quoted 136 lines · Jordan Rome

In cases where we want a stable way to observe/trace cap_capable (e.g. protection from inlining and API updates) add a tracepoint that passes:
- The credentials used
- The user namespace of the resource being accessed
- The user namespace that has the capability to access the targeted resource
- The capability to check for
- Bitmask of options defined in include/linux/security.h
- The return value of the check

---
 MAINTAINERS                       |  1 +
 include/trace/events/capability.h | 60 +++++++++++++++++++++++++++++++
 security/commoncap.c              | 31 +++++++++++-----
 3 files changed, 84 insertions(+), 8 deletions(-)
 create mode 100644 include/trace/events/capability.h
MAINTAINERS +1 -0
index cc40a9d9b8cd..210e9076c858 100644
@@ -4994,6 +4994,7 @@ M:        Serge Hallyn <[email protected]>
 L:     [email protected]
 S:     Supported
 F:     include/linux/capability.h
+F:     include/trace/events/capability.h
 F:     include/uapi/linux/capability.h
 F:     kernel/capability.c
 F:     security/commoncap.c
include/trace/events/capability.h new file +60 -0
new file mode 100644
index 000000000000..e706ce690c38
@@ -0,0 +1,60 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#undef TRACE_SYSTEM
+#define TRACE_SYSTEM capability
+
+#if !defined(_TRACE_CAPABILITY_H) || defined(TRACE_HEADER_MULTI_READ)
+#define _TRACE_CAPABILITY_H
+
+#include <linux/cred.h>
+#include <linux/tracepoint.h>
+#include <linux/user_namespace.h>
+
+/**
+ * cap_capable - called after it's determined if a task has a particular
+ * effective capability
+ *
+ * @cred: The credentials used
+ * @targ_ns: The user namespace of the resource being accessed
+ * @capable_ns: The user namespace in which the credential provides the
+ *              capability to access the targeted resource.
+ *              This will be NULL if ret is not 0.
+ * @cap: The capability to check for
+ * @opts: Bitmask of options defined in include/linux/security.h
+ * @ret: The return value of the check: 0 if it does, -ve if it does not
+ *
+ * Allows to trace calls to cap_capable in commoncap.c
+ */
+TRACE_EVENT(cap_capable,
+
+       TP_PROTO(const struct cred *cred, struct user_namespace *targ_ns,
+               struct user_namespace *capable_ns, int cap, unsigned int opts, int ret),
+
+       TP_ARGS(cred, targ_ns, capable_ns, cap, opts, ret),
+
+       TP_STRUCT__entry(
+               __field(const struct cred *, cred)
+               __field(struct user_namespace *, targ_ns)
+               __field(struct user_namespace *, capable_ns)
+               __field(int, cap)
+               __field(unsigned int, opts)
+               __field(int, ret)
+       ),
+
+       TP_fast_assign(
+               __entry->cred       = cred;
+               __entry->targ_ns    = targ_ns;
+               __entry->capable_ns = capable_ns;
+               __entry->cap        = cap;
+               __entry->opts       = opts;
+               __entry->ret        = ret;
+       ),
+
+       TP_printk("cred %p, targ_ns %p, capable_ns %p, cap %d, opts %u, ret %d",
+               __entry->cred, __entry->targ_ns, __entry->capable_ns, __entry->cap,
+               __entry->opts, __entry->ret)
+);
+
+#endif /* _TRACE_CAPABILITY_H */
+
+/* This part must be outside protection */
+#include <trace/define_trace.h>
security/commoncap.c +6 -3
index 162d96b3a676..12c3ddfe0d6e 100644
@@ -27,6 +27,9 @@
 #include <linux/mnt_idmapping.h>
 #include <uapi/linux/lsm.h>
 
+#define CREATE_TRACE_POINTS
+#include <trace/events/capability.h>
+
 /*
  * If a non-root user executes a setuid-root binary in
  * !secure(SECURE_NOROOT) mode, then we raise capabilities.
@@ -52,7 +55,7 @@ static void warn_setuid_and_fcaps_mixed(const char *fname)
 /**
  * cap_capable - Determine whether a task has a particular effective capability
  * @cred: The credentials to use
- * @targ_ns:  The user namespace in which we need the capability
+ * @targ_ns:  The user namespace of the resource being accessed
  * @cap: The capability to check for
  * @opts: Bitmask of options defined in include/linux/security.h
  *
@@ -68,6 +71,7 @@ int cap_capable(const struct cred *cred, struct user_namespace *targ_ns,
                int cap, unsigned int opts)
 {
        struct user_namespace *ns = targ_ns;
+       int ret = -EPERM;
 
        /* See if cred has the capability in the target user namespace
         * by examining the target user namespace and all of the target
@@ -75,22 +79,32 @@ int cap_capable(const struct cred *cred, struct user_namespace *targ_ns,
         */
        for (;;) {
                /* Do we have the necessary capabilities? */
-               if (ns == cred->user_ns)
-                       return cap_raised(cred->cap_effective, cap) ? 0 : -EPERM;
+               if (ns == cred->user_ns) {
Quoted 8 lines

security/commoncap.c int cap_capable(const struct cred *cred, struct user_namespace *targ_ns, · line 86

Quoted 4 lines
security/commoncap.c +4 -0
+                       if (cap_raised(cred->cap_effective, cap))
+                               ret = 0;
+                       else
+                               ns = NULL;

This is a bit unfortunate :( so maybe all we needed was ns = ns->parent for that one use case, and keep the original `ret ? NULL :
ns` inside trace_cap_capable().

Yeah, that would be fine with me. Or maybe just doing

	/* in case of an error, trace should show ns=NULL */
	if (ret)
		ns = NULL;

right above the trace_cap_capable() call would be clearer.

Quoted 46 lines

But whatever security folks prefer, I'm fine with either.

Quoted 42 lines
security/commoncap.c +13 -6
+                       break;
+               }
 
                /*
                 * If we're already at a lower level than we're looking for,
                 * we're done searching.
                 */
-               if (ns->level <= cred->user_ns->level)
-                       return -EPERM;
+               if (ns->level <= cred->user_ns->level) {
+                       ns = NULL;
+                       break;
+               }
 
                /*
                 * The owner of the user namespace in the parent of the
                 * user namespace has all caps.
                 */
-               if ((ns->parent == cred->user_ns) && uid_eq(ns->owner, cred->euid))
-                       return 0;
+               if ((ns->parent == cred->user_ns) && uid_eq(ns->owner, cred->euid)) {
+                       ns = ns->parent;
+                       ret = 0;
+                       break;
+               }
 
                /*
                 * If you have a capability in a parent user ns, then you have
@@ -99,7 +113,8 @@ int cap_capable(const struct cred *cred, struct user_namespace *targ_ns,
                ns = ns->parent;
        }
 
-       /* We never get here */
+       trace_cap_capable(cred, targ_ns, ns, cap, opts, ret);
+       return ret;
 }
 
 /**
--

2.43.5

#3 lore raw
Quoted 148 lines · Serge E. Hallyn
Quoted 137 lines · Andrii Nakryiko
Quoted 136 lines · Jordan Rome

In cases where we want a stable way to observe/trace cap_capable (e.g. protection from inlining and API updates) add a tracepoint that passes:
- The credentials used
- The user namespace of the resource being accessed
- The user namespace that has the capability to access the targeted resource
- The capability to check for
- Bitmask of options defined in include/linux/security.h
- The return value of the check

---
 MAINTAINERS                       |  1 +
 include/trace/events/capability.h | 60 +++++++++++++++++++++++++++++++
 security/commoncap.c              | 31 +++++++++++-----
 3 files changed, 84 insertions(+), 8 deletions(-)
 create mode 100644 include/trace/events/capability.h
MAINTAINERS +1 -0
index cc40a9d9b8cd..210e9076c858 100644
@@ -4994,6 +4994,7 @@ M:        Serge Hallyn <[email protected]>
 L:     [email protected]
 S:     Supported
 F:     include/linux/capability.h
+F:     include/trace/events/capability.h
 F:     include/uapi/linux/capability.h
 F:     kernel/capability.c
 F:     security/commoncap.c
include/trace/events/capability.h new file +60 -0
new file mode 100644
index 000000000000..e706ce690c38
@@ -0,0 +1,60 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#undef TRACE_SYSTEM
+#define TRACE_SYSTEM capability
+
+#if !defined(_TRACE_CAPABILITY_H) || defined(TRACE_HEADER_MULTI_READ)
+#define _TRACE_CAPABILITY_H
+
+#include <linux/cred.h>
+#include <linux/tracepoint.h>
+#include <linux/user_namespace.h>
+
+/**
+ * cap_capable - called after it's determined if a task has a particular
+ * effective capability
+ *
+ * @cred: The credentials used
+ * @targ_ns: The user namespace of the resource being accessed
+ * @capable_ns: The user namespace in which the credential provides the
+ *              capability to access the targeted resource.
+ *              This will be NULL if ret is not 0.
+ * @cap: The capability to check for
+ * @opts: Bitmask of options defined in include/linux/security.h
+ * @ret: The return value of the check: 0 if it does, -ve if it does not
+ *
+ * Allows to trace calls to cap_capable in commoncap.c
+ */
+TRACE_EVENT(cap_capable,
+
+       TP_PROTO(const struct cred *cred, struct user_namespace *targ_ns,
+               struct user_namespace *capable_ns, int cap, unsigned int opts, int ret),
+
+       TP_ARGS(cred, targ_ns, capable_ns, cap, opts, ret),
+
+       TP_STRUCT__entry(
+               __field(const struct cred *, cred)
+               __field(struct user_namespace *, targ_ns)
+               __field(struct user_namespace *, capable_ns)
+               __field(int, cap)
+               __field(unsigned int, opts)
+               __field(int, ret)
+       ),
+
+       TP_fast_assign(
+               __entry->cred       = cred;
+               __entry->targ_ns    = targ_ns;
+               __entry->capable_ns = capable_ns;
+               __entry->cap        = cap;
+               __entry->opts       = opts;
+               __entry->ret        = ret;
+       ),
+
+       TP_printk("cred %p, targ_ns %p, capable_ns %p, cap %d, opts %u, ret %d",
+               __entry->cred, __entry->targ_ns, __entry->capable_ns, __entry->cap,
+               __entry->opts, __entry->ret)
+);
+
+#endif /* _TRACE_CAPABILITY_H */
+
+/* This part must be outside protection */
+#include <trace/define_trace.h>
security/commoncap.c +6 -3
index 162d96b3a676..12c3ddfe0d6e 100644
@@ -27,6 +27,9 @@
 #include <linux/mnt_idmapping.h>
 #include <uapi/linux/lsm.h>
 
+#define CREATE_TRACE_POINTS
+#include <trace/events/capability.h>
+
 /*
  * If a non-root user executes a setuid-root binary in
  * !secure(SECURE_NOROOT) mode, then we raise capabilities.
@@ -52,7 +55,7 @@ static void warn_setuid_and_fcaps_mixed(const char *fname)
 /**
  * cap_capable - Determine whether a task has a particular effective capability
  * @cred: The credentials to use
- * @targ_ns:  The user namespace in which we need the capability
+ * @targ_ns:  The user namespace of the resource being accessed
  * @cap: The capability to check for
  * @opts: Bitmask of options defined in include/linux/security.h
  *
@@ -68,6 +71,7 @@ int cap_capable(const struct cred *cred, struct user_namespace *targ_ns,
                int cap, unsigned int opts)
 {
        struct user_namespace *ns = targ_ns;
+       int ret = -EPERM;
 
        /* See if cred has the capability in the target user namespace
         * by examining the target user namespace and all of the target
@@ -75,22 +79,32 @@ int cap_capable(const struct cred *cred, struct user_namespace *targ_ns,
         */
        for (;;) {
                /* Do we have the necessary capabilities? */
-               if (ns == cred->user_ns)
-                       return cap_raised(cred->cap_effective, cap) ? 0 : -EPERM;
+               if (ns == cred->user_ns) {
Quoted 8 lines

security/commoncap.c int cap_capable(const struct cred *cred, struct user_namespace *targ_ns, · line 86

Quoted 4 lines
security/commoncap.c +4 -0
+                       if (cap_raised(cred->cap_effective, cap))
+                               ret = 0;
+                       else
+                               ns = NULL;

This is a bit unfortunate :( so maybe all we needed was ns = ns->parent for that one use case, and keep the original `ret ? NULL :
ns` inside trace_cap_capable().

Quoted 7 lines

Yeah, that would be fine with me. Or maybe just doing

        /* in case of an error, trace should show ns=NULL */
        if (ret)
                ns = NULL;

right above the trace_cap_capable() call would be clearer.

I feel like having less trace specific logic in this function would be a good thing,
so I'm for Andrii's suggestion of doing the ret check there but also fine to do what security folks prefer :)

Quoted 47 lines
Quoted 47 lines

But whatever security folks prefer, I'm fine with either.

Quoted 42 lines
security/commoncap.c +13 -6
+                       break;
+               }
 
                /*
                 * If we're already at a lower level than we're looking for,
                 * we're done searching.
                 */
-               if (ns->level <= cred->user_ns->level)
-                       return -EPERM;
+               if (ns->level <= cred->user_ns->level) {
+                       ns = NULL;
+                       break;
+               }
 
                /*
                 * The owner of the user namespace in the parent of the
                 * user namespace has all caps.
                 */
-               if ((ns->parent == cred->user_ns) && uid_eq(ns->owner, cred->euid))
-                       return 0;
+               if ((ns->parent == cred->user_ns) && uid_eq(ns->owner, cred->euid)) {
+                       ns = ns->parent;
+                       ret = 0;
+                       break;
+               }
 
                /*
                 * If you have a capability in a parent user ns, then you have
@@ -99,7 +113,8 @@ int cap_capable(const struct cred *cred, struct user_namespace *targ_ns,
                ns = ns->parent;
        }
 
-       /* We never get here */
+       trace_cap_capable(cred, targ_ns, ns, cap, opts, ret);
+       return ret;
 }
 
 /**
--

2.43.5

maintainer Reviewed-by
#4 lore raw
Quoted 155 lines · Jordan Rome
Quoted 154 lines · Serge E. Hallyn
Quoted 137 lines · Andrii Nakryiko
Quoted 136 lines · Jordan Rome

In cases where we want a stable way to observe/trace cap_capable (e.g. protection from inlining and API updates) add a tracepoint that passes:
- The credentials used
- The user namespace of the resource being accessed
- The user namespace that has the capability to access the targeted resource
- The capability to check for
- Bitmask of options defined in include/linux/security.h
- The return value of the check

---
 MAINTAINERS                       |  1 +
 include/trace/events/capability.h | 60 +++++++++++++++++++++++++++++++
 security/commoncap.c              | 31 +++++++++++-----
 3 files changed, 84 insertions(+), 8 deletions(-)
 create mode 100644 include/trace/events/capability.h
MAINTAINERS +1 -0
index cc40a9d9b8cd..210e9076c858 100644
@@ -4994,6 +4994,7 @@ M:        Serge Hallyn <[email protected]>
 L:     [email protected]
 S:     Supported
 F:     include/linux/capability.h
+F:     include/trace/events/capability.h
 F:     include/uapi/linux/capability.h
 F:     kernel/capability.c
 F:     security/commoncap.c
include/trace/events/capability.h new file +60 -0
new file mode 100644
index 000000000000..e706ce690c38
@@ -0,0 +1,60 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#undef TRACE_SYSTEM
+#define TRACE_SYSTEM capability
+
+#if !defined(_TRACE_CAPABILITY_H) || defined(TRACE_HEADER_MULTI_READ)
+#define _TRACE_CAPABILITY_H
+
+#include <linux/cred.h>
+#include <linux/tracepoint.h>
+#include <linux/user_namespace.h>
+
+/**
+ * cap_capable - called after it's determined if a task has a particular
+ * effective capability
+ *
+ * @cred: The credentials used
+ * @targ_ns: The user namespace of the resource being accessed
+ * @capable_ns: The user namespace in which the credential provides the
+ *              capability to access the targeted resource.
+ *              This will be NULL if ret is not 0.
+ * @cap: The capability to check for
+ * @opts: Bitmask of options defined in include/linux/security.h
+ * @ret: The return value of the check: 0 if it does, -ve if it does not
+ *
+ * Allows to trace calls to cap_capable in commoncap.c
+ */
+TRACE_EVENT(cap_capable,
+
+       TP_PROTO(const struct cred *cred, struct user_namespace *targ_ns,
+               struct user_namespace *capable_ns, int cap, unsigned int opts, int ret),
+
+       TP_ARGS(cred, targ_ns, capable_ns, cap, opts, ret),
+
+       TP_STRUCT__entry(
+               __field(const struct cred *, cred)
+               __field(struct user_namespace *, targ_ns)
+               __field(struct user_namespace *, capable_ns)
+               __field(int, cap)
+               __field(unsigned int, opts)
+               __field(int, ret)
+       ),
+
+       TP_fast_assign(
+               __entry->cred       = cred;
+               __entry->targ_ns    = targ_ns;
+               __entry->capable_ns = capable_ns;
+               __entry->cap        = cap;
+               __entry->opts       = opts;
+               __entry->ret        = ret;
+       ),
+
+       TP_printk("cred %p, targ_ns %p, capable_ns %p, cap %d, opts %u, ret %d",
+               __entry->cred, __entry->targ_ns, __entry->capable_ns, __entry->cap,
+               __entry->opts, __entry->ret)
+);
+
+#endif /* _TRACE_CAPABILITY_H */
+
+/* This part must be outside protection */
+#include <trace/define_trace.h>
security/commoncap.c +6 -3
index 162d96b3a676..12c3ddfe0d6e 100644
@@ -27,6 +27,9 @@
 #include <linux/mnt_idmapping.h>
 #include <uapi/linux/lsm.h>
 
+#define CREATE_TRACE_POINTS
+#include <trace/events/capability.h>
+
 /*
  * If a non-root user executes a setuid-root binary in
  * !secure(SECURE_NOROOT) mode, then we raise capabilities.
@@ -52,7 +55,7 @@ static void warn_setuid_and_fcaps_mixed(const char *fname)
 /**
  * cap_capable - Determine whether a task has a particular effective capability
  * @cred: The credentials to use
- * @targ_ns:  The user namespace in which we need the capability
+ * @targ_ns:  The user namespace of the resource being accessed
  * @cap: The capability to check for
  * @opts: Bitmask of options defined in include/linux/security.h
  *
@@ -68,6 +71,7 @@ int cap_capable(const struct cred *cred, struct user_namespace *targ_ns,
                int cap, unsigned int opts)
 {
        struct user_namespace *ns = targ_ns;
+       int ret = -EPERM;
 
        /* See if cred has the capability in the target user namespace
         * by examining the target user namespace and all of the target
@@ -75,22 +79,32 @@ int cap_capable(const struct cred *cred, struct user_namespace *targ_ns,
         */
        for (;;) {
                /* Do we have the necessary capabilities? */
-               if (ns == cred->user_ns)
-                       return cap_raised(cred->cap_effective, cap) ? 0 : -EPERM;
+               if (ns == cred->user_ns) {
Quoted 8 lines

security/commoncap.c int cap_capable(const struct cred *cred, struct user_namespace *targ_ns, · line 86

Quoted 4 lines
security/commoncap.c +4 -0
+                       if (cap_raised(cred->cap_effective, cap))
+                               ret = 0;
+                       else
+                               ns = NULL;

This is a bit unfortunate :( so maybe all we needed was ns = ns->parent for that one use case, and keep the original `ret ? NULL :
ns` inside trace_cap_capable().

Yeah, that would be fine with me. Or maybe just doing

        /* in case of an error, trace should show ns=NULL */
        if (ret)
                ns = NULL;
Quoted 6 lines
Quoted 1 line

right above the trace_cap_capable() call would be clearer.

I feel like having less trace specific logic in this function would be a good thing,
so I'm for Andrii's suggestion of doing the ret check there but also fine to do what security folks prefer :)

I think a comment is needed to remind us (me) in 2 years why the seting of ns to NULL is there. But the comment of trace_cap_capable() probably suffices, so sure, go with Andrii's suggestion. And then

for the capability code.

thanks,
-serge

#5 lore raw
Quoted 168 lines · Serge E. Hallyn
Quoted 155 lines · Jordan Rome
Quoted 154 lines · Serge E. Hallyn
Quoted 137 lines · Andrii Nakryiko
Quoted 136 lines · Jordan Rome

In cases where we want a stable way to observe/trace cap_capable (e.g. protection from inlining and API updates) add a tracepoint that passes:
- The credentials used
- The user namespace of the resource being accessed
- The user namespace that has the capability to access the targeted resource
- The capability to check for
- Bitmask of options defined in include/linux/security.h
- The return value of the check

---
 MAINTAINERS                       |  1 +
 include/trace/events/capability.h | 60 +++++++++++++++++++++++++++++++
 security/commoncap.c              | 31 +++++++++++-----
 3 files changed, 84 insertions(+), 8 deletions(-)
 create mode 100644 include/trace/events/capability.h
MAINTAINERS +1 -0
index cc40a9d9b8cd..210e9076c858 100644
@@ -4994,6 +4994,7 @@ M:        Serge Hallyn <[email protected]>
 L:     [email protected]
 S:     Supported
 F:     include/linux/capability.h
+F:     include/trace/events/capability.h
 F:     include/uapi/linux/capability.h
 F:     kernel/capability.c
 F:     security/commoncap.c
include/trace/events/capability.h new file +60 -0
new file mode 100644
index 000000000000..e706ce690c38
@@ -0,0 +1,60 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#undef TRACE_SYSTEM
+#define TRACE_SYSTEM capability
+
+#if !defined(_TRACE_CAPABILITY_H) || defined(TRACE_HEADER_MULTI_READ)
+#define _TRACE_CAPABILITY_H
+
+#include <linux/cred.h>
+#include <linux/tracepoint.h>
+#include <linux/user_namespace.h>
+
+/**
+ * cap_capable - called after it's determined if a task has a particular
+ * effective capability
+ *
+ * @cred: The credentials used
+ * @targ_ns: The user namespace of the resource being accessed
+ * @capable_ns: The user namespace in which the credential provides the
+ *              capability to access the targeted resource.
+ *              This will be NULL if ret is not 0.
+ * @cap: The capability to check for
+ * @opts: Bitmask of options defined in include/linux/security.h
+ * @ret: The return value of the check: 0 if it does, -ve if it does not
+ *
+ * Allows to trace calls to cap_capable in commoncap.c
+ */
+TRACE_EVENT(cap_capable,
+
+       TP_PROTO(const struct cred *cred, struct user_namespace *targ_ns,
+               struct user_namespace *capable_ns, int cap, unsigned int opts, int ret),
+
+       TP_ARGS(cred, targ_ns, capable_ns, cap, opts, ret),
+
+       TP_STRUCT__entry(
+               __field(const struct cred *, cred)
+               __field(struct user_namespace *, targ_ns)
+               __field(struct user_namespace *, capable_ns)
+               __field(int, cap)
+               __field(unsigned int, opts)
+               __field(int, ret)
+       ),
+
+       TP_fast_assign(
+               __entry->cred       = cred;
+               __entry->targ_ns    = targ_ns;
+               __entry->capable_ns = capable_ns;
+               __entry->cap        = cap;
+               __entry->opts       = opts;
+               __entry->ret        = ret;
+       ),
+
+       TP_printk("cred %p, targ_ns %p, capable_ns %p, cap %d, opts %u, ret %d",
+               __entry->cred, __entry->targ_ns, __entry->capable_ns, __entry->cap,
+               __entry->opts, __entry->ret)
+);
+
+#endif /* _TRACE_CAPABILITY_H */
+
+/* This part must be outside protection */
+#include <trace/define_trace.h>
security/commoncap.c +6 -3
index 162d96b3a676..12c3ddfe0d6e 100644
@@ -27,6 +27,9 @@
 #include <linux/mnt_idmapping.h>
 #include <uapi/linux/lsm.h>
 
+#define CREATE_TRACE_POINTS
+#include <trace/events/capability.h>
+
 /*
  * If a non-root user executes a setuid-root binary in
  * !secure(SECURE_NOROOT) mode, then we raise capabilities.
@@ -52,7 +55,7 @@ static void warn_setuid_and_fcaps_mixed(const char *fname)
 /**
  * cap_capable - Determine whether a task has a particular effective capability
  * @cred: The credentials to use
- * @targ_ns:  The user namespace in which we need the capability
+ * @targ_ns:  The user namespace of the resource being accessed
  * @cap: The capability to check for
  * @opts: Bitmask of options defined in include/linux/security.h
  *
@@ -68,6 +71,7 @@ int cap_capable(const struct cred *cred, struct user_namespace *targ_ns,
                int cap, unsigned int opts)
 {
        struct user_namespace *ns = targ_ns;
+       int ret = -EPERM;
 
        /* See if cred has the capability in the target user namespace
         * by examining the target user namespace and all of the target
@@ -75,22 +79,32 @@ int cap_capable(const struct cred *cred, struct user_namespace *targ_ns,
         */
        for (;;) {
                /* Do we have the necessary capabilities? */
-               if (ns == cred->user_ns)
-                       return cap_raised(cred->cap_effective, cap) ? 0 : -EPERM;
+               if (ns == cred->user_ns) {
Quoted 8 lines

security/commoncap.c int cap_capable(const struct cred *cred, struct user_namespace *targ_ns, · line 86

Quoted 4 lines
security/commoncap.c +4 -0
+                       if (cap_raised(cred->cap_effective, cap))
+                               ret = 0;
+                       else
+                               ns = NULL;

This is a bit unfortunate :( so maybe all we needed was ns = ns->parent for that one use case, and keep the original `ret ? NULL :
ns` inside trace_cap_capable().

Yeah, that would be fine with me. Or maybe just doing

        /* in case of an error, trace should show ns=NULL */
        if (ret)
                ns = NULL;
Quoted 6 lines
Quoted 1 line

right above the trace_cap_capable() call would be clearer.

I feel like having less trace specific logic in this function would be a good thing,
so I'm for Andrii's suggestion of doing the ret check there but also fine to do what security folks prefer :)

I think a comment is needed to remind us (me) in 2 years why the seting of ns to NULL is there. But the comment of trace_cap_capable() probably suffices, so sure, go with Andrii's suggestion. And then

Quoted 6 lines

for the capability code.

thanks,
-serge

I think we're suggesting to not set ns = NULL here and instead check the ret value in the trace code e.g. __entry->capable_ns = ret ? NULL : capable_ns;

I think the only trace-specific thing, which I can add a comment for, is this part ns = ns->parent; after we already set the ret to 0.

maintainer
#6 lore raw
Quoted 171 lines · Jordan Rome
Quoted 170 lines · Serge E. Hallyn
Quoted 155 lines · Jordan Rome
Quoted 154 lines · Serge E. Hallyn
Quoted 137 lines · Andrii Nakryiko
Quoted 136 lines · Jordan Rome

In cases where we want a stable way to observe/trace cap_capable (e.g. protection from inlining and API updates) add a tracepoint that passes:
- The credentials used
- The user namespace of the resource being accessed
- The user namespace that has the capability to access the targeted resource
- The capability to check for
- Bitmask of options defined in include/linux/security.h
- The return value of the check

---
 MAINTAINERS                       |  1 +
 include/trace/events/capability.h | 60 +++++++++++++++++++++++++++++++
 security/commoncap.c              | 31 +++++++++++-----
 3 files changed, 84 insertions(+), 8 deletions(-)
 create mode 100644 include/trace/events/capability.h
MAINTAINERS +1 -0
index cc40a9d9b8cd..210e9076c858 100644
@@ -4994,6 +4994,7 @@ M:        Serge Hallyn <[email protected]>
 L:     [email protected]
 S:     Supported
 F:     include/linux/capability.h
+F:     include/trace/events/capability.h
 F:     include/uapi/linux/capability.h
 F:     kernel/capability.c
 F:     security/commoncap.c
include/trace/events/capability.h new file +60 -0
new file mode 100644
index 000000000000..e706ce690c38
@@ -0,0 +1,60 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#undef TRACE_SYSTEM
+#define TRACE_SYSTEM capability
+
+#if !defined(_TRACE_CAPABILITY_H) || defined(TRACE_HEADER_MULTI_READ)
+#define _TRACE_CAPABILITY_H
+
+#include <linux/cred.h>
+#include <linux/tracepoint.h>
+#include <linux/user_namespace.h>
+
+/**
+ * cap_capable - called after it's determined if a task has a particular
+ * effective capability
+ *
+ * @cred: The credentials used
+ * @targ_ns: The user namespace of the resource being accessed
+ * @capable_ns: The user namespace in which the credential provides the
+ *              capability to access the targeted resource.
+ *              This will be NULL if ret is not 0.
+ * @cap: The capability to check for
+ * @opts: Bitmask of options defined in include/linux/security.h
+ * @ret: The return value of the check: 0 if it does, -ve if it does not
+ *
+ * Allows to trace calls to cap_capable in commoncap.c
+ */
+TRACE_EVENT(cap_capable,
+
+       TP_PROTO(const struct cred *cred, struct user_namespace *targ_ns,
+               struct user_namespace *capable_ns, int cap, unsigned int opts, int ret),
+
+       TP_ARGS(cred, targ_ns, capable_ns, cap, opts, ret),
+
+       TP_STRUCT__entry(
+               __field(const struct cred *, cred)
+               __field(struct user_namespace *, targ_ns)
+               __field(struct user_namespace *, capable_ns)
+               __field(int, cap)
+               __field(unsigned int, opts)
+               __field(int, ret)
+       ),
+
+       TP_fast_assign(
+               __entry->cred       = cred;
+               __entry->targ_ns    = targ_ns;
+               __entry->capable_ns = capable_ns;
+               __entry->cap        = cap;
+               __entry->opts       = opts;
+               __entry->ret        = ret;
+       ),
+
+       TP_printk("cred %p, targ_ns %p, capable_ns %p, cap %d, opts %u, ret %d",
+               __entry->cred, __entry->targ_ns, __entry->capable_ns, __entry->cap,
+               __entry->opts, __entry->ret)
+);
+
+#endif /* _TRACE_CAPABILITY_H */
+
+/* This part must be outside protection */
+#include <trace/define_trace.h>
security/commoncap.c +6 -3
index 162d96b3a676..12c3ddfe0d6e 100644
@@ -27,6 +27,9 @@
 #include <linux/mnt_idmapping.h>
 #include <uapi/linux/lsm.h>
 
+#define CREATE_TRACE_POINTS
+#include <trace/events/capability.h>
+
 /*
  * If a non-root user executes a setuid-root binary in
  * !secure(SECURE_NOROOT) mode, then we raise capabilities.
@@ -52,7 +55,7 @@ static void warn_setuid_and_fcaps_mixed(const char *fname)
 /**
  * cap_capable - Determine whether a task has a particular effective capability
  * @cred: The credentials to use
- * @targ_ns:  The user namespace in which we need the capability
+ * @targ_ns:  The user namespace of the resource being accessed
  * @cap: The capability to check for
  * @opts: Bitmask of options defined in include/linux/security.h
  *
@@ -68,6 +71,7 @@ int cap_capable(const struct cred *cred, struct user_namespace *targ_ns,
                int cap, unsigned int opts)
 {
        struct user_namespace *ns = targ_ns;
+       int ret = -EPERM;
 
        /* See if cred has the capability in the target user namespace
         * by examining the target user namespace and all of the target
@@ -75,22 +79,32 @@ int cap_capable(const struct cred *cred, struct user_namespace *targ_ns,
         */
        for (;;) {
                /* Do we have the necessary capabilities? */
-               if (ns == cred->user_ns)
-                       return cap_raised(cred->cap_effective, cap) ? 0 : -EPERM;
+               if (ns == cred->user_ns) {
Quoted 8 lines

security/commoncap.c int cap_capable(const struct cred *cred, struct user_namespace *targ_ns, · line 86

Quoted 4 lines
security/commoncap.c +4 -0
+                       if (cap_raised(cred->cap_effective, cap))
+                               ret = 0;
+                       else
+                               ns = NULL;

This is a bit unfortunate :( so maybe all we needed was ns = ns->parent for that one use case, and keep the original `ret ? NULL :
ns` inside trace_cap_capable().

Yeah, that would be fine with me. Or maybe just doing

        /* in case of an error, trace should show ns=NULL */
        if (ret)
                ns = NULL;
Quoted 6 lines
Quoted 1 line

right above the trace_cap_capable() call would be clearer.

I feel like having less trace specific logic in this function would be a good thing,
so I'm for Andrii's suggestion of doing the ret check there but also fine to do what security folks prefer :)

I think a comment is needed to remind us (me) in 2 years why the seting of ns to NULL is there. But the comment of trace_cap_capable() probably suffices, so sure, go with Andrii's suggestion. And then

Quoted 8 lines
Quoted 4 lines

for the capability code.

thanks,
-serge

I think we're suggesting to not set ns = NULL here and instead check the ret value in the trace code e.g. __entry->capable_ns = ret ? NULL : capable_ns;

Perfect. Was originally going to suggest this, but then thought well the rest of the ns logic is purely capability not tracing related. But since the comment is in trace_cap_capable(), putting the assignment there makes sense.

Quoted 2 lines

I think the only trace-specific thing, which I can add a comment for, is this part ns = ns->parent; after we already set the ret to 0.

#7 lore raw
Quoted 178 lines · Serge E. Hallyn
Quoted 176 lines · Jordan Rome
Quoted 174 lines · Serge E. Hallyn
Quoted 155 lines · Jordan Rome
Quoted 154 lines · Serge E. Hallyn
Quoted 137 lines · Andrii Nakryiko
Quoted 136 lines · Jordan Rome

In cases where we want a stable way to observe/trace cap_capable (e.g. protection from inlining and API updates) add a tracepoint that passes:
- The credentials used
- The user namespace of the resource being accessed
- The user namespace that has the capability to access the targeted resource
- The capability to check for
- Bitmask of options defined in include/linux/security.h
- The return value of the check

---
 MAINTAINERS                       |  1 +
 include/trace/events/capability.h | 60 +++++++++++++++++++++++++++++++
 security/commoncap.c              | 31 +++++++++++-----
 3 files changed, 84 insertions(+), 8 deletions(-)
 create mode 100644 include/trace/events/capability.h
MAINTAINERS +1 -0
index cc40a9d9b8cd..210e9076c858 100644
@@ -4994,6 +4994,7 @@ M:        Serge Hallyn <[email protected]>
 L:     [email protected]
 S:     Supported
 F:     include/linux/capability.h
+F:     include/trace/events/capability.h
 F:     include/uapi/linux/capability.h
 F:     kernel/capability.c
 F:     security/commoncap.c
include/trace/events/capability.h new file +60 -0
new file mode 100644
index 000000000000..e706ce690c38
@@ -0,0 +1,60 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#undef TRACE_SYSTEM
+#define TRACE_SYSTEM capability
+
+#if !defined(_TRACE_CAPABILITY_H) || defined(TRACE_HEADER_MULTI_READ)
+#define _TRACE_CAPABILITY_H
+
+#include <linux/cred.h>
+#include <linux/tracepoint.h>
+#include <linux/user_namespace.h>
+
+/**
+ * cap_capable - called after it's determined if a task has a particular
+ * effective capability
+ *
+ * @cred: The credentials used
+ * @targ_ns: The user namespace of the resource being accessed
+ * @capable_ns: The user namespace in which the credential provides the
+ *              capability to access the targeted resource.
+ *              This will be NULL if ret is not 0.
+ * @cap: The capability to check for
+ * @opts: Bitmask of options defined in include/linux/security.h
+ * @ret: The return value of the check: 0 if it does, -ve if it does not
+ *
+ * Allows to trace calls to cap_capable in commoncap.c
+ */
+TRACE_EVENT(cap_capable,
+
+       TP_PROTO(const struct cred *cred, struct user_namespace *targ_ns,
+               struct user_namespace *capable_ns, int cap, unsigned int opts, int ret),
+
+       TP_ARGS(cred, targ_ns, capable_ns, cap, opts, ret),
+
+       TP_STRUCT__entry(
+               __field(const struct cred *, cred)
+               __field(struct user_namespace *, targ_ns)
+               __field(struct user_namespace *, capable_ns)
+               __field(int, cap)
+               __field(unsigned int, opts)
+               __field(int, ret)
+       ),
+
+       TP_fast_assign(
+               __entry->cred       = cred;
+               __entry->targ_ns    = targ_ns;
+               __entry->capable_ns = capable_ns;
+               __entry->cap        = cap;
+               __entry->opts       = opts;
+               __entry->ret        = ret;
+       ),
+
+       TP_printk("cred %p, targ_ns %p, capable_ns %p, cap %d, opts %u, ret %d",
+               __entry->cred, __entry->targ_ns, __entry->capable_ns, __entry->cap,
+               __entry->opts, __entry->ret)
+);
+
+#endif /* _TRACE_CAPABILITY_H */
+
+/* This part must be outside protection */
+#include <trace/define_trace.h>
security/commoncap.c +6 -3
index 162d96b3a676..12c3ddfe0d6e 100644
@@ -27,6 +27,9 @@
 #include <linux/mnt_idmapping.h>
 #include <uapi/linux/lsm.h>
 
+#define CREATE_TRACE_POINTS
+#include <trace/events/capability.h>
+
 /*
  * If a non-root user executes a setuid-root binary in
  * !secure(SECURE_NOROOT) mode, then we raise capabilities.
@@ -52,7 +55,7 @@ static void warn_setuid_and_fcaps_mixed(const char *fname)
 /**
  * cap_capable - Determine whether a task has a particular effective capability
  * @cred: The credentials to use
- * @targ_ns:  The user namespace in which we need the capability
+ * @targ_ns:  The user namespace of the resource being accessed
  * @cap: The capability to check for
  * @opts: Bitmask of options defined in include/linux/security.h
  *
@@ -68,6 +71,7 @@ int cap_capable(const struct cred *cred, struct user_namespace *targ_ns,
                int cap, unsigned int opts)
 {
        struct user_namespace *ns = targ_ns;
+       int ret = -EPERM;
 
        /* See if cred has the capability in the target user namespace
         * by examining the target user namespace and all of the target
@@ -75,22 +79,32 @@ int cap_capable(const struct cred *cred, struct user_namespace *targ_ns,
         */
        for (;;) {
                /* Do we have the necessary capabilities? */
-               if (ns == cred->user_ns)
-                       return cap_raised(cred->cap_effective, cap) ? 0 : -EPERM;
+               if (ns == cred->user_ns) {
Quoted 8 lines

security/commoncap.c int cap_capable(const struct cred *cred, struct user_namespace *targ_ns, · line 86

Quoted 4 lines
security/commoncap.c +4 -0
+                       if (cap_raised(cred->cap_effective, cap))
+                               ret = 0;
+                       else
+                               ns = NULL;

This is a bit unfortunate :( so maybe all we needed was ns = ns->parent for that one use case, and keep the original `ret ? NULL :
ns` inside trace_cap_capable().

Yeah, that would be fine with me. Or maybe just doing

        /* in case of an error, trace should show ns=NULL */
        if (ret)
                ns = NULL;
Quoted 6 lines
Quoted 1 line

right above the trace_cap_capable() call would be clearer.

I feel like having less trace specific logic in this function would be a good thing,
so I'm for Andrii's suggestion of doing the ret check there but also fine to do what security folks prefer :)

I think a comment is needed to remind us (me) in 2 years why the seting of ns to NULL is there. But the comment of trace_cap_capable() probably suffices, so sure, go with Andrii's suggestion. And then

for the capability code.

thanks,
-serge

Quoted 9 lines
Quoted 3 lines

I think we're suggesting to not set ns = NULL here and instead check the ret value in the trace code e.g. __entry->capable_ns = ret ? NULL : capable_ns;

Perfect. Was originally going to suggest this, but then thought well the rest of the ns logic is purely capability not tracing related. But since the comment is in trace_cap_capable(), putting the assignment there makes sense.

Actually, I had another idea. What about just having a separate variable in the cap_capable function for capable_ns that only gets set if ret is 0. Then we're not changing the ns variable at all for the purposes of the trace function.

Quoted 2 lines
Quoted 2 lines

I think the only trace-specific thing, which I can add a comment for, is this part ns = ns->parent; after we already set the ret to 0.

#8 lore raw
Quoted 189 lines · Jordan Rome
Quoted 178 lines · Serge E. Hallyn
Quoted 176 lines · Jordan Rome
Quoted 174 lines · Serge E. Hallyn
Quoted 155 lines · Jordan Rome
Quoted 154 lines · Serge E. Hallyn
Quoted 137 lines · Andrii Nakryiko
Quoted 136 lines · Jordan Rome

In cases where we want a stable way to observe/trace cap_capable (e.g. protection from inlining and API updates) add a tracepoint that passes:
- The credentials used
- The user namespace of the resource being accessed
- The user namespace that has the capability to access the targeted resource
- The capability to check for
- Bitmask of options defined in include/linux/security.h
- The return value of the check

---
 MAINTAINERS                       |  1 +
 include/trace/events/capability.h | 60 +++++++++++++++++++++++++++++++
 security/commoncap.c              | 31 +++++++++++-----
 3 files changed, 84 insertions(+), 8 deletions(-)
 create mode 100644 include/trace/events/capability.h
MAINTAINERS +1 -0
index cc40a9d9b8cd..210e9076c858 100644
@@ -4994,6 +4994,7 @@ M:        Serge Hallyn <[email protected]>
 L:     [email protected]
 S:     Supported
 F:     include/linux/capability.h
+F:     include/trace/events/capability.h
 F:     include/uapi/linux/capability.h
 F:     kernel/capability.c
 F:     security/commoncap.c
include/trace/events/capability.h new file +60 -0
new file mode 100644
index 000000000000..e706ce690c38
@@ -0,0 +1,60 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#undef TRACE_SYSTEM
+#define TRACE_SYSTEM capability
+
+#if !defined(_TRACE_CAPABILITY_H) || defined(TRACE_HEADER_MULTI_READ)
+#define _TRACE_CAPABILITY_H
+
+#include <linux/cred.h>
+#include <linux/tracepoint.h>
+#include <linux/user_namespace.h>
+
+/**
+ * cap_capable - called after it's determined if a task has a particular
+ * effective capability
+ *
+ * @cred: The credentials used
+ * @targ_ns: The user namespace of the resource being accessed
+ * @capable_ns: The user namespace in which the credential provides the
+ *              capability to access the targeted resource.
+ *              This will be NULL if ret is not 0.
+ * @cap: The capability to check for
+ * @opts: Bitmask of options defined in include/linux/security.h
+ * @ret: The return value of the check: 0 if it does, -ve if it does not
+ *
+ * Allows to trace calls to cap_capable in commoncap.c
+ */
+TRACE_EVENT(cap_capable,
+
+       TP_PROTO(const struct cred *cred, struct user_namespace *targ_ns,
+               struct user_namespace *capable_ns, int cap, unsigned int opts, int ret),
+
+       TP_ARGS(cred, targ_ns, capable_ns, cap, opts, ret),
+
+       TP_STRUCT__entry(
+               __field(const struct cred *, cred)
+               __field(struct user_namespace *, targ_ns)
+               __field(struct user_namespace *, capable_ns)
+               __field(int, cap)
+               __field(unsigned int, opts)
+               __field(int, ret)
+       ),
+
+       TP_fast_assign(
+               __entry->cred       = cred;
+               __entry->targ_ns    = targ_ns;
+               __entry->capable_ns = capable_ns;
+               __entry->cap        = cap;
+               __entry->opts       = opts;
+               __entry->ret        = ret;
+       ),
+
+       TP_printk("cred %p, targ_ns %p, capable_ns %p, cap %d, opts %u, ret %d",
+               __entry->cred, __entry->targ_ns, __entry->capable_ns, __entry->cap,
+               __entry->opts, __entry->ret)
+);
+
+#endif /* _TRACE_CAPABILITY_H */
+
+/* This part must be outside protection */
+#include <trace/define_trace.h>
security/commoncap.c +6 -3
index 162d96b3a676..12c3ddfe0d6e 100644
@@ -27,6 +27,9 @@
 #include <linux/mnt_idmapping.h>
 #include <uapi/linux/lsm.h>
 
+#define CREATE_TRACE_POINTS
+#include <trace/events/capability.h>
+
 /*
  * If a non-root user executes a setuid-root binary in
  * !secure(SECURE_NOROOT) mode, then we raise capabilities.
@@ -52,7 +55,7 @@ static void warn_setuid_and_fcaps_mixed(const char *fname)
 /**
  * cap_capable - Determine whether a task has a particular effective capability
  * @cred: The credentials to use
- * @targ_ns:  The user namespace in which we need the capability
+ * @targ_ns:  The user namespace of the resource being accessed
  * @cap: The capability to check for
  * @opts: Bitmask of options defined in include/linux/security.h
  *
@@ -68,6 +71,7 @@ int cap_capable(const struct cred *cred, struct user_namespace *targ_ns,
                int cap, unsigned int opts)
 {
        struct user_namespace *ns = targ_ns;
+       int ret = -EPERM;
 
        /* See if cred has the capability in the target user namespace
         * by examining the target user namespace and all of the target
@@ -75,22 +79,32 @@ int cap_capable(const struct cred *cred, struct user_namespace *targ_ns,
         */
        for (;;) {
                /* Do we have the necessary capabilities? */
-               if (ns == cred->user_ns)
-                       return cap_raised(cred->cap_effective, cap) ? 0 : -EPERM;
+               if (ns == cred->user_ns) {
Quoted 8 lines

security/commoncap.c int cap_capable(const struct cred *cred, struct user_namespace *targ_ns, · line 86

Quoted 4 lines
security/commoncap.c +4 -0
+                       if (cap_raised(cred->cap_effective, cap))
+                               ret = 0;
+                       else
+                               ns = NULL;

This is a bit unfortunate :( so maybe all we needed was ns = ns->parent for that one use case, and keep the original `ret ? NULL :
ns` inside trace_cap_capable().

Yeah, that would be fine with me. Or maybe just doing

        /* in case of an error, trace should show ns=NULL */
        if (ret)
                ns = NULL;
Quoted 6 lines
Quoted 1 line

right above the trace_cap_capable() call would be clearer.

I feel like having less trace specific logic in this function would be a good thing,
so I'm for Andrii's suggestion of doing the ret check there but also fine to do what security folks prefer :)

I think a comment is needed to remind us (me) in 2 years why the seting of ns to NULL is there. But the comment of trace_cap_capable() probably suffices, so sure, go with Andrii's suggestion. And then

for the capability code.

thanks,
-serge

Quoted 9 lines
Quoted 3 lines

I think we're suggesting to not set ns = NULL here and instead check the ret value in the trace code e.g. __entry->capable_ns = ret ? NULL : capable_ns;

Perfect. Was originally going to suggest this, but then thought well the rest of the ns logic is purely capability not tracing related. But since the comment is in trace_cap_capable(), putting the assignment there makes sense.

Quoted 4 lines

Actually, I had another idea. What about just having a separate variable in the cap_capable function for capable_ns that only gets set if ret is 0. Then we're not changing the ns variable at all for the purposes of the trace function.

FWIW that sounds great.

-serge