security: add trace event for cap_capable
Subsystems (3)
PATCH 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 which needs the capability
- The user namespace that actually has the capability (if one exists)
- 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 | 58 +++++++++++++++++++++++++++++++ security/commoncap.c | 21 +++++++---- 3 files changed, 74 insertions(+), 6 deletions(-) create mode 100644 include/trace/events/capability.h
MAINTAINERS +1 -0
@@ -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 +58 -0
58 lines not on this page · raw message
security/commoncap.c +15 -6
@@ -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.
@@ -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 =0 ;/* 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,26 @@ 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) { + ret =cap_raised (cred->cap_effective, cap) ?0 : -EPERM ; +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) { + ret = -EPERM ; +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 ; +break ;/* * If you have a capability in a parent user ns, then you have
@@ -99,7 +107,8 @@ int cap_capable(const struct cred *cred, struct user_namespace *targ_ns, ns = ns->parent; } -/* We never get here */ +trace_capable (cred, targ_ns, ret ==0 ? ns :NULL , cap, opts, ret); +return ret; }/**
--
2.43.5
Steven Rostedt
maintainerQuoted 19 lines · On Thu, 24 Oct 2024 03:40:12 -0700 Jordan Rome <[email protected]> wrote:
include/trace/events/capability.h new file +19
-0+TRACE_EVENT(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;
include/trace/events/capability.h line 54
Quoted 10 lines
include/trace/events/capability.h new file +10
-0+ __entry->capable_ns = capable_ns; + __entry->cap = cap; + __entry->opts = opts; + __entry->ret = ret; + ), + +TP_printk ("cap %d, opts %u, ret %d" , + __entry->cap, __entry->opts, __entry->ret) +); +
You record cred, targ_ns and capable_ns but don't use it in TP_printk?
It's fine to print pointers there. Is there a reason you do not?
-- Steve
Quoted 36 lines · Steven Rostedt
Quoted 19 lines · On Thu, 24 Oct 2024 03:40:12 -0700 Jordan Rome <[email protected]> wrote:
include/trace/events/capability.h new file +19
-0+TRACE_EVENT(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;include/trace/events/capability.h line 54
Quoted 10 lines
include/trace/events/capability.h new file +10
-0+ __entry->capable_ns = capable_ns; + __entry->cap = cap; + __entry->opts = opts; + __entry->ret = ret; + ), + +TP_printk ("cap %d, opts %u, ret %d" , + __entry->cap, __entry->opts, __entry->ret) +); +You record cred,
targ_nsandcapable_nsbut don't use it inTP_printk?
Quoted 3 lines
It's fine to print pointers there. Is there a reason you do not?
-- Steve
I wasn't sure if printing the pointers was useful. I'm fine to add them in.
Quoted 36 lines · Steven Rostedt
Quoted 19 lines · On Thu, 24 Oct 2024 03:40:12 -0700 Jordan Rome <[email protected]> wrote:
include/trace/events/capability.h new file +19
-0+TRACE_EVENT(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;include/trace/events/capability.h line 54
Quoted 10 lines
include/trace/events/capability.h new file +10
-0+ __entry->capable_ns = capable_ns; + __entry->cap = cap; + __entry->opts = opts; + __entry->ret = ret; + ), + +TP_printk ("cap %d, opts %u, ret %d" , + __entry->cap, __entry->opts, __entry->ret) +); +You record cred,
targ_nsandcapable_nsbut don't use it inTP_printk?
Quoted 1 line
It's fine to print pointers there. Is there a reason you do not?
Are those pointers really useful for anything? Maybe it's better to print ns->ns.inum instead? At least that's something that is usable from user space side, no?
Quoted 2 lines
-- Steve
Quoted 125 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 which needs the capability
- The user namespace that actually has the capability (if one exists)
- The capability to check for
- Bitmask of options defined ininclude/linux/security.h
- The return value of the checkSigned-off-by Jordan Rome <[email protected]>--- MAINTAINERS | 1 + include/trace/events/capability.h | 58 +++++++++++++++++++++++++++++++ security/commoncap.c | 21 +++++++---- 3 files changed, 74 insertions(+), 6 deletions(-) create mode 100644 include/trace/events/capability.hMAINTAINERS +1
-0@@ -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.cinclude/trace/events/capability.h new file +58
-0@@ -0,0 +1,58 @@ +/* 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> + +/** +* capable - called after it's determined if a task has a particular +* effective capability +* +* @cred: The credentials used +* @targ_ns: The user namespace which needs the capability +* @capable_ns: The user namespace that actually has the capability +* if ret is 0 otherwise this will be NULL +* @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 (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 ("cap %d, opts %u, ret %d" , + __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
-2@@ -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. @@ -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 =0 ;/* 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,26 @@ 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) { + ret =cap_raised (cred->cap_effective, cap) ?0 : -EPERM ;
security/commoncap.c int cap_capable(const struct cred *cred, struct user_namespace *targ_ns, · line 91
Quoted 10 lines
security/commoncap.c +3
-2+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) {
I'd do
ns = NULL;
here
security/commoncap.c int cap_capable(const struct cred *cred, struct user_namespace *targ_ns, · line 100
Quoted 9 lines
security/commoncap.c +3
-0+ ret = -EPERM ; +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))
and this needs ns = ns->parent; (because that's the namespace that grants capability, is that right?)
security/commoncap.c int cap_capable(const struct cred *cred, struct user_namespace *targ_ns, · line 110
Quoted 11 lines
security/commoncap.c +2
-2-return 0 ; +break ;/* * If you have a capability in a parent user ns, then you have @@ -99,7 +107,8 @@ int cap_capable(const struct cred *cred, struct user_namespace *targ_ns, ns = ns->parent; } -/* We never get here */ +trace_capable (cred, targ_ns, ret ==0 ? ns :NULL , cap, opts, ret);
with the above changes just pass ns directly, no need for ret == 0 check (and it seems it's wrong for one of the case due to ns->parent use)
Quoted 7 lines
security/commoncap.c +1
-1+return ret; }/** -- 2.43.5
Paul Moore
maintainerQuoted 18 lines · On Oct 24, 2024 Jordan Rome <[email protected]> wrote:
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 which needs the capability
- The user namespace that actually has the capability (if one exists)
- The capability to check for
- Bitmask of options defined ininclude/linux/security.h
- The return value of the checkSigned-off-by Jordan Rome <[email protected]>--- MAINTAINERS | 1 + include/trace/events/capability.h | 58 +++++++++++++++++++++++++++++++ security/commoncap.c | 21 +++++++---- 3 files changed, 74 insertions(+), 6 deletions(-) create mode 100644 include/trace/events/capability.h
...
Quoted 22 lines
include/trace/events/capability.h new file +16
-0@@ -0,0 +1,58 @@ +/* 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> + +/** +* capable - called after it's determined if a task has a particular +* effective capability +* +* @cred: The credentials used
include/trace/events/capability.h line 26
Quoted 10 lines
include/trace/events/capability.h new file +10
-0+* @targ_ns: The user namespace which needs the capability +* @capable_ns: The user namespace that actually has the capability +* if ret is 0 otherwise this will be NULL +* @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(capable,
This should either be named "cap_capable" if you are only interested in the CAP_XXX capability checks or "capable" if you are interested in all of the checks that are performed when capable() is called from within the kernel. Presently safesetid, apparmor, and selinux all enforce access controls when capable() is called, with the potential for additional checks in future kernel releases.
Quoted 21 lines
include/trace/events/capability.h new file +21
-0+ 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;
include/trace/events/capability.h line 58
Quoted 10 lines
include/trace/events/capability.h new file +10
-0+ ), + +TP_printk ("cap %d, opts %u, ret %d" , + __entry->cap, __entry->opts, __entry->ret) +); + +#endif /* _TRACE_CAPABILITY_H */ + +/* This part must be outside protection */ +#include <trace/define_trace.h>
--
paul-moore.com
Quoted 5 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 which needs the capability
"the user namespace which needs the capability" is not quite the right way to put this. It's the user namespace against which the capability is needed. It's an object, not a subject. Or maybe "the user namespace of the resource being accessed".
Quoted 1 line
- The user namespace that actually has the capability (if one exists)
How about "the user namespace in which the task has the capability targeted at the resource"? (It's not the user namespace itself that has the capability)
Quoted 37 lines
- The capability to check for
- Bitmask of options defined ininclude/linux/security.h
- The return value of the checkSigned-off-by Jordan Rome <[email protected]>--- MAINTAINERS | 1 + include/trace/events/capability.h | 58 +++++++++++++++++++++++++++++++ security/commoncap.c | 21 +++++++---- 3 files changed, 74 insertions(+), 6 deletions(-) create mode 100644 include/trace/events/capability.hMAINTAINERS +1
-0@@ -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.cinclude/trace/events/capability.h new file +7
-0@@ -0,0 +1,58 @@ +/* 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/trace/events/capability.h line 17
Quoted 10 lines
include/trace/events/capability.h new file +10
-0+#include <linux/cred.h> +#include <linux/tracepoint.h> +#include <linux/user_namespace.h> + +/** +* capable - called after it's determined if a task has a particular +* effective capability +* +* @cred: The credentials used +* @targ_ns: The user namespace which needs the capability
(same here)
Quoted 96 lines
include/trace/events/capability.h new file +41
-0+* @capable_ns: The user namespace that actually has the capability +* if ret is 0 otherwise this will be NULL +* @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 (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 ("cap %d, opts %u, ret %d" , + __entry->cap, __entry->opts, __entry->ret) +); + +#endif /* _TRACE_CAPABILITY_H */ + +/* This part must be outside protection */ +#include <trace/define_trace.h> security/commoncap.c +13
-5@@ -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. @@ -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 =0 ;/* 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,26 @@ 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) { + ret =cap_raised (cred->cap_effective, cap) ?0 : -EPERM ; +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) { + ret = -EPERM ; +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 ; +break ;/* * If you have a capability in a parent user ns, then you have
security/commoncap.c int cap_capable(const struct cred *cred, struct user_namespace *targ_ns, · line 102
Quoted 4 lines
security/commoncap.c +0
-1@@ -99,7 +107,8 @@ int cap_capable(const struct cred *cred, struct user_namespace *targ_ns, ns = ns->parent; } -/* We never get here */
With this change, I become less comfortable with us assuming that it is the case that we'll never just drop off the end of the while loop. I'd be more comfortable if you set ret = -EPERM at the top, and set it to 0 in the last break.
Quoted 8 lines
security/commoncap.c +2
-1+trace_capable (cred, targ_ns, ret ==0 ? ns :NULL , cap, opts, ret); +return ret; }/** -- 2.43.5
Steven Rostedt
maintainerQuoted 7 lines · On Thu, 24 Oct 2024 10:48:55 -0700 Andrii Nakryiko <[email protected]>…
Quoted 3 lines
You record cred,
targ_nsandcapable_nsbut don't use it inTP_printk?It's fine to print pointers there. Is there a reason you do not?
Are those pointers really useful for anything? Maybe it's better to print
ns->ns.inuminstead? At least that's something that is usable from user space side, no?
Pointers are actually useful from user space. It allows you to add eprobes to get data from the structure. Yes, you can do this from BPF but sometimes a shell script is nicer to use.
$ gdb vmlinux (gdb) print &(((struct user_namespace *)0)->ns.inum) $2 = (unsigned int *) 0xe8
# cd /sys/kernel/tracing # echo 'e:cap capability/capable num=+0e8($capable-ns)' > dynamic_events # echo 1 > events/eprobes/cap/enable # cat trace
Thus pointers give a nice way of getting info dynamically, and having the pointer printed out in the TP_printk also helps to know you can do this.
I realize that eprobes is not documented well (or at all) which needs to be fixed.
-- Steve
Quoted 8 lines · On Thu, Oct 24, 2024 at 4:28 PM <[email protected]> wrote:
Quoted 5 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 which needs the capability
Quoted 5 lines
"the user namespace which needs the capability" is not quite the right way to put this. It's the user namespace against which the capability is needed. It's an object, not a subject. Or maybe "the user namespace of the resource being accessed".
I like "The user namespace of the resource being accessed"
Quoted 6 lines
Quoted 1 line
- The user namespace that actually has the capability (if one exists)
How about "the user namespace in which the task has the capability targeted at the resource"? (It's not the user namespace itself that has the capability)
This phrasing seems a little confusing. How about:
"The user namespace that has the capability to access the targeted resource" ?
Quoted 149 lines
Quoted 37 lines
- The capability to check for
- Bitmask of options defined ininclude/linux/security.h
- The return value of the checkSigned-off-by Jordan Rome <[email protected]>--- MAINTAINERS | 1 + include/trace/events/capability.h | 58 +++++++++++++++++++++++++++++++ security/commoncap.c | 21 +++++++---- 3 files changed, 74 insertions(+), 6 deletions(-) create mode 100644 include/trace/events/capability.hMAINTAINERS +1
-0@@ -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.cinclude/trace/events/capability.h new file +7
-0@@ -0,0 +1,58 @@ +/* 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/trace/events/capability.h line 17
Quoted 10 lines
include/trace/events/capability.h new file +10
-0+#include <linux/cred.h> +#include <linux/tracepoint.h> +#include <linux/user_namespace.h> + +/** +* capable - called after it's determined if a task has a particular +* effective capability +* +* @cred: The credentials used +* @targ_ns: The user namespace which needs the capability (same here)
Quoted 99 lines
include/trace/events/capability.h new file +41
-0+* @capable_ns: The user namespace that actually has the capability +* if ret is 0 otherwise this will be NULL +* @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 (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 ("cap %d, opts %u, ret %d" , + __entry->cap, __entry->opts, __entry->ret) +); + +#endif /* _TRACE_CAPABILITY_H */ + +/* This part must be outside protection */ +#include <trace/define_trace.h> security/commoncap.c +13
-5@@ -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. @@ -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 =0 ;/* 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,26 @@ 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) { + ret =cap_raised (cred->cap_effective, cap) ?0 : -EPERM ; +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) { + ret = -EPERM ; +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 ; +break ;/* * If you have a capability in a parent user ns, then you have @@ -99,7 +107,8 @@ int cap_capable(const struct cred *cred, struct user_namespace *targ_ns, ns = ns->parent; }
Quoted 7 lines
security/commoncap.c int cap_capable(const struct cred *cred, struct user_namespace *targ_ns, · line 102
Quoted 1 line
security/commoncap.c +0
-1-/* We never get here */ With this change, I become less comfortable with us assuming that it is the case that we'll never just drop off the end of the while loop. I'd be more comfortable if you set ret =
-EPERMat the top, and set it to 0 in the last break.
Sure. I can make this change.
Quoted 8 lines
Quoted 8 lines
security/commoncap.c +2
-1+trace_capable (cred, targ_ns, ret ==0 ? ns :NULL , cap, opts, ret); +return ret; }/** -- 2.43.5
Quoted 19 lines · Jordan Rome
Quoted 8 lines · On Thu, Oct 24, 2024 at 4:28 PM <[email protected]> wrote:
Quoted 5 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 which needs the capabilityQuoted 5 lines
"the user namespace which needs the capability" is not quite the right way to put this. It's the user namespace against which the capability is needed. It's an object, not a subject. Or maybe "the user namespace of the resource being accessed".
I like "The user namespace of the resource being accessed"
Quoted 2 lines
Quoted 1 line
- The user namespace that actually has the capability (if one exists)
Quoted 7 lines
Quoted 4 lines
How about "the user namespace in which the task has the capability targeted at the resource"? (It's not the user namespace itself that has the capability)
This phrasing seems a little confusing. How about:
"The user namespace that has the capability to access the targeted resource" ?
The problem again is that it's not the user namespace that has the capability. The cred has the capability in that user namespace.
"The user namespace in which the credential has the capability to access the targeted resource"?
Or maybe s/has/provides/.
Quoted 167 lines
Quoted 149 lines
Quoted 37 lines
- The capability to check for
- Bitmask of options defined ininclude/linux/security.h
- The return value of the checkSigned-off-by Jordan Rome <[email protected]>--- MAINTAINERS | 1 + include/trace/events/capability.h | 58 +++++++++++++++++++++++++++++++ security/commoncap.c | 21 +++++++---- 3 files changed, 74 insertions(+), 6 deletions(-) create mode 100644 include/trace/events/capability.hMAINTAINERS +1
-0@@ -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.cinclude/trace/events/capability.h new file +7
-0@@ -0,0 +1,58 @@ +/* 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/trace/events/capability.h line 17
Quoted 10 lines
include/trace/events/capability.h new file +10
-0+#include <linux/cred.h> +#include <linux/tracepoint.h> +#include <linux/user_namespace.h> + +/** +* capable - called after it's determined if a task has a particular +* effective capability +* +* @cred: The credentials used +* @targ_ns: The user namespace which needs the capability (same here)
Quoted 99 lines
include/trace/events/capability.h new file +41
-0+* @capable_ns: The user namespace that actually has the capability +* if ret is 0 otherwise this will be NULL +* @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 (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 ("cap %d, opts %u, ret %d" , + __entry->cap, __entry->opts, __entry->ret) +); + +#endif /* _TRACE_CAPABILITY_H */ + +/* This part must be outside protection */ +#include <trace/define_trace.h> security/commoncap.c +13
-5@@ -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. @@ -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 =0 ;/* 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,26 @@ 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) { + ret =cap_raised (cred->cap_effective, cap) ?0 : -EPERM ; +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) { + ret = -EPERM ; +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 ; +break ;/* * If you have a capability in a parent user ns, then you have @@ -99,7 +107,8 @@ int cap_capable(const struct cred *cred, struct user_namespace *targ_ns, ns = ns->parent; }Quoted 7 lines
security/commoncap.c int cap_capable(const struct cred *cred, struct user_namespace *targ_ns, · line 102
Quoted 1 line
security/commoncap.c +0
-1-/* We never get here */ With this change, I become less comfortable with us assuming that it is the case that we'll never just drop off the end of the while loop. I'd be more comfortable if you set ret =
-EPERMat the top, and set it to 0 in the last break.Sure. I can make this change.
Quoted 8 lines
Quoted 8 lines
security/commoncap.c +2
-1+trace_capable (cred, targ_ns, ret ==0 ? ns :NULL , cap, opts, ret); +return ret; }/** -- 2.43.5
Quoted 29 lines · On Fri, Oct 25, 2024 at 7:18 AM <[email protected]> wrote:
Quoted 19 lines · Jordan Rome
Quoted 8 lines · On Thu, Oct 24, 2024 at 4:28 PM <[email protected]> wrote:
Quoted 5 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 which needs the capabilityQuoted 5 lines
"the user namespace which needs the capability" is not quite the right way to put this. It's the user namespace against which the capability is needed. It's an object, not a subject. Or maybe "the user namespace of the resource being accessed".
I like "The user namespace of the resource being accessed"
Quoted 2 lines
Quoted 1 line
- The user namespace that actually has the capability (if one exists)
Quoted 7 lines
Quoted 4 lines
How about "the user namespace in which the task has the capability targeted at the resource"? (It's not the user namespace itself that has the capability)
This phrasing seems a little confusing. How about:
"The user namespace that has the capability to access the targeted resource" ?
Quoted 8 lines
The problem again is that it's not the user namespace that has the capability. The cred has the capability in that user namespace.
"The user namespace in which the credential has the capability to access the targeted resource"?
Or maybe s/has/provides/.
This sounds good to me with "provides"
Quoted 167 lines
Quoted 167 lines
Quoted 149 lines
Quoted 37 lines
- The capability to check for
- Bitmask of options defined ininclude/linux/security.h
- The return value of the checkSigned-off-by Jordan Rome <[email protected]>--- MAINTAINERS | 1 + include/trace/events/capability.h | 58 +++++++++++++++++++++++++++++++ security/commoncap.c | 21 +++++++---- 3 files changed, 74 insertions(+), 6 deletions(-) create mode 100644 include/trace/events/capability.hMAINTAINERS +1
-0@@ -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.cinclude/trace/events/capability.h new file +7
-0@@ -0,0 +1,58 @@ +/* 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/trace/events/capability.h line 17
Quoted 10 lines
include/trace/events/capability.h new file +10
-0+#include <linux/cred.h> +#include <linux/tracepoint.h> +#include <linux/user_namespace.h> + +/** +* capable - called after it's determined if a task has a particular +* effective capability +* +* @cred: The credentials used +* @targ_ns: The user namespace which needs the capability (same here)
Quoted 99 lines
include/trace/events/capability.h new file +41
-0+* @capable_ns: The user namespace that actually has the capability +* if ret is 0 otherwise this will be NULL +* @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 (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 ("cap %d, opts %u, ret %d" , + __entry->cap, __entry->opts, __entry->ret) +); + +#endif /* _TRACE_CAPABILITY_H */ + +/* This part must be outside protection */ +#include <trace/define_trace.h> security/commoncap.c +13
-5@@ -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. @@ -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 =0 ;/* 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,26 @@ 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) { + ret =cap_raised (cred->cap_effective, cap) ?0 : -EPERM ; +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) { + ret = -EPERM ; +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 ; +break ;/* * If you have a capability in a parent user ns, then you have @@ -99,7 +107,8 @@ int cap_capable(const struct cred *cred, struct user_namespace *targ_ns, ns = ns->parent; }Quoted 7 lines
security/commoncap.c int cap_capable(const struct cred *cred, struct user_namespace *targ_ns, · line 102
Quoted 1 line
security/commoncap.c +0
-1-/* We never get here */ With this change, I become less comfortable with us assuming that it is the case that we'll never just drop off the end of the while loop. I'd be more comfortable if you set ret =
-EPERMat the top, and set it to 0 in the last break.Sure. I can make this change.
Quoted 8 lines
Quoted 8 lines
security/commoncap.c +2
-1+trace_capable (cred, targ_ns, ret ==0 ? ns :NULL , cap, opts, ret); +return ret; }/** -- 2.43.5