Skip to contentSkip to conversation

selinux: add netlink nlmsg_type audit message

PATCH Superseded Thiébaud Weksteen 2 participants 4 messages Oct 22, 2024 – Dec 13, 2024

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 selinux: add netlink nlmsg_type audit message

Add a new audit message type to capture nlmsg-related information. This is similar to LSM_AUDIT_DATA_IOCTL_OP which was added for the other SELinux extended permission (ioctl).

Adding a new type is preferred to adding to the existing lsm_network_audit structure which contains irrelevant information for the netlink sockets (i.e., dport, sport).

---
 include/linux/lsm_audit.h | 2 ++
 security/lsm_audit.c      | 3 +++
 security/selinux/hooks.c  | 4 ++--
 3 files changed, 7 insertions(+), 2 deletions(-)
include/linux/lsm_audit.h +2 -0
index 97a8b21eb033..69d2b7bc00ed 100644
@@ -77,6 +77,7 @@ struct common_audit_data {
 #define LSM_AUDIT_DATA_LOCKDOWN 15
 #define LSM_AUDIT_DATA_NOTIFICATION 16
 #define LSM_AUDIT_DATA_ANONINODE	17
+#define LSM_AUDIT_DATA_NLMSGTYPE	18
 	union 	{
 		struct path path;
 		struct dentry *dentry;
@@ -98,6 +99,7 @@ struct common_audit_data {
 		struct lsm_ibendport_audit *ibendport;
 		int reason;
 		const char *anonclass;
+		u16 nlmsg_type;
 	} u;
 	/* this union contains LSM specific data */
 	union {
security/lsm_audit.c +3 -0
index 849e832719e2..b6544024f688 100644
@@ -425,6 +425,9 @@ static void dump_common_audit_data(struct audit_buffer *ab,
 	case LSM_AUDIT_DATA_ANONINODE:
 		audit_log_format(ab, " anonclass=%s", a->u.anonclass);
 		break;
+	case LSM_AUDIT_DATA_NLMSGTYPE:
+		audit_log_format(ab, " nlmsg_type=%hu", a->u.nlmsg_type);
+		break;
 	} /* switch (a->type) */
 }
 
security/selinux/hooks.c +2 -2
index ad3abd48eed1..40afeba0b7de 100644
@@ -5932,14 +5932,14 @@ static int nlmsg_sock_has_extended_perms(struct sock *sk, u32 perms, u16 nlmsg_t
 {
 	struct sk_security_struct *sksec = sk->sk_security;
 	struct common_audit_data ad;
-	struct lsm_network_audit net;
 	u8 driver;
 	u8 xperm;
 
 	if (sock_skip_has_perm(sksec->sid))
 		return 0;
 
-	ad_net_init_from_sk(&ad, &net, sk);
+	ad.type = LSM_AUDIT_DATA_NLMSGTYPE;
+	ad.u.nlmsg_type = nlmsg_type;
 
 	driver = nlmsg_type >> 8;
 	xperm = nlmsg_type & 0xff;
Signature · 1 line
2.47.0.105.g07ac214952-goog
maintainer
#1 lore raw
Quoted 15 lines · On Oct 22, 2024 "=?UTF-8?q?Thi=C3=A9baud=20Weksteen?=" <[email protected]> wrote:

Add a new audit message type to capture nlmsg-related information. This is similar to LSM_AUDIT_DATA_IOCTL_OP which was added for the other SELinux extended permission (ioctl).

Adding a new type is preferred to adding to the existing lsm_network_audit structure which contains irrelevant information for the netlink sockets (i.e., dport, sport).

---
 include/linux/lsm_audit.h | 2 ++
 security/lsm_audit.c      | 3 +++
 security/selinux/hooks.c  | 4 ++--
 3 files changed, 7 insertions(+), 2 deletions(-)

...

security/lsm_audit.c static void dump_common_audit_data(struct audit_buffer *ab, · line 430

Quoted 11 lines
security/lsm_audit.c +3 -0
index 849e832719e2..b6544024f688 100644
@@ -425,6 +425,9 @@ static void dump_common_audit_data(struct audit_buffer *ab,
 	case LSM_AUDIT_DATA_ANONINODE:
 		audit_log_format(ab, " anonclass=%s", a->u.anonclass);
 		break;
+	case LSM_AUDIT_DATA_NLMSGTYPE:
+		audit_log_format(ab, " nlmsg_type=%hu", a->u.nlmsg_type);
+		break;

Based on the audit field dictionary, link below, it appears that netlink related fields follow the "nlnk-XXX" pattern, and while I don't recall any current users in the kernel, it seems like sticking with that pattern is probably a good idea. With that in mind, what do you think about changing "nlmsg_type" into "nlnk-msgtype"?

https://github.com/linux-audit/audit-documentation/blob/main/specs/fields/field-dictionary.csv

security/lsm_audit.c static void dump_common_audit_data(struct audit_buffer *ab, · line 433

Quoted 3 lines
security/lsm_audit.c +0 -0
 	} /* switch (a->type) */
 }
 

--
paul-moore.com

#2 lore raw
Quoted 28 lines · On Oct 22, 2024 "=?UTF-8?q?Thi=C3=A9baud=20Weksteen?=" <[email protected]> wrote:
Quoted 15 lines

Add a new audit message type to capture nlmsg-related information. This is similar to LSM_AUDIT_DATA_IOCTL_OP which was added for the other SELinux extended permission (ioctl).

Adding a new type is preferred to adding to the existing lsm_network_audit structure which contains irrelevant information for the netlink sockets (i.e., dport, sport).

---
 include/linux/lsm_audit.h | 2 ++
 security/lsm_audit.c      | 3 +++
 security/selinux/hooks.c  | 4 ++--
 3 files changed, 7 insertions(+), 2 deletions(-)

...

Quoted 9 lines
security/lsm_audit.c +1 -0
index 849e832719e2..b6544024f688 100644
@@ -425,6 +425,9 @@ static void dump_common_audit_data(struct audit_buffer *ab,
      case LSM_AUDIT_DATA_ANONINODE:
              audit_log_format(ab, " anonclass=%s", a->u.anonclass);
              break;
+     case LSM_AUDIT_DATA_NLMSGTYPE:
Quoted 9 lines

security/lsm_audit.c static void dump_common_audit_data(struct audit_buffer *ab, · line 430

Quoted 2 lines
security/lsm_audit.c +2 -0
+             audit_log_format(ab, " nlmsg_type=%hu", a->u.nlmsg_type);
+             break;

Based on the audit field dictionary, link below, it appears that netlink related fields follow the "nlnk-XXX" pattern, and while I don't recall any current users in the kernel, it seems like sticking with that pattern is probably a good idea. With that in mind, what do you think about changing "nlmsg_type" into "nlnk-msgtype"?

Thanks Paul, I wasn't aware of this list. I found one example of a netlink field in kernel/audit.c (function audit_log_multicast), which was added in commit 9d2161bed4e39. The field is 'nl-mcgrp'. The name was changed from nlnk-grp between v4 and v5 of the patch, but I can't seem to find the reasoning.

Do you know if 'nlnk-fam' and 'nlnk-pid' were deprecated/removed at some point?

I don't mind either way. If you think 'nlnk-msgtype' (or 'nl-msgtype') is more consistent with the other audit fields, I'm happy to send an updated version.

maintainer
#3 lore raw
Quoted 1 line · Thiébaud Weksteen
Quoted 1 line · On Oct 22, 2024 "=?UTF-8?q?Thi=C3=A9baud=20Weksteen?=" <[email protected]> wrote:

...

Quoted 19 lines
Quoted 9 lines
Quoted 9 lines
security/lsm_audit.c +1 -0
index 849e832719e2..b6544024f688 100644
@@ -425,6 +425,9 @@ static void dump_common_audit_data(struct audit_buffer *ab,
      case LSM_AUDIT_DATA_ANONINODE:
              audit_log_format(ab, " anonclass=%s", a->u.anonclass);
              break;
+     case LSM_AUDIT_DATA_NLMSGTYPE:
Quoted 8 lines

security/lsm_audit.c static void dump_common_audit_data(struct audit_buffer *ab, · line 430

Quoted 2 lines
security/lsm_audit.c +2 -0
+             audit_log_format(ab, " nlmsg_type=%hu", a->u.nlmsg_type);
+             break;

Based on the audit field dictionary, link below, it appears that netlink related fields follow the "nlnk-XXX" pattern, and while I don't recall any current users in the kernel, it seems like sticking with that pattern is probably a good idea. With that in mind, what do you think about changing "nlmsg_type" into "nlnk-msgtype"?

Quoted 5 lines

Thanks Paul, I wasn't aware of this list. I found one example of a netlink field in kernel/audit.c (function audit_log_multicast), which was added in commit 9d2161bed4e39. The field is 'nl-mcgrp'. The name was changed from nlnk-grp between v4 and v5 of the patch, but I can't seem to find the reasoning.

I suspect there was a concern about the size of the record. There are some audit folks that are extremely sensitive about audit record sizes due to heavy logging, processing, and retention issues. I'm not overly convinced that a few bytes in an infrequent record are that big of a deal, but who knows ...

The bigger issue is that we have some actual precedence in the code for using "nl-XXX", which seems as good a reason as any to use "nl-msgtype".

Quoted 1 line

Do you know if 'nlnk-fam' and 'nlnk-pid' were deprecated/removed at some point?

I can't say for certain without really digging through the git history. For whatever it is worth, I just did a quick search for "nlnk" in the 'git log -p kernel/audit.c' and didn't see anything.

Quoted 3 lines

I don't mind either way. If you think 'nlnk-msgtype' (or 'nl-msgtype') is more consistent with the other audit fields, I'm happy to send an updated version.

Based on what you found, I think "nl-msgtype" is the right answer, I'll follow up on your v2 patch posting.

Signature · 1 line
paul-moore.com