Skip to contentSkip to conversation

capability: introduce new capable flag NODENYAUDIT

PATCH In review Christian Göttsche 8 participants 23 messages May 11, 2023 – Jun 6, 2023

Subsystems (22)
lore
Share & tools
lore
#0 lore raw

PATCH v4 1/9 capability: introduce new capable flag NODENYAUDIT

Introduce a new capable flag, CAP_OPT_NODENYAUDIT, to not generate an audit event if the requested capability is not granted. This will be used in a new capable_any() functionality to reduce the number of necessary capable calls.

Handle the flag accordingly in AppArmor and SELinux.

---
 include/linux/security.h       |  2 ++
 security/apparmor/capability.c |  8 +++++---
 security/selinux/hooks.c       | 14 ++++++++------
 3 files changed, 15 insertions(+), 9 deletions(-)
include/linux/security.h +2 -0
index e2734e9e44d5..629c775ec297 100644
@@ -67,6 +67,8 @@ struct watch_notification;
 #define CAP_OPT_NOAUDIT BIT(1)
 /* If capable is being called by a setid function */
 #define CAP_OPT_INSETID BIT(2)
+/* If capable should audit the security request for authorized requests only */
+#define CAP_OPT_NODENYAUDIT BIT(3)
 
 /* LSM Agnostic defines for security_sb_set_mnt_opts() flags */
 #define SECURITY_LSM_NATIVE_LABELS	1
security/apparmor/capability.c +5 -3
index 326a51838ef2..98120dd62ca7 100644
@@ -108,7 +108,8 @@ static int audit_caps(struct common_audit_data *sa, struct aa_profile *profile,
  * profile_capable - test if profile allows use of capability @cap
  * @profile: profile being enforced    (NOT NULL, NOT unconfined)
  * @cap: capability to test if allowed
- * @opts: CAP_OPT_NOAUDIT bit determines whether audit record is generated
+ * @opts: CAP_OPT_NOAUDIT/CAP_OPT_NODENYAUDIT bit determines whether audit
+ *	record is generated
  * @sa: audit data (MAY BE NULL indicating no auditing)
  *
  * Returns: 0 if allowed else -EPERM
@@ -126,7 +127,7 @@ static int profile_capable(struct aa_profile *profile, int cap,
 	else
 		error = -EPERM;
 
-	if (opts & CAP_OPT_NOAUDIT) {
+	if ((opts & CAP_OPT_NOAUDIT) || ((opts & CAP_OPT_NODENYAUDIT) && error)) {
 		if (!COMPLAIN_MODE(profile))
 			return error;
 		/* audit the cap request in complain mode but note that it
@@ -142,7 +143,8 @@ static int profile_capable(struct aa_profile *profile, int cap,
  * aa_capable - test permission to use capability
  * @label: label being tested for capability (NOT NULL)
  * @cap: capability to be tested
- * @opts: CAP_OPT_NOAUDIT bit determines whether audit record is generated
+ * @opts: CAP_OPT_NOAUDIT/CAP_OPT_NODENYAUDIT bit determines whether audit
+ *	record is generated
  *
  * Look up capability in profile capability set.
  *
security/selinux/hooks.c +8 -6
index 79b4890e9936..0730edf2f5f1 100644
@@ -1571,7 +1571,7 @@ static int cred_has_capability(const struct cred *cred,
 	u16 sclass;
 	u32 sid = cred_sid(cred);
 	u32 av = CAP_TO_MASK(cap);
-	int rc;
+	int rc, rc2;
 
 	ad.type = LSM_AUDIT_DATA_CAP;
 	ad.u.cap = cap;
@@ -1590,11 +1590,13 @@ static int cred_has_capability(const struct cred *cred,
 	}
 
 	rc = avc_has_perm_noaudit(sid, sid, sclass, av, 0, &avd);
-	if (!(opts & CAP_OPT_NOAUDIT)) {
-		int rc2 = avc_audit(sid, sid, sclass, av, &avd, rc, &ad);
-		if (rc2)
-			return rc2;
-	}
+	if ((opts & CAP_OPT_NOAUDIT) || ((opts & CAP_OPT_NODENYAUDIT) && rc))
+		return rc;
+
+	rc2 = avc_audit(sid, sid, sclass, av, &avd, rc, &ad);
+	if (rc2)
+		return rc2;
+
 	return rc;
 }
 
Signature · 1 line
2.40.1
#1 lore raw

PATCH v4 2/9 capability: add any wrapper to test for multiple caps with exactly one audit message

Add the interfaces capable_any() and ns_capable_any() as an alternative to multiple capable()/ns_capable() calls, like capable_any(CAP_SYS_NICE, CAP_SYS_ADMIN) instead of capable(CAP_SYS_NICE) || capable(CAP_SYS_ADMIN).

capable_any()/ns_capable_any() will in particular generate exactly one audit message, either for the left most capability in effect or, if the task has none, the first one.

This is especially helpful with regard to SELinux, where each audit message about a not allowed capability request will create a denial message. Using this new wrapper with the least invasive capability as left most argument (e.g. CAP_SYS_NICE before CAP_SYS_ADMIN) enables policy writers to only grant the least invasive one for the particular subject instead of both.

---
v4:
Use CAP_OPT_NODENYAUDIT via added ns_capable_nodenyaudit()
v3:
- rename to capable_any()
- fix typo in function documentation
- add ns_capable_any()
v2:
avoid varargs and fix to two capabilities; capable_or3() can be added later if needed

---
 include/linux/capability.h | 10 ++++++
 kernel/capability.c        | 70 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 80 insertions(+)
include/linux/capability.h +10 -0
index 0c356a517991..eeb958440656 100644
@@ -146,7 +146,9 @@ extern bool has_capability_noaudit(struct task_struct *t, int cap);
 extern bool has_ns_capability_noaudit(struct task_struct *t,
 				      struct user_namespace *ns, int cap);
 extern bool capable(int cap);
+extern bool capable_any(int cap1, int cap2);
 extern bool ns_capable(struct user_namespace *ns, int cap);
+extern bool ns_capable_any(struct user_namespace *ns, int cap1, int cap2);
 extern bool ns_capable_noaudit(struct user_namespace *ns, int cap);
 extern bool ns_capable_setid(struct user_namespace *ns, int cap);
 #else
@@ -172,10 +174,18 @@ static inline bool capable(int cap)
 {
 	return true;
 }
+static inline bool capable_any(int cap1, int cap2)
+{
+	return true;
+}
 static inline bool ns_capable(struct user_namespace *ns, int cap)
 {
 	return true;
 }
+static inline bool ns_capable_any(struct user_namespace *ns, int cap1, int cap2)
+{
+	return true;
+}
 static inline bool ns_capable_noaudit(struct user_namespace *ns, int cap)
 {
 	return true;
kernel/capability.c +70 -0

82 lines not on this page · raw message

Signature · 1 line
2.40.1
#2 lore raw

PATCH v4 3/9 capability: use new capable_any functionality

In reply to Christian Göttsche

Use the new added capable_any function in appropriate cases, where a task is required to have any of two capabilities.

---
v3:
- rename to capable_any()
- simplify checkpoint_restore_ns_capable()

---
 include/linux/capability.h | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
include/linux/capability.h +3 -4
index eeb958440656..4db0ffb47271 100644
@@ -204,18 +204,17 @@ extern bool file_ns_capable(const struct file *file, struct user_namespace *ns,
 extern bool ptracer_capable(struct task_struct *tsk, struct user_namespace *ns);
 static inline bool perfmon_capable(void)
 {
-	return capable(CAP_PERFMON) || capable(CAP_SYS_ADMIN);
+	return capable_any(CAP_PERFMON, CAP_SYS_ADMIN);
 }
 
 static inline bool bpf_capable(void)
 {
-	return capable(CAP_BPF) || capable(CAP_SYS_ADMIN);
+	return capable_any(CAP_BPF, CAP_SYS_ADMIN);
 }
 
 static inline bool checkpoint_restore_ns_capable(struct user_namespace *ns)
 {
-	return ns_capable(ns, CAP_CHECKPOINT_RESTORE) ||
-		ns_capable(ns, CAP_SYS_ADMIN);
+	return ns_capable_any(ns, CAP_CHECKPOINT_RESTORE, CAP_SYS_ADMIN);
 }
 
 /* audit system wants to get cap info from files as well */
Signature · 1 line
2.40.1
Acked-by on v4 3/9
#3 lore raw

Re: [PATCH v4 3/9] capability: use new capable_any functionality

Quoted 21 lines · Christian Göttsche

Use the new added capable_any function in appropriate cases, where a task is required to have any of two capabilities.

---
v3:
- rename to capable_any()
- simplify checkpoint_restore_ns_capable()

---
 include/linux/capability.h | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
include/linux/capability.h +0 -0
index eeb958440656..4db0ffb47271 100644
@@ -204,18 +204,17 @@ extern bool file_ns_capable(const struct file *file, struct user_namespace *ns,
 extern bool ptracer_capable(struct task_struct *tsk, struct user_namespace *ns);
 static inline bool perfmon_capable(void)
 {

include/linux/capability.h extern bool file_ns_capable(const struct file *file, struct user_namespace *ns, · line 214

Quoted 10 lines
include/linux/capability.h +2 -2
-       return capable(CAP_PERFMON) || capable(CAP_SYS_ADMIN);
+       return capable_any(CAP_PERFMON, CAP_SYS_ADMIN);
 }
 
 static inline bool bpf_capable(void)
 {
-       return capable(CAP_BPF) || capable(CAP_SYS_ADMIN);
+       return capable_any(CAP_BPF, CAP_SYS_ADMIN);
 }
 

For bpf parts:

Quoted 12 lines
include/linux/capability.h +1 -3
 static inline bool checkpoint_restore_ns_capable(struct user_namespace *ns)
 {
-       return ns_capable(ns, CAP_CHECKPOINT_RESTORE) ||
-               ns_capable(ns, CAP_SYS_ADMIN);
+       return ns_capable_any(ns, CAP_CHECKPOINT_RESTORE, CAP_SYS_ADMIN);
 }
 
 /* audit system wants to get cap info from files as well */
--

2.40.1

#4 lore raw

PATCH v4 4/9 block: use new capable_any functionality

In reply to Christian Göttsche

Use the new added capable_any function in appropriate cases, where a task is required to have any of two capabilities.

Reorder CAP_SYS_ADMIN last.

Fixes 94c4b4fd25e6 ("block: Check ADMIN before NICE for IOPRIO_CLASS_RT")

---
v3:
rename to capable_any()

---
 block/ioprio.c | 9 +--------
 1 file changed, 1 insertion(+), 8 deletions(-)
block/ioprio.c +1 -8
index 32a456b45804..0a7df88bf6d9 100644
@@ -37,14 +37,7 @@ int ioprio_check_cap(int ioprio)
 
 	switch (class) {
 		case IOPRIO_CLASS_RT:
-			/*
-			 * Originally this only checked for CAP_SYS_ADMIN,
-			 * which was implicitly allowed for pid 0 by security
-			 * modules such as SELinux. Make sure we check
-			 * CAP_SYS_ADMIN first to avoid a denial/avc for
-			 * possibly missing CAP_SYS_NICE permission.
-			 */
-			if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_NICE))
+			if (!capable_any(CAP_SYS_NICE, CAP_SYS_ADMIN))
 				return -EPERM;
 			fallthrough;
 			/* rt has prio field too */
Signature · 1 line
2.40.1
#5 lore raw

Re: [PATCH v4 4/9] block: use new capable_any functionality

Quoted 2 lines · Christian Göttsche

Use the new added capable_any function in appropriate cases, where a task is required to have any of two capabilities.

What is this new function and why should we using it?

Your also forgot to Cc the block list on the entire series, making this page completely unreviewable.

#6 lore raw

Re: [PATCH v4 4/9] block: use new capable_any functionality

Quoted 6 lines · Christoph Hellwig
Quoted 2 lines · Christian Göttsche

Use the new added capable_any function in appropriate cases, where a task is required to have any of two capabilities.

What is this new function and why should we using it?

Quoting the description from
https://lore.kernel.org/all/[email protected]/
:

Add the interfaces capable_any() and ns_capable_any() as an alternative to multiple capable()/ns_capable() calls, like capable_any(CAP_SYS_NICE, CAP_SYS_ADMIN) instead of capable(CAP_SYS_NICE) || capable(CAP_SYS_ADMIN).

capable_any()/ns_capable_any() will in particular generate exactly one audit message, either for the left most capability in effect or, if the task has none, the first one.

This is especially helpful with regard to SELinux, where each audit message about a not allowed capability request will create a denial message. Using this new wrapper with the least invasive capability as left most argument (e.g. CAP_SYS_NICE before CAP_SYS_ADMIN) enables policy writers to only grant the least invasive one for the particular subject instead of both.

Quoted 2 lines

Your also forgot to Cc the block list on the entire series, making this page completely unreviewable.

#7 lore raw

PATCH v4 5/9 drivers: use new capable_any functionality

In reply to Christian Göttsche

Use the new added capable_any function in appropriate cases, where a task is required to have any of two capabilities.

Reorder CAP_SYS_ADMIN last.

---
v4:
Additional usage in kfd_ioctl()
v3:
rename to capable_any()

---
 drivers/gpu/drm/amd/amdkfd/kfd_chardev.c | 3 +--
 drivers/net/caif/caif_serial.c           | 2 +-
 drivers/s390/block/dasd_eckd.c           | 2 +-
 3 files changed, 3 insertions(+), 4 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +1 -2
index 1b54a9aaae70..d21fb9d1556b 100644
@@ -2896,8 +2896,7 @@ static long kfd_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
 	 * more priviledged access.
 	 */
 	if (unlikely(ioctl->flags & KFD_IOC_FLAG_CHECKPOINT_RESTORE)) {
-		if (!capable(CAP_CHECKPOINT_RESTORE) &&
-						!capable(CAP_SYS_ADMIN)) {
+		if (!capable_any(CAP_CHECKPOINT_RESTORE, CAP_SYS_ADMIN)) {
 			retcode = -EACCES;
 			goto err_i1;
 		}
drivers/net/caif/caif_serial.c +1 -1
index 688075859ae4..ca3f82a0e3a6 100644
@@ -326,7 +326,7 @@ static int ldisc_open(struct tty_struct *tty)
 	/* No write no play */
 	if (tty->ops->write == NULL)
 		return -EOPNOTSUPP;
-	if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_TTY_CONFIG))
+	if (!capable_any(CAP_SYS_TTY_CONFIG, CAP_SYS_ADMIN))
 		return -EPERM;
 
 	/* release devices to avoid name collision */
drivers/s390/block/dasd_eckd.c +1 -1
index ade1369fe5ed..67d1058bce1b 100644
@@ -5370,7 +5370,7 @@ static int dasd_symm_io(struct dasd_device *device, void __user *argp)
 	char psf0, psf1;
 	int rc;
 
-	if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RAWIO))
+	if (!capable_any(CAP_SYS_RAWIO, CAP_SYS_ADMIN))
 		return -EACCES;
 	psf0 = psf1 = 0;
 
Signature · 1 line
2.40.1
maintainer Acked-by on v4 5/9
#8 lore raw

Re: [PATCH v4 5/9] drivers: use new capable_any functionality

Quoted 16 lines · Christian Göttsche

Use the new added capable_any function in appropriate cases, where a task is required to have any of two capabilities.

Reorder CAP_SYS_ADMIN last.

---
v4:
Additional usage in kfd_ioctl()
v3:
rename to capable_any()

---
 drivers/gpu/drm/amd/amdkfd/kfd_chardev.c | 3 +--
 drivers/net/caif/caif_serial.c           | 2 +-
 drivers/s390/block/dasd_eckd.c           | 2 +-
 3 files changed, 3 insertions(+), 4 deletions(-)

...

drivers/s390/block/dasd_eckd.c static int dasd_symm_io(struct dasd_device *device, void __user *argp) · line 5375

Quoted 12 lines
drivers/s390/block/dasd_eckd.c +1 -1
index ade1369fe5ed..67d1058bce1b 100644
@@ -5370,7 +5370,7 @@ static int dasd_symm_io(struct dasd_device *device, void __user *argp)
 	char psf0, psf1;
 	int rc;
 
-	if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RAWIO))
+	if (!capable_any(CAP_SYS_RAWIO, CAP_SYS_ADMIN))
 		return -EACCES;
 	psf0 = psf1 = 0;

For s390 part:

#9 lore raw

PATCH v4 6/9 fs: use new capable_any functionality

In reply to Christian Göttsche

Use the new added capable_any function in appropriate cases, where a task is required to have any of two capabilities.

---
v3:
rename to capable_any()

---
 fs/pipe.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/pipe.c +1 -1
index ceb17d2dfa19..05c64494d37b 100644
@@ -776,7 +776,7 @@ bool too_many_pipe_buffers_hard(unsigned long user_bufs)
 
 bool pipe_is_unprivileged_user(void)
 {
-	return !capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN);
+	return !capable_any(CAP_SYS_RESOURCE, CAP_SYS_ADMIN);
 }
 
 struct pipe_inode_info *alloc_pipe_info(void)
Signature · 1 line
2.40.1
#11 lore raw

PATCH v4 7/9 kernel: use new capable_any functionality

In reply to Christian Göttsche

Use the new added capable_any function in appropriate cases, where a task is required to have any of two capabilities.

---
v3:
rename to capable_any()

---
 kernel/fork.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/fork.c +1 -1
index ed4e01daccaa..6e00933e8ef4 100644
@@ -2371,7 +2371,7 @@ __latent_entropy struct task_struct *copy_process(
 	retval = -EAGAIN;
 	if (is_rlimit_overlimit(task_ucounts(p), UCOUNT_RLIMIT_NPROC, rlimit(RLIMIT_NPROC))) {
 		if (p->real_cred->user != INIT_USER &&
-		    !capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN))
+		    !capable_any(CAP_SYS_RESOURCE, CAP_SYS_ADMIN))
 			goto bad_fork_cleanup_count;
 	}
 	current->flags &= ~PF_NPROC_EXCEEDED;
Signature · 1 line
2.40.1
#13 lore raw

PATCH v4 8/9 bpf: use new capable_any functionality

In reply to Christian Göttsche

Use the new added capable_any function in appropriate cases, where a task is required to have any of two capabilities.

---
v3:
rename to capable_any()

---
 kernel/bpf/syscall.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/bpf/syscall.c +1 -1
index 14f39c1e573e..1bd50da05a22 100644
@@ -2539,7 +2539,7 @@ static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr, u32 uattr_size)
 	    !bpf_capable())
 		return -EPERM;
 
-	if (is_net_admin_prog_type(type) && !capable(CAP_NET_ADMIN) && !capable(CAP_SYS_ADMIN))
+	if (is_net_admin_prog_type(type) && !capable_any(CAP_NET_ADMIN, CAP_SYS_ADMIN))
 		return -EPERM;
 	if (is_perfmon_prog_type(type) && !perfmon_capable())
 		return -EPERM;
Signature · 1 line
2.40.1
Acked-by on v4 8/9
#14 lore raw

Re: [PATCH v4 8/9] bpf: use new capable_any functionality

Quoted 4 lines · Christian Göttsche

Use the new added capable_any function in appropriate cases, where a task is required to have any of two capabilities.

Quoted 8 lines

---
v3:
rename to capable_any()

---
 kernel/bpf/syscall.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 16 lines
kernel/bpf/syscall.c +1 -2
index 14f39c1e573e..1bd50da05a22 100644
@@ -2539,7 +2539,7 @@ static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr, u32 uattr_size)
            !bpf_capable())
                return -EPERM;
 
-       if (is_net_admin_prog_type(type) && !capable(CAP_NET_ADMIN) && !capable(CAP_SYS_ADMIN))
+       if (is_net_admin_prog_type(type) && !capable_any(CAP_NET_ADMIN, CAP_SYS_ADMIN))
                return -EPERM;
        if (is_perfmon_prog_type(type) && !perfmon_capable())
                return -EPERM;
--

2.40.1

#15 lore raw

PATCH v4 9/9 net: use new capable_any functionality

In reply to Christian Göttsche

Use the new added capable_any function in appropriate cases, where a task is required to have any of two capabilities.

Add sock_ns_capable_any() wrapper similar to existing sock_ns_capable() one.

Reorder CAP_SYS_ADMIN last.

---
v4:
- introduce sockopt_ns_capable_any()
v3:
- rename to capable_any()
- make use of ns_capable_any

---
 include/net/sock.h       |  1 +
 net/caif/caif_socket.c   |  2 +-
 net/core/sock.c          | 18 ++++++++++--------
 net/ieee802154/socket.c  |  6 ++----
 net/ipv4/ip_sockglue.c   |  4 ++--
 net/ipv6/ipv6_sockglue.c |  3 +--
 net/unix/scm.c           |  2 +-
 7 files changed, 18 insertions(+), 18 deletions(-)
include/net/sock.h +1 -0
index 8b7ed7167243..a17178e31e91 100644
@@ -1762,6 +1762,7 @@ static inline void unlock_sock_fast(struct sock *sk, bool slow)
 void sockopt_lock_sock(struct sock *sk);
 void sockopt_release_sock(struct sock *sk);
 bool sockopt_ns_capable(struct user_namespace *ns, int cap);
+bool sockopt_ns_capable_any(struct user_namespace *ns, int cap1, int cap2);
 bool sockopt_capable(int cap);
 
 /* Used by processes to "lock" a socket state, so that
net/caif/caif_socket.c +1 -1
index 4eebcc66c19a..6dcc08f9da3b 100644
@@ -1027,7 +1027,7 @@ static int caif_create(struct net *net, struct socket *sock, int protocol,
 		.usersize = sizeof_field(struct caifsock, conn_req.param)
 	};
 
-	if (!capable(CAP_SYS_ADMIN) && !capable(CAP_NET_ADMIN))
+	if (!capable_any(CAP_NET_ADMIN, CAP_SYS_ADMIN))
 		return -EPERM;
 	/*
 	 * The sock->type specifies the socket type to use.
net/core/sock.c +10 -8
index 5440e67bcfe3..6a236d649bec 100644
@@ -1073,6 +1073,12 @@ bool sockopt_ns_capable(struct user_namespace *ns, int cap)
 }
 EXPORT_SYMBOL(sockopt_ns_capable);
 
+bool sockopt_ns_capable_any(struct user_namespace *ns, int cap1, int cap2)
+{
+	return has_current_bpf_ctx() || ns_capable_any(ns, cap1, cap2);
+}
+EXPORT_SYMBOL(sockopt_ns_capable_any);
+
 bool sockopt_capable(int cap)
 {
 	return has_current_bpf_ctx() || capable(cap);
@@ -1207,8 +1213,7 @@ int sk_setsockopt(struct sock *sk, int level, int optname,
 
 	case SO_PRIORITY:
 		if ((val >= 0 && val <= 6) ||
-		    sockopt_ns_capable(sock_net(sk)->user_ns, CAP_NET_RAW) ||
-		    sockopt_ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
+		    sockopt_ns_capable_any(sock_net(sk)->user_ns, CAP_NET_RAW, CAP_NET_ADMIN))
 			sk->sk_priority = val;
 		else
 			ret = -EPERM;
@@ -1353,8 +1358,7 @@ int sk_setsockopt(struct sock *sk, int level, int optname,
 			clear_bit(SOCK_PASSSEC, &sock->flags);
 		break;
 	case SO_MARK:
-		if (!sockopt_ns_capable(sock_net(sk)->user_ns, CAP_NET_RAW) &&
-		    !sockopt_ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN)) {
+		if (!sockopt_ns_capable_any(sock_net(sk)->user_ns, CAP_NET_RAW, CAP_NET_ADMIN)) {
 			ret = -EPERM;
 			break;
 		}
@@ -1362,8 +1366,7 @@ int sk_setsockopt(struct sock *sk, int level, int optname,
 		__sock_set_mark(sk, val);
 		break;
 	case SO_RCVMARK:
-		if (!sockopt_ns_capable(sock_net(sk)->user_ns, CAP_NET_RAW) &&
-		    !sockopt_ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN)) {
+		if (!sockopt_ns_capable_any(sock_net(sk)->user_ns, CAP_NET_RAW, CAP_NET_ADMIN)) {
 			ret = -EPERM;
 			break;
 		}
@@ -2747,8 +2750,7 @@ int __sock_cmsg_send(struct sock *sk, struct cmsghdr *cmsg,
 
 	switch (cmsg->cmsg_type) {
 	case SO_MARK:
-		if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_RAW) &&
-		    !ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
+		if (!ns_capable_any(sock_net(sk)->user_ns, CAP_NET_RAW, CAP_NET_ADMIN))
 			return -EPERM;
 		if (cmsg->cmsg_len != CMSG_LEN(sizeof(u32)))
 			return -EINVAL;
net/ieee802154/socket.c +2 -4
index 1fa2fe041ec0..f9bc6cae4af9 100644
@@ -904,8 +904,7 @@ static int dgram_setsockopt(struct sock *sk, int level, int optname,
 		ro->want_lqi = !!val;
 		break;
 	case WPAN_SECURITY:
-		if (!ns_capable(net->user_ns, CAP_NET_ADMIN) &&
-		    !ns_capable(net->user_ns, CAP_NET_RAW)) {
+		if (!ns_capable_any(net->user_ns, CAP_NET_ADMIN, CAP_NET_RAW)) {
 			err = -EPERM;
 			break;
 		}
@@ -928,8 +927,7 @@ static int dgram_setsockopt(struct sock *sk, int level, int optname,
 		}
 		break;
 	case WPAN_SECURITY_LEVEL:
-		if (!ns_capable(net->user_ns, CAP_NET_ADMIN) &&
-		    !ns_capable(net->user_ns, CAP_NET_RAW)) {
+		if (!ns_capable_any(net->user_ns, CAP_NET_ADMIN, CAP_NET_RAW)) {
 			err = -EPERM;
 			break;
 		}
net/ipv4/ip_sockglue.c +2 -2
index b511ff0adc0a..4dd752743b84 100644
@@ -1341,8 +1341,8 @@ int do_ip_setsockopt(struct sock *sk, int level, int optname,
 		break;
 
 	case IP_TRANSPARENT:
-		if (!!val && !sockopt_ns_capable(sock_net(sk)->user_ns, CAP_NET_RAW) &&
-		    !sockopt_ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN)) {
+		if (!!val && !sockopt_ns_capable_any(sock_net(sk)->user_ns, CAP_NET_RAW,
+						     CAP_NET_ADMIN)) {
 			err = -EPERM;
 			break;
 		}
net/ipv6/ipv6_sockglue.c +1 -2
index ae818ff46224..38aad44547e4 100644
@@ -625,8 +625,7 @@ int do_ipv6_setsockopt(struct sock *sk, int level, int optname,
 		break;
 
 	case IPV6_TRANSPARENT:
-		if (valbool && !sockopt_ns_capable(net->user_ns, CAP_NET_RAW) &&
-		    !sockopt_ns_capable(net->user_ns, CAP_NET_ADMIN)) {
+		if (valbool && !sockopt_ns_capable_any(net->user_ns, CAP_NET_RAW, CAP_NET_ADMIN)) {
 			retv = -EPERM;
 			break;
 		}
net/unix/scm.c +1 -1
index f9152881d77f..4d18187a5349 100644
@@ -99,7 +99,7 @@ static inline bool too_many_unix_fds(struct task_struct *p)
 	struct user_struct *user = current_user();
 
 	if (unlikely(user->unix_inflight > task_rlimit(p, RLIMIT_NOFILE)))
-		return !capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN);
+		return !capable_any(CAP_SYS_RESOURCE, CAP_SYS_ADMIN);
 	return false;
 }
 
Signature · 1 line
2.40.1
maintainer Reviewed-by on v4 9/9
#16 lore raw

Re: [PATCH v4 9/9] net: use new capable_any functionality

Hi Christian,

[email protected] wrote on Thu, 11 May 2023 16:25:32 +0200:

Quoted 26 lines

Use the new added capable_any function in appropriate cases, where a task is required to have any of two capabilities.

Add sock_ns_capable_any() wrapper similar to existing sock_ns_capable() one.

Reorder CAP_SYS_ADMIN last.

---
v4:
- introduce sockopt_ns_capable_any()
v3:
- rename to capable_any()
- make use of ns_capable_any

---
 include/net/sock.h       |  1 +
 net/caif/caif_socket.c   |  2 +-
 net/core/sock.c          | 18 ++++++++++--------
 net/ieee802154/socket.c  |  6 ++----
 net/ipv4/ip_sockglue.c   |  4 ++--
 net/ipv6/ipv6_sockglue.c |  3 +--
 net/unix/scm.c           |  2 +-
 7 files changed, 18 insertions(+), 18 deletions(-)

[...]

Quoted 15 lines
net/ieee802154/socket.c +1 -2
index 1fa2fe041ec0..f9bc6cae4af9 100644
@@ -904,8 +904,7 @@ static int dgram_setsockopt(struct sock *sk, int level, int optname,
 		ro->want_lqi = !!val;
 		break;
 	case WPAN_SECURITY:
-		if (!ns_capable(net->user_ns, CAP_NET_ADMIN) &&
-		    !ns_capable(net->user_ns, CAP_NET_RAW)) {
+		if (!ns_capable_any(net->user_ns, CAP_NET_ADMIN, CAP_NET_RAW)) {
 			err = -EPERM;
 			break;
 		}

net/ieee802154/socket.c static int dgram_setsockopt(struct sock *sk, int level, int optname, · line 933

Quoted 9 lines
net/ieee802154/socket.c +1 -2
@@ -928,8 +927,7 @@ static int dgram_setsockopt(struct sock *sk, int level, int optname,
 		}
 		break;
 	case WPAN_SECURITY_LEVEL:
-		if (!ns_capable(net->user_ns, CAP_NET_ADMIN) &&
-		    !ns_capable(net->user_ns, CAP_NET_RAW)) {
+		if (!ns_capable_any(net->user_ns, CAP_NET_ADMIN, CAP_NET_RAW)) {
 			err = -EPERM;
 			break;
 		}

I was not noticed this was applied already, so, for ieee802154:

Thanks,
Miquèl

#17 lore raw

v4 cover Introduce capable_any()

In reply to Christian Göttsche

Add the interfaces capable_any() and ns_capable_any() as an alternative to multiple capable()/ns_capable() calls, like capable_any(CAP_SYS_NICE, CAP_SYS_ADMIN) instead of capable(CAP_SYS_NICE) || capable(CAP_SYS_ADMIN).

capable_any()/ns_capable_any() will in particular generate exactly one audit message, either for the left most capability in effect or, if the task has none, the first one.

This is especially helpful with regard to SELinux, where each audit message about a not allowed capability request will create a denial message. Using this new wrapper with the least invasive capability as left most argument (e.g. CAP_SYS_NICE before CAP_SYS_ADMIN) enables policy writers to only grant the least invasive one for the particular subject instead of both.

v3 discussion:
https://patchwork.kernel.org/project/selinux/patch/[email protected]/

v4:
- add CAP_OPT_NODENYAUDIT capable flag

Christian Göttsche (9):
capability: introduce new capable flag NODENYAUDIT
capability: add any wrapper to test for multiple caps with exactly one
audit message
capability: use new capable_any functionality
block: use new capable_any functionality
drivers: use new capable_any functionality
fs: use new capable_any functionality
kernel: use new capable_any functionality
bpf: use new capable_any functionality
net: use new capable_any functionality

 block/ioprio.c                           |  9 +--
 drivers/gpu/drm/amd/amdkfd/kfd_chardev.c |  3 +-
 drivers/net/caif/caif_serial.c           |  2 +-
 drivers/s390/block/dasd_eckd.c           |  2 +-
 fs/pipe.c                                |  2 +-
 include/linux/capability.h               | 17 ++++--
 include/linux/security.h                 |  2 +
 include/net/sock.h                       |  1 +
 kernel/bpf/syscall.c                     |  2 +-
 kernel/capability.c                      | 70 ++++++++++++++++++++++++
 kernel/fork.c                            |  2 +-
 net/caif/caif_socket.c                   |  2 +-
 net/core/sock.c                          | 18 +++---
 net/ieee802154/socket.c                  |  6 +-
 net/ipv4/ip_sockglue.c                   |  4 +-
 net/ipv6/ipv6_sockglue.c                 |  3 +-
 net/unix/scm.c                           |  2 +-
 security/apparmor/capability.c           |  8 ++-
 security/selinux/hooks.c                 | 14 +++--
 19 files changed, 123 insertions(+), 46 deletions(-)
Signature · 1 line
2.40.1
maintainer Reviewed-by on v4 1/9
#18 lore raw
In reply to Christian Göttsche
Quoted 9 lines · Christian Göttsche

Introduce a new capable flag, CAP_OPT_NODENYAUDIT, to not generate an audit event if the requested capability is not granted. This will be used in a new capable_any() functionality to reduce the number of necessary capable calls.

Handle the flag accordingly in AppArmor and SELinux.

Quoted 66 lines
---
 include/linux/security.h       |  2 ++
 security/apparmor/capability.c |  8 +++++---
 security/selinux/hooks.c       | 14 ++++++++------
 3 files changed, 15 insertions(+), 9 deletions(-)
include/linux/security.h +2 -0
index e2734e9e44d5..629c775ec297 100644
@@ -67,6 +67,8 @@ struct watch_notification;
 #define CAP_OPT_NOAUDIT BIT(1)
 /* If capable is being called by a setid function */
 #define CAP_OPT_INSETID BIT(2)
+/* If capable should audit the security request for authorized requests only */
+#define CAP_OPT_NODENYAUDIT BIT(3)
 
 /* LSM Agnostic defines for security_sb_set_mnt_opts() flags */
 #define SECURITY_LSM_NATIVE_LABELS	1
security/apparmor/capability.c +5 -3
index 326a51838ef2..98120dd62ca7 100644
@@ -108,7 +108,8 @@ static int audit_caps(struct common_audit_data *sa, struct aa_profile *profile,
  * profile_capable - test if profile allows use of capability @cap
  * @profile: profile being enforced    (NOT NULL, NOT unconfined)
  * @cap: capability to test if allowed
- * @opts: CAP_OPT_NOAUDIT bit determines whether audit record is generated
+ * @opts: CAP_OPT_NOAUDIT/CAP_OPT_NODENYAUDIT bit determines whether audit
+ *	record is generated
  * @sa: audit data (MAY BE NULL indicating no auditing)
  *
  * Returns: 0 if allowed else -EPERM
@@ -126,7 +127,7 @@ static int profile_capable(struct aa_profile *profile, int cap,
 	else
 		error = -EPERM;
 
-	if (opts & CAP_OPT_NOAUDIT) {
+	if ((opts & CAP_OPT_NOAUDIT) || ((opts & CAP_OPT_NODENYAUDIT) && error)) {
 		if (!COMPLAIN_MODE(profile))
 			return error;
 		/* audit the cap request in complain mode but note that it
@@ -142,7 +143,8 @@ static int profile_capable(struct aa_profile *profile, int cap,
  * aa_capable - test permission to use capability
  * @label: label being tested for capability (NOT NULL)
  * @cap: capability to be tested
- * @opts: CAP_OPT_NOAUDIT bit determines whether audit record is generated
+ * @opts: CAP_OPT_NOAUDIT/CAP_OPT_NODENYAUDIT bit determines whether audit
+ *	record is generated
  *
  * Look up capability in profile capability set.
  *
security/selinux/hooks.c +1 -1
index 79b4890e9936..0730edf2f5f1 100644
@@ -1571,7 +1571,7 @@ static int cred_has_capability(const struct cred *cred,
 	u16 sclass;
 	u32 sid = cred_sid(cred);
 	u32 av = CAP_TO_MASK(cap);
-	int rc;
+	int rc, rc2;
 
 	ad.type = LSM_AUDIT_DATA_CAP;
 	ad.u.cap = cap;

security/selinux/hooks.c static int cred_has_capability(const struct cred *cred, · line 1594

Quoted 10 lines
security/selinux/hooks.c +2 -5
@@ -1590,11 +1590,13 @@ static int cred_has_capability(const struct cred *cred,
 	}
 
 	rc = avc_has_perm_noaudit(sid, sid, sclass, av, 0, &avd);
-	if (!(opts & CAP_OPT_NOAUDIT)) {
-		int rc2 = avc_audit(sid, sid, sclass, av, &avd, rc, &ad);
-		if (rc2)
-			return rc2;
-	}
+	if ((opts & CAP_OPT_NOAUDIT) || ((opts & CAP_OPT_NODENYAUDIT) && rc))
+		return rc;

Hm, if the caller passes only CAP_OPT_NODENYAUDIT, and rc == 0, then you will audit the allow. Is that what you want, or did you want, or did you want CAP_OPT_NODENYAUDIT to imply CAP_OPT_NOAUDIT?

Quoted 10 lines
security/selinux/hooks.c +5 -0
+
+	rc2 = avc_audit(sid, sid, sclass, av, &avd, rc, &ad);
+	if (rc2)
+		return rc2;
+
 	return rc;
 }
 
Signature · 1 line
2.40.1
maintainer
#19 lore raw
Quoted 6 lines · Serge E. Hallyn
Quoted 5 lines · Christian Göttsche

Introduce a new capable flag, CAP_OPT_NODENYAUDIT, to not generate an audit event if the requested capability is not granted. This will be used in a new capable_any() functionality to reduce the number of necessary capable calls.

Quoted 6 lines
Quoted 4 lines

Handle the flag accordingly in AppArmor and SELinux.

Sorry, obviously I should have removed this, until the comment below was answered :)

Quoted 91 lines
Quoted 66 lines
---
 include/linux/security.h       |  2 ++
 security/apparmor/capability.c |  8 +++++---
 security/selinux/hooks.c       | 14 ++++++++------
 3 files changed, 15 insertions(+), 9 deletions(-)
include/linux/security.h +2 -0
index e2734e9e44d5..629c775ec297 100644
@@ -67,6 +67,8 @@ struct watch_notification;
 #define CAP_OPT_NOAUDIT BIT(1)
 /* If capable is being called by a setid function */
 #define CAP_OPT_INSETID BIT(2)
+/* If capable should audit the security request for authorized requests only */
+#define CAP_OPT_NODENYAUDIT BIT(3)
 
 /* LSM Agnostic defines for security_sb_set_mnt_opts() flags */
 #define SECURITY_LSM_NATIVE_LABELS	1
security/apparmor/capability.c +5 -3
index 326a51838ef2..98120dd62ca7 100644
@@ -108,7 +108,8 @@ static int audit_caps(struct common_audit_data *sa, struct aa_profile *profile,
  * profile_capable - test if profile allows use of capability @cap
  * @profile: profile being enforced    (NOT NULL, NOT unconfined)
  * @cap: capability to test if allowed
- * @opts: CAP_OPT_NOAUDIT bit determines whether audit record is generated
+ * @opts: CAP_OPT_NOAUDIT/CAP_OPT_NODENYAUDIT bit determines whether audit
+ *	record is generated
  * @sa: audit data (MAY BE NULL indicating no auditing)
  *
  * Returns: 0 if allowed else -EPERM
@@ -126,7 +127,7 @@ static int profile_capable(struct aa_profile *profile, int cap,
 	else
 		error = -EPERM;
 
-	if (opts & CAP_OPT_NOAUDIT) {
+	if ((opts & CAP_OPT_NOAUDIT) || ((opts & CAP_OPT_NODENYAUDIT) && error)) {
 		if (!COMPLAIN_MODE(profile))
 			return error;
 		/* audit the cap request in complain mode but note that it
@@ -142,7 +143,8 @@ static int profile_capable(struct aa_profile *profile, int cap,
  * aa_capable - test permission to use capability
  * @label: label being tested for capability (NOT NULL)
  * @cap: capability to be tested
- * @opts: CAP_OPT_NOAUDIT bit determines whether audit record is generated
+ * @opts: CAP_OPT_NOAUDIT/CAP_OPT_NODENYAUDIT bit determines whether audit
+ *	record is generated
  *
  * Look up capability in profile capability set.
  *
security/selinux/hooks.c +1 -1
index 79b4890e9936..0730edf2f5f1 100644
@@ -1571,7 +1571,7 @@ static int cred_has_capability(const struct cred *cred,
 	u16 sclass;
 	u32 sid = cred_sid(cred);
 	u32 av = CAP_TO_MASK(cap);
-	int rc;
+	int rc, rc2;
 
 	ad.type = LSM_AUDIT_DATA_CAP;
 	ad.u.cap = cap;

security/selinux/hooks.c static int cred_has_capability(const struct cred *cred, · line 1594

Quoted 10 lines
security/selinux/hooks.c +2 -5
@@ -1590,11 +1590,13 @@ static int cred_has_capability(const struct cred *cred,
 	}
 
 	rc = avc_has_perm_noaudit(sid, sid, sclass, av, 0, &avd);
-	if (!(opts & CAP_OPT_NOAUDIT)) {
-		int rc2 = avc_audit(sid, sid, sclass, av, &avd, rc, &ad);
-		if (rc2)
-			return rc2;
-	}
+	if ((opts & CAP_OPT_NOAUDIT) || ((opts & CAP_OPT_NODENYAUDIT) && rc))
+		return rc;

Hm, if the caller passes only CAP_OPT_NODENYAUDIT, and rc == 0, then you will audit the allow. Is that what you want, or did you want, or did you want CAP_OPT_NODENYAUDIT to imply CAP_OPT_NOAUDIT?

Quoted 10 lines
security/selinux/hooks.c +5 -0
+
+	rc2 = avc_audit(sid, sid, sclass, av, &avd, rc, &ad);
+	if (rc2)
+		return rc2;
+
 	return rc;
 }
 
Signature · 1 line
2.40.1
#20 lore raw
Quoted 10 lines · Serge E. Hallyn
Quoted 8 lines · Serge E. Hallyn
Quoted 7 lines · Christian Göttsche

Introduce a new capable flag, CAP_OPT_NODENYAUDIT, to not generate an audit event if the requested capability is not granted. This will be used in a new capable_any() functionality to reduce the number of necessary capable calls.

Handle the flag accordingly in AppArmor and SELinux.

Quoted 89 lines
Quoted 4 lines
Quoted 2 lines

Sorry, obviously I should have removed this, until the comment below was answered :)

Quoted 81 lines
Quoted 66 lines
---
 include/linux/security.h       |  2 ++
 security/apparmor/capability.c |  8 +++++---
 security/selinux/hooks.c       | 14 ++++++++------
 3 files changed, 15 insertions(+), 9 deletions(-)
include/linux/security.h +2 -0
index e2734e9e44d5..629c775ec297 100644
@@ -67,6 +67,8 @@ struct watch_notification;
 #define CAP_OPT_NOAUDIT BIT(1)
 /* If capable is being called by a setid function */
 #define CAP_OPT_INSETID BIT(2)
+/* If capable should audit the security request for authorized requests only */
+#define CAP_OPT_NODENYAUDIT BIT(3)
 
 /* LSM Agnostic defines for security_sb_set_mnt_opts() flags */
 #define SECURITY_LSM_NATIVE_LABELS 1
security/apparmor/capability.c +5 -3
index 326a51838ef2..98120dd62ca7 100644
@@ -108,7 +108,8 @@ static int audit_caps(struct common_audit_data *sa, struct aa_profile *profile,
  * profile_capable - test if profile allows use of capability @cap
  * @profile: profile being enforced    (NOT NULL, NOT unconfined)
  * @cap: capability to test if allowed
- * @opts: CAP_OPT_NOAUDIT bit determines whether audit record is generated
+ * @opts: CAP_OPT_NOAUDIT/CAP_OPT_NODENYAUDIT bit determines whether audit
+ * record is generated
  * @sa: audit data (MAY BE NULL indicating no auditing)
  *
  * Returns: 0 if allowed else -EPERM
@@ -126,7 +127,7 @@ static int profile_capable(struct aa_profile *profile, int cap,
    else
            error = -EPERM;
 
-   if (opts & CAP_OPT_NOAUDIT) {
+   if ((opts & CAP_OPT_NOAUDIT) || ((opts & CAP_OPT_NODENYAUDIT) && error)) {
            if (!COMPLAIN_MODE(profile))
                    return error;
            /* audit the cap request in complain mode but note that it
@@ -142,7 +143,8 @@ static int profile_capable(struct aa_profile *profile, int cap,
  * aa_capable - test permission to use capability
  * @label: label being tested for capability (NOT NULL)
  * @cap: capability to be tested
- * @opts: CAP_OPT_NOAUDIT bit determines whether audit record is generated
+ * @opts: CAP_OPT_NOAUDIT/CAP_OPT_NODENYAUDIT bit determines whether audit
+ * record is generated
  *
  * Look up capability in profile capability set.
  *
security/selinux/hooks.c +1 -1
index 79b4890e9936..0730edf2f5f1 100644
@@ -1571,7 +1571,7 @@ static int cred_has_capability(const struct cred *cred,
    u16 sclass;
    u32 sid = cred_sid(cred);
    u32 av = CAP_TO_MASK(cap);
-   int rc;
+   int rc, rc2;
 
    ad.type = LSM_AUDIT_DATA_CAP;
    ad.u.cap = cap;

security/selinux/hooks.c static int cred_has_capability(const struct cred *cred, · line 1594

Quoted 10 lines
security/selinux/hooks.c +2 -5
@@ -1590,11 +1590,13 @@ static int cred_has_capability(const struct cred *cred,
    }
 
    rc = avc_has_perm_noaudit(sid, sid, sclass, av, 0, &avd);
-   if (!(opts & CAP_OPT_NOAUDIT)) {
-           int rc2 = avc_audit(sid, sid, sclass, av, &avd, rc, &ad);
-           if (rc2)
-                   return rc2;
-   }
+   if ((opts & CAP_OPT_NOAUDIT) || ((opts & CAP_OPT_NODENYAUDIT) && rc))
+           return rc;

Hm, if the caller passes only CAP_OPT_NODENYAUDIT, and rc == 0, then you will audit the allow. Is that what you want, or did you want, or did you want CAP_OPT_NODENYAUDIT to imply CAP_OPT_NOAUDIT?

The new option should cause to issue an audit event if (and only if) the requested capability is in effect for the current task. If the task does not have the capability no audit event should be issued.

The new option should not imply CAP_OPT_NOAUDIT since we want an audit event in the case the capability is in effect.

I admit the naming is a bit confusing as CAP_OPT_NODENYAUDIT as well as the commit description contains a double negation (while the inline comment for the macro definition does not).

Do you prefer naming the constant CAP_OPT_ALLOWAUDIT or CAP_OPT_AUDIT_ON_ALLOW?

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
security/selinux/hooks.c +5 -1
+
+   rc2 = avc_audit(sid, sid, sclass, av, &avd, rc, &ad);
+   if (rc2)
+           return rc2;
+
    return rc;
 }
 
--

2.40.1

maintainer
#21 lore raw
Quoted 105 lines · Christian Göttsche
Quoted 10 lines · Serge E. Hallyn
Quoted 8 lines · Serge E. Hallyn
Quoted 7 lines · Christian Göttsche

Introduce a new capable flag, CAP_OPT_NODENYAUDIT, to not generate an audit event if the requested capability is not granted. This will be used in a new capable_any() functionality to reduce the number of necessary capable calls.

Handle the flag accordingly in AppArmor and SELinux.

Quoted 89 lines
Quoted 4 lines
Quoted 2 lines

Sorry, obviously I should have removed this, until the comment below was answered :)

Quoted 81 lines
Quoted 66 lines
---
 include/linux/security.h       |  2 ++
 security/apparmor/capability.c |  8 +++++---
 security/selinux/hooks.c       | 14 ++++++++------
 3 files changed, 15 insertions(+), 9 deletions(-)
include/linux/security.h +2 -0
index e2734e9e44d5..629c775ec297 100644
@@ -67,6 +67,8 @@ struct watch_notification;
 #define CAP_OPT_NOAUDIT BIT(1)
 /* If capable is being called by a setid function */
 #define CAP_OPT_INSETID BIT(2)
+/* If capable should audit the security request for authorized requests only */
+#define CAP_OPT_NODENYAUDIT BIT(3)
 
 /* LSM Agnostic defines for security_sb_set_mnt_opts() flags */
 #define SECURITY_LSM_NATIVE_LABELS 1
security/apparmor/capability.c +5 -3
index 326a51838ef2..98120dd62ca7 100644
@@ -108,7 +108,8 @@ static int audit_caps(struct common_audit_data *sa, struct aa_profile *profile,
  * profile_capable - test if profile allows use of capability @cap
  * @profile: profile being enforced    (NOT NULL, NOT unconfined)
  * @cap: capability to test if allowed
- * @opts: CAP_OPT_NOAUDIT bit determines whether audit record is generated
+ * @opts: CAP_OPT_NOAUDIT/CAP_OPT_NODENYAUDIT bit determines whether audit
+ * record is generated
  * @sa: audit data (MAY BE NULL indicating no auditing)
  *
  * Returns: 0 if allowed else -EPERM
@@ -126,7 +127,7 @@ static int profile_capable(struct aa_profile *profile, int cap,
    else
            error = -EPERM;
 
-   if (opts & CAP_OPT_NOAUDIT) {
+   if ((opts & CAP_OPT_NOAUDIT) || ((opts & CAP_OPT_NODENYAUDIT) && error)) {
            if (!COMPLAIN_MODE(profile))
                    return error;
            /* audit the cap request in complain mode but note that it
@@ -142,7 +143,8 @@ static int profile_capable(struct aa_profile *profile, int cap,
  * aa_capable - test permission to use capability
  * @label: label being tested for capability (NOT NULL)
  * @cap: capability to be tested
- * @opts: CAP_OPT_NOAUDIT bit determines whether audit record is generated
+ * @opts: CAP_OPT_NOAUDIT/CAP_OPT_NODENYAUDIT bit determines whether audit
+ * record is generated
  *
  * Look up capability in profile capability set.
  *
security/selinux/hooks.c +1 -1
index 79b4890e9936..0730edf2f5f1 100644
@@ -1571,7 +1571,7 @@ static int cred_has_capability(const struct cred *cred,
    u16 sclass;
    u32 sid = cred_sid(cred);
    u32 av = CAP_TO_MASK(cap);
-   int rc;
+   int rc, rc2;
 
    ad.type = LSM_AUDIT_DATA_CAP;
    ad.u.cap = cap;

security/selinux/hooks.c static int cred_has_capability(const struct cred *cred, · line 1594

Quoted 10 lines
security/selinux/hooks.c +2 -5
@@ -1590,11 +1590,13 @@ static int cred_has_capability(const struct cred *cred,
    }
 
    rc = avc_has_perm_noaudit(sid, sid, sclass, av, 0, &avd);
-   if (!(opts & CAP_OPT_NOAUDIT)) {
-           int rc2 = avc_audit(sid, sid, sclass, av, &avd, rc, &ad);
-           if (rc2)
-                   return rc2;
-   }
+   if ((opts & CAP_OPT_NOAUDIT) || ((opts & CAP_OPT_NODENYAUDIT) && rc))
+           return rc;

Hm, if the caller passes only CAP_OPT_NODENYAUDIT, and rc == 0, then you will audit the allow. Is that what you want, or did you want, or did you want CAP_OPT_NODENYAUDIT to imply CAP_OPT_NOAUDIT?

The new option should cause to issue an audit event if (and only if) the requested capability is in effect for the current task. If the task does not have the capability no audit event should be issued.

Quoted 8 lines

The new option should not imply CAP_OPT_NOAUDIT since we want an audit event in the case the capability is in effect.

I admit the naming is a bit confusing as CAP_OPT_NODENYAUDIT as well as the commit description contains a double negation (while the inline comment for the macro definition does not).

Do you prefer naming the constant CAP_OPT_ALLOWAUDIT or CAP_OPT_AUDIT_ON_ALLOW?

I think we need a different name, although I'm struggling to think of something ... I don't think ALLOWAUDIT is right, as I believe it implies that it is needed to "allow" auditing to take place for the operation. AUDIT_ON_ALLOW is better, but it still seems like it would be required if you wanted to generate audit records on a successful operation, which isn't correct. I think we need to focus on the idea that the flag blocks auditing for denials.

CAP_OPT_NOAUDITDENY is pretty much what you have, but in my mind the NOAUDITDENY shares enough with the existing NOAUDIT flag that it makes a bit more sense.

I honestly don't know. However, whatever you pick, make sure you update patch 2/X so that the name of ns_capable_nodenyaudit() is kept close to the flag's name.

Signature · 1 line
paul-moore.com
maintainer
#22 lore raw
Quoted 124 lines · Paul Moore
Quoted 105 lines · Christian Göttsche
Quoted 10 lines · Serge E. Hallyn
Quoted 8 lines · Serge E. Hallyn
Quoted 7 lines · Christian Göttsche

Introduce a new capable flag, CAP_OPT_NODENYAUDIT, to not generate an audit event if the requested capability is not granted. This will be used in a new capable_any() functionality to reduce the number of necessary capable calls.

Handle the flag accordingly in AppArmor and SELinux.

Quoted 89 lines
Quoted 4 lines
Quoted 2 lines

Sorry, obviously I should have removed this, until the comment below was answered :)

Quoted 81 lines
Quoted 66 lines
---
 include/linux/security.h       |  2 ++
 security/apparmor/capability.c |  8 +++++---
 security/selinux/hooks.c       | 14 ++++++++------
 3 files changed, 15 insertions(+), 9 deletions(-)
include/linux/security.h +2 -0
index e2734e9e44d5..629c775ec297 100644
@@ -67,6 +67,8 @@ struct watch_notification;
 #define CAP_OPT_NOAUDIT BIT(1)
 /* If capable is being called by a setid function */
 #define CAP_OPT_INSETID BIT(2)
+/* If capable should audit the security request for authorized requests only */
+#define CAP_OPT_NODENYAUDIT BIT(3)
 
 /* LSM Agnostic defines for security_sb_set_mnt_opts() flags */
 #define SECURITY_LSM_NATIVE_LABELS 1
security/apparmor/capability.c +5 -3
index 326a51838ef2..98120dd62ca7 100644
@@ -108,7 +108,8 @@ static int audit_caps(struct common_audit_data *sa, struct aa_profile *profile,
  * profile_capable - test if profile allows use of capability @cap
  * @profile: profile being enforced    (NOT NULL, NOT unconfined)
  * @cap: capability to test if allowed
- * @opts: CAP_OPT_NOAUDIT bit determines whether audit record is generated
+ * @opts: CAP_OPT_NOAUDIT/CAP_OPT_NODENYAUDIT bit determines whether audit
+ * record is generated
  * @sa: audit data (MAY BE NULL indicating no auditing)
  *
  * Returns: 0 if allowed else -EPERM
@@ -126,7 +127,7 @@ static int profile_capable(struct aa_profile *profile, int cap,
    else
            error = -EPERM;
 
-   if (opts & CAP_OPT_NOAUDIT) {
+   if ((opts & CAP_OPT_NOAUDIT) || ((opts & CAP_OPT_NODENYAUDIT) && error)) {
            if (!COMPLAIN_MODE(profile))
                    return error;
            /* audit the cap request in complain mode but note that it
@@ -142,7 +143,8 @@ static int profile_capable(struct aa_profile *profile, int cap,
  * aa_capable - test permission to use capability
  * @label: label being tested for capability (NOT NULL)
  * @cap: capability to be tested
- * @opts: CAP_OPT_NOAUDIT bit determines whether audit record is generated
+ * @opts: CAP_OPT_NOAUDIT/CAP_OPT_NODENYAUDIT bit determines whether audit
+ * record is generated
  *
  * Look up capability in profile capability set.
  *
security/selinux/hooks.c +1 -1
index 79b4890e9936..0730edf2f5f1 100644
@@ -1571,7 +1571,7 @@ static int cred_has_capability(const struct cred *cred,
    u16 sclass;
    u32 sid = cred_sid(cred);
    u32 av = CAP_TO_MASK(cap);
-   int rc;
+   int rc, rc2;
 
    ad.type = LSM_AUDIT_DATA_CAP;
    ad.u.cap = cap;

security/selinux/hooks.c static int cred_has_capability(const struct cred *cred, · line 1594

Quoted 10 lines
security/selinux/hooks.c +2 -5
@@ -1590,11 +1590,13 @@ static int cred_has_capability(const struct cred *cred,
    }
 
    rc = avc_has_perm_noaudit(sid, sid, sclass, av, 0, &avd);
-   if (!(opts & CAP_OPT_NOAUDIT)) {
-           int rc2 = avc_audit(sid, sid, sclass, av, &avd, rc, &ad);
-           if (rc2)
-                   return rc2;
-   }
+   if ((opts & CAP_OPT_NOAUDIT) || ((opts & CAP_OPT_NODENYAUDIT) && rc))
+           return rc;

Hm, if the caller passes only CAP_OPT_NODENYAUDIT, and rc == 0, then you will audit the allow. Is that what you want, or did you want, or did you want CAP_OPT_NODENYAUDIT to imply CAP_OPT_NOAUDIT?

The new option should cause to issue an audit event if (and only if) the requested capability is in effect for the current task. If the task does not have the capability no audit event should be issued.

Quoted 8 lines

The new option should not imply CAP_OPT_NOAUDIT since we want an audit event in the case the capability is in effect.

I admit the naming is a bit confusing as CAP_OPT_NODENYAUDIT as well as the commit description contains a double negation (while the inline comment for the macro definition does not).

Do you prefer naming the constant CAP_OPT_ALLOWAUDIT or CAP_OPT_AUDIT_ON_ALLOW?

I think we need a different name, although I'm struggling to think of something ... I don't think ALLOWAUDIT is right, as I believe it implies that it is needed to "allow" auditing to take place for the operation. AUDIT_ON_ALLOW is better, but it still seems like it would be required if you wanted to generate audit records on a successful operation, which isn't correct. I think we need to focus on the idea that the flag blocks auditing for denials.

Quoted 7 lines

CAP_OPT_NOAUDITDENY is pretty much what you have, but in my mind the NOAUDITDENY shares enough with the existing NOAUDIT flag that it makes a bit more sense.

I honestly don't know. However, whatever you pick, make sure you update patch 2/X so that the name of ns_capable_nodenyaudit() is kept close to the flag's name.

(Sorry for the late response. I still need to fix my filters)

Is CAP_OPT_NOAUDIT_ONDENY or CAP_OPT_AUDIT_ONLY_ONALLOW too long? :)

Anyway, Christian, I leave the final choice to you, then please feel free to add my Reviewed-by.

thanks,
-serge