Skip to contentSkip to conversation

tty_audit: random cleanups

PATCH New Jiri Slaby (SUSE) 1 participant 7 messages Jun 21, 2023 TTY layer and serial drivers

lore
Share & tools
lore

Jiri Slaby (SUSE)

author maintainer
#0 lore raw

cover tty_audit: random cleanups

These are random clean up patches which I came up with while doing more invasive cleanup over the tty layer (to be sent later).

Jiri Slaby (SUSE) (6):
tty_audit: use TASK_COMM_LEN for task comm
tty_audit: use kzalloc() in tty_audit_buf_alloc()
tty_audit: invert the condition in tty_audit_log()
tty_audit: make icanon a bool
tty_audit: make tty pointers in exposed functions const
tty_audit: make data of tty_audit_log() const

 drivers/tty/tty.h       | 11 ++++++-----
 drivers/tty/tty_audit.c | 43 +++++++++++++++++++++--------------------
 2 files changed, 28 insertions(+), 26 deletions(-)
Signature · 1 line
2.41.0
author maintainer
#1 lore raw

PATCH 1/6 tty_audit: use TASK_COMM_LEN for task comm

This is the preferred way of declaring an array for get_task_comm().

---
 drivers/tty/tty_audit.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/tty/tty_audit.c +1 -1
index ca7afd7b2716..b98b1aef5f6f 100644
@@ -69,7 +69,7 @@ static void tty_audit_log(const char *description, dev_t dev,
 
 	ab = audit_log_start(audit_context(), GFP_KERNEL, AUDIT_TTY);
 	if (ab) {
-		char name[sizeof(current->comm)];
+		char name[TASK_COMM_LEN];
 
 		audit_log_format(ab, "%s pid=%u uid=%u auid=%u ses=%u major=%d"
 				 " minor=%d comm=", description, pid, uid,
Signature · 1 line
2.41.0
author maintainer
#2 lore raw

PATCH 2/6 tty_audit: use kzalloc() in tty_audit_buf_alloc()

In reply to Jiri Slaby (SUSE)

tty_audit_buf_alloc() manually erases most of the entries after kmalloc(). So use kzalloc() and remove the manual sets to zero.

That way, we are sure that we do not omit anything.

---
 drivers/tty/tty_audit.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/tty/tty_audit.c +4 -4
index b98b1aef5f6f..43f34465b9df 100644
@@ -33,16 +33,16 @@ static struct tty_audit_buf *tty_audit_buf_alloc(void)
 {
 	struct tty_audit_buf *buf;
 
-	buf = kmalloc(sizeof(*buf), GFP_KERNEL);
+	buf = kzalloc(sizeof(*buf), GFP_KERNEL);
 	if (!buf)
 		goto err;
+
 	buf->data = kmalloc(N_TTY_BUF_SIZE, GFP_KERNEL);
 	if (!buf->data)
 		goto err_buf;
+
 	mutex_init(&buf->mutex);
-	buf->dev = MKDEV(0, 0);
-	buf->icanon = 0;
-	buf->valid = 0;
+
 	return buf;
 
 err_buf:
Signature · 1 line
2.41.0
author maintainer
#3 lore raw

PATCH 3/6 tty_audit: invert the condition in tty_audit_log()

In reply to Jiri Slaby (SUSE)

If we cannot obtain an audit buffer in tty_audit_log(), simply return from the function. Apart this is mostly preferred in the kernel, it allows to merge the split audit string while still keeping it readable.

---
 drivers/tty/tty_audit.c | 24 ++++++++++++------------
 1 file changed, 12 insertions(+), 12 deletions(-)
drivers/tty/tty_audit.c +12 -12
index 43f34465b9df..c6c27acad2ad 100644
@@ -66,20 +66,20 @@ static void tty_audit_log(const char *description, dev_t dev,
 	uid_t uid = from_kuid(&init_user_ns, task_uid(current));
 	uid_t loginuid = from_kuid(&init_user_ns, audit_get_loginuid(current));
 	unsigned int sessionid = audit_get_sessionid(current);
+	char name[TASK_COMM_LEN];
 
 	ab = audit_log_start(audit_context(), GFP_KERNEL, AUDIT_TTY);
-	if (ab) {
-		char name[TASK_COMM_LEN];
-
-		audit_log_format(ab, "%s pid=%u uid=%u auid=%u ses=%u major=%d"
-				 " minor=%d comm=", description, pid, uid,
-				 loginuid, sessionid, MAJOR(dev), MINOR(dev));
-		get_task_comm(name, current);
-		audit_log_untrustedstring(ab, name);
-		audit_log_format(ab, " data=");
-		audit_log_n_hex(ab, data, size);
-		audit_log_end(ab);
-	}
+	if (!ab)
+		return;
+
+	audit_log_format(ab, "%s pid=%u uid=%u auid=%u ses=%u major=%d minor=%d comm=",
+			 description, pid, uid, loginuid, sessionid,
+			 MAJOR(dev), MINOR(dev));
+	get_task_comm(name, current);
+	audit_log_untrustedstring(ab, name);
+	audit_log_format(ab, " data=");
+	audit_log_n_hex(ab, data, size);
+	audit_log_end(ab);
 }
 
 /*
Signature · 1 line
2.41.0
author maintainer
#4 lore raw

PATCH 4/6 tty_audit: make icanon a bool

In reply to Jiri Slaby (SUSE)

Use bool for tty_audit_buf::icanon in favor of ugly bitfields. And get rid of "!!" as that is completely unnecessary.

---
 drivers/tty/tty_audit.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/tty/tty_audit.c +2 -2
index c6c27acad2ad..6cf8b63368ee 100644
@@ -15,7 +15,7 @@
 struct tty_audit_buf {
 	struct mutex mutex;	/* Protects all data below */
 	dev_t dev;		/* The TTY which the data is from */
-	unsigned icanon:1;
+	bool icanon;
 	size_t valid;
 	unsigned char *data;	/* Allocated size N_TTY_BUF_SIZE */
 };
@@ -202,8 +202,8 @@ static struct tty_audit_buf *tty_audit_buf_get(void)
 void tty_audit_add_data(struct tty_struct *tty, const void *data, size_t size)
 {
 	struct tty_audit_buf *buf;
-	unsigned int icanon = !!L_ICANON(tty);
 	unsigned int audit_tty;
+	bool icanon = L_ICANON(tty);
 	dev_t dev;
 
 	audit_tty = READ_ONCE(current->signal->audit_tty);
Signature · 1 line
2.41.0
author maintainer
#5 lore raw

PATCH 5/6 tty_audit: make tty pointers in exposed functions const

In reply to Jiri Slaby (SUSE)

Both tty_audit_add_data() and tty_audit_tiocsti() need only to read from the tty struct, so make the tty parameters of them both const. This aids the compiler a bit.

---
 drivers/tty/tty.h       | 11 ++++++-----
 drivers/tty/tty_audit.c |  5 +++--
 2 files changed, 9 insertions(+), 7 deletions(-)
drivers/tty/tty.h +6 -5
index 1e0d80e98d26..89769a1f1f97 100644
@@ -99,14 +99,15 @@ extern int tty_ldisc_autoload;
 
 /* tty_audit.c */
 #ifdef CONFIG_AUDIT
-void tty_audit_add_data(struct tty_struct *tty, const void *data, size_t size);
-void tty_audit_tiocsti(struct tty_struct *tty, char ch);
+void tty_audit_add_data(const struct tty_struct *tty, const void *data,
+			size_t size);
+void tty_audit_tiocsti(const struct tty_struct *tty, char ch);
 #else
-static inline void tty_audit_add_data(struct tty_struct *tty, const void *data,
-				      size_t size)
+static inline void tty_audit_add_data(const struct tty_struct *tty,
+				      const void *data, size_t size)
 {
 }
-static inline void tty_audit_tiocsti(struct tty_struct *tty, char ch)
+static inline void tty_audit_tiocsti(const struct tty_struct *tty, char ch)
 {
 }
 #endif
drivers/tty/tty_audit.c +3 -2
index 6cf8b63368ee..5cbe28ac1763 100644
@@ -134,7 +134,7 @@ void tty_audit_fork(struct signal_struct *sig)
 /*
  *	tty_audit_tiocsti	-	Log TIOCSTI
  */
-void tty_audit_tiocsti(struct tty_struct *tty, char ch)
+void tty_audit_tiocsti(const struct tty_struct *tty, char ch)
 {
 	dev_t dev;
 
@@ -199,7 +199,8 @@ static struct tty_audit_buf *tty_audit_buf_get(void)
  *
  *	Audit @data of @size from @tty, if necessary.
  */
-void tty_audit_add_data(struct tty_struct *tty, const void *data, size_t size)
+void tty_audit_add_data(const struct tty_struct *tty, const void *data,
+			size_t size)
 {
 	struct tty_audit_buf *buf;
 	unsigned int audit_tty;
Signature · 1 line
2.41.0
author maintainer
#6 lore raw

PATCH 6/6 tty_audit: make data of tty_audit_log() const

In reply to Jiri Slaby (SUSE)

'data' are only read (passed down to audit_log_n_hex()), so they can be const -- the same what is expected in audit_log_n_hex(). Only a minor cleanup to be consistent.

---
 drivers/tty/tty_audit.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/tty/tty_audit.c +1 -1
index 5cbe28ac1763..24d010589379 100644
@@ -59,7 +59,7 @@ static void tty_audit_buf_free(struct tty_audit_buf *buf)
 }
 
 static void tty_audit_log(const char *description, dev_t dev,
-			  unsigned char *data, size_t size)
+			  const unsigned char *data, size_t size)
 {
 	struct audit_buffer *ab;
 	pid_t pid = task_pid_nr(current);
Signature · 1 line
2.41.0