Skip to contentSkip to conversation

- audit cmdline on events

PATCH New William Roberts 2 participants 8 messages Nov 19, 2013

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

PATCH 1/1 - audit cmdline on events

Draft versions of some work I have been doing auditing the cmdline value on events. The reason for this, is that I need to get the package name in Android in the audit records. Often times, the app dies before userspace would be able to get it from procfs.

I'll (attempt) to summarize the feedback so far.

* RGB - Can we make this dynamic?
** This was nak'd by Steve Grubb and subsequently dropped from these patches.

* Stephen Smalley - Can we cache this in audit struct for performance concerns? ** I think I address this in patch 2

* Steve Grubb - Is cmdline generic enough? Should we extend prctl for an extended comm field?
** The heart of the matter is some spot the process can stick
more than 16 chars of data. I think this meets that, without having to modify prctl.

* Steve Grubb - Can you use a user audit record?
** I can, but the downside is that it doesnt
keep the same id with the related issues, you
have to combine them by hand, by pid. Doesn't
seem like a generic solution.

Right now, the cache never gets invalidated, as their is no kernel interface on which to invalidate the cache on. This would be one win for adding to prctl.

Once we have a clear way forward on this, I can make the effort to port to master.

[PATCH 1/2] audit: Allow auditing of proc/self/cmdline value [PATCH 2/2] audit: Enable cacheing of cmdline in audit_context

#1 lore raw

PATCH 1/2 audit: Allow auditing of proc/self/cmdline value

Audit records will now contain a new field, cmdline. This is the value that is stored in proc/self/cmdline, and is useful for debugging when processes are being run via VM's. A primary example of this is Android, in which package names are set in this location, and thread names are set via PR_SET_NAME. The other benefit is this is not limited to 16 bytes as COMM historically has.

Change-Id I9bf0928a8aa249d22ecd55fa9cd27325dd394eb1
---
 fs/proc/base.c          |    2 +-
 include/linux/proc_fs.h |    1 +
 kernel/auditsc.c        |   33 +++++++++++++++++++++++++++++++++
 3 files changed, 35 insertions(+), 1 deletion(-)
fs/proc/base.c +1 -1
index 2f198da..25b73d3 100644
@@ -209,7 +209,7 @@ struct mm_struct *mm_for_maps(struct task_struct *task)
 	return mm_access(task, PTRACE_MODE_READ);
 }
 
-static int proc_pid_cmdline(struct task_struct *task, char * buffer)
+int proc_pid_cmdline(struct task_struct *task, char *buffer)
 {
 	int res = 0;
 	unsigned int len;
include/linux/proc_fs.h +1 -0
index 85c5073..d85ac14 100644
@@ -118,6 +118,7 @@ struct pid_namespace;
 
 extern int pid_ns_prepare_proc(struct pid_namespace *ns);
 extern void pid_ns_release_proc(struct pid_namespace *ns);
+extern int proc_pid_cmdline(struct task_struct *task, char *buffer);
 
 /*
  * proc_tty.c
kernel/auditsc.c +33 -0
index 27ad9dd..45fd3d0 100644
@@ -67,6 +67,7 @@
 #include <linux/syscalls.h>
 #include <linux/capability.h>
 #include <linux/fs_struct.h>
+#include <linux/proc_fs.h>
 
 #include "audit.h"
 
@@ -1153,6 +1154,37 @@ error_path:
 
 EXPORT_SYMBOL(audit_log_task_context);
 
+static void audit_log_add_cmdline(struct audit_buffer *ab,
+				  struct task_struct *tsk)
+{
+	int len;
+	unsigned long page;
+	char *msg = "(null)";
+
+	audit_log_format(ab, " cmdline=");
+
+	/* Get the process cmdline */
+	page = __get_free_page(GFP_TEMPORARY);
+	if (!page) {
+		audit_log_untrustedstring(ab, msg);
+		return;
+	}
+	len = proc_pid_cmdline(tsk, (char *)page);
+	if (len <= 0) {
+		free_page(page);
+		audit_log_untrustedstring(ab, msg);
+		return;
+	}
+	/*
+	 * Ensure NULL terminated! Application could
+	 * could be using setproctitle(3).
+	 */
+	((char *)page)[len-1] = '\0';
+	msg = (char *)page;
+	audit_log_untrustedstring(ab, msg);
+	free_page(page);
+}
+
 static void audit_log_task_info(struct audit_buffer *ab, struct task_struct *tsk)
 {
 	char name[sizeof(tsk->comm)];
@@ -1179,6 +1211,7 @@ static void audit_log_task_info(struct audit_buffer *ab, struct task_struct *tsk
 		}
 		up_read(&mm->mmap_sem);
 	}
+	audit_log_add_cmdline(ab, tsk);
 	audit_log_task_context(ab);
 }
 
Signature · 1 line
1.7.9.5
#2 lore raw

Re: [PATCH 1/2] audit: Allow auditing of proc/self/cmdline value

Quoted 7 lines · William Roberts

Audit records will now contain a new field, cmdline. This is the value that is stored in proc/self/cmdline, and is useful for debugging when processes are being run via VM's. A primary example of this is Android, in which package names are set in this location, and thread names are set via PR_SET_NAME. The other benefit is this is not limited to 16 bytes as COMM historically has.

This patch looks good to me.

Quoted 94 lines
Change-Id I9bf0928a8aa249d22ecd55fa9cd27325dd394eb1
---
 fs/proc/base.c          |    2 +-
 include/linux/proc_fs.h |    1 +
 kernel/auditsc.c        |   33 +++++++++++++++++++++++++++++++++
 3 files changed, 35 insertions(+), 1 deletion(-)
fs/proc/base.c +1 -1
index 2f198da..25b73d3 100644
@@ -209,7 +209,7 @@ struct mm_struct *mm_for_maps(struct task_struct *task)
 	return mm_access(task, PTRACE_MODE_READ);
 }
 
-static int proc_pid_cmdline(struct task_struct *task, char * buffer)
+int proc_pid_cmdline(struct task_struct *task, char *buffer)
 {
 	int res = 0;
 	unsigned int len;
include/linux/proc_fs.h +1 -0
index 85c5073..d85ac14 100644
@@ -118,6 +118,7 @@ struct pid_namespace;
 
 extern int pid_ns_prepare_proc(struct pid_namespace *ns);
 extern void pid_ns_release_proc(struct pid_namespace *ns);
+extern int proc_pid_cmdline(struct task_struct *task, char *buffer);
 
 /*
  * proc_tty.c
kernel/auditsc.c +33 -0
index 27ad9dd..45fd3d0 100644
@@ -67,6 +67,7 @@
 #include <linux/syscalls.h>
 #include <linux/capability.h>
 #include <linux/fs_struct.h>
+#include <linux/proc_fs.h>
 
 #include "audit.h"
 
@@ -1153,6 +1154,37 @@ error_path:
 
 EXPORT_SYMBOL(audit_log_task_context);
 
+static void audit_log_add_cmdline(struct audit_buffer *ab,
+				  struct task_struct *tsk)
+{
+	int len;
+	unsigned long page;
+	char *msg = "(null)";
+
+	audit_log_format(ab, " cmdline=");
+
+	/* Get the process cmdline */
+	page = __get_free_page(GFP_TEMPORARY);
+	if (!page) {
+		audit_log_untrustedstring(ab, msg);
+		return;
+	}
+	len = proc_pid_cmdline(tsk, (char *)page);
+	if (len <= 0) {
+		free_page(page);
+		audit_log_untrustedstring(ab, msg);
+		return;
+	}
+	/*
+	 * Ensure NULL terminated! Application could
+	 * could be using setproctitle(3).
+	 */
+	((char *)page)[len-1] = '\0';
+	msg = (char *)page;
+	audit_log_untrustedstring(ab, msg);
+	free_page(page);
+}
+
 static void audit_log_task_info(struct audit_buffer *ab, struct task_struct *tsk)
 {
 	char name[sizeof(tsk->comm)];
@@ -1179,6 +1211,7 @@ static void audit_log_task_info(struct audit_buffer *ab, struct task_struct *tsk
 		}
 		up_read(&mm->mmap_sem);
 	}
+	audit_log_add_cmdline(ab, tsk);
 	audit_log_task_context(ab);
 }
 
Signature · 1 line
1.7.9.5

- RGB

--
Richard Guy Briggs <[email protected]>
Senior Software Engineer, Kernel Security, AMER ENG Base Operating Systems, Red Hat Remote, Ottawa, Canada
Voice: +1.647.777.2635, Internal: (81) 32635, Alt: +1.613.693.0684x3545

#3 lore raw

PATCH 2/2 audit: Enable cacheing of cmdline in audit_context

In reply to William Roberts

Not enitrely sure if this is getting us any benefit, as in my environment, these contexts are getting free'd immediately.

Change-Id Ia0d432bc4aba8588840f0dc0026a1e9483e5b485
---
 kernel/auditsc.c |   48 +++++++++++++++++++++++++++++++++++++-----------
 1 file changed, 37 insertions(+), 11 deletions(-)
kernel/auditsc.c +37 -11
index 45fd3d0..4b30c5d 100644
@@ -270,6 +270,7 @@ struct audit_context {
 		} mmap;
 	};
 	int fds[2];
+	unsigned long cmdline;
 
 #if AUDIT_DEBUG
 	int		    put_count;
@@ -1044,6 +1045,14 @@ static inline void audit_free_aux(struct audit_context *context)
 	}
 }
 
+static inline void audit_cmdline_free(struct audit_context *ctx)
+{
+	if (!ctx->cmdline)
+		return;
+	free_page(ctx->cmdline);
+	ctx->cmdline = 0;
+}
+
 static inline void audit_zero_context(struct audit_context *context,
 				      enum audit_state state)
 {
@@ -1118,6 +1127,7 @@ static inline void audit_free_context(struct audit_context *context)
 		audit_free_aux(context);
 		kfree(context->filterkey);
 		kfree(context->sockaddr);
+		audit_cmdline_free(context);
 		kfree(context);
 		context  = previous;
 	} while (context);
@@ -1154,35 +1164,51 @@ error_path:
 
 EXPORT_SYMBOL(audit_log_task_context);
 
-static void audit_log_add_cmdline(struct audit_buffer *ab,
+static unsigned long audit_cmdline_get_page(struct audit_buffer *ab,
 				  struct task_struct *tsk)
 {
 	int len;
 	unsigned long page;
-	char *msg = "(null)";
-
-	audit_log_format(ab, " cmdline=");
 
 	/* Get the process cmdline */
 	page = __get_free_page(GFP_TEMPORARY);
 	if (!page) {
-		audit_log_untrustedstring(ab, msg);
-		return;
+		return 0;
 	}
 	len = proc_pid_cmdline(tsk, (char *)page);
 	if (len <= 0) {
 		free_page(page);
-		audit_log_untrustedstring(ab, msg);
-		return;
+		return 0;
 	}
 	/*
 	 * Ensure NULL terminated! Application could
 	 * could be using setproctitle(3).
 	 */
 	((char *)page)[len-1] = '\0';
-	msg = (char *)page;
+
+	/* TODO: Re-alloc to something smaller then a page here? */
+	return page;
+}
+
+static void audit_log_cmdline(struct audit_buffer *ab, struct task_struct *tsk,
+			      struct audit_context *context)
+{
+	char *msg = "(null)";
+
+	audit_log_format(ab, " cmdline=");
+
+	/* Already cached */
+	if (context->cmdline) {
+		msg = (char *)context->cmdline;
+		goto out;
+	}
+	/* Not cached yet */
+	context->cmdline = audit_cmdline_get_page(ab, tsk);
+	if (!context->cmdline)
+		goto out;
+	msg = (char *)context->cmdline;
+out:
 	audit_log_untrustedstring(ab, msg);
-	free_page(page);
 }
 
 static void audit_log_task_info(struct audit_buffer *ab, struct task_struct *tsk)
@@ -1211,7 +1237,6 @@ static void audit_log_task_info(struct audit_buffer *ab, struct task_struct *tsk
 		}
 		up_read(&mm->mmap_sem);
 	}
-	audit_log_add_cmdline(ab, tsk);
 	audit_log_task_context(ab);
 }
 
@@ -1679,6 +1704,7 @@ static void audit_log_exit(struct audit_context *context, struct task_struct *ts
 
 
 	audit_log_task_info(ab, tsk);
+	audit_log_cmdline(ab, tsk, context);
 	audit_log_key(ab, context->filterkey);
 	audit_log_end(ab);
 
Signature · 1 line
1.7.9.5
#4 lore raw

Re: [PATCH 2/2] audit: Enable cacheing of cmdline in audit_context

Quoted 14 lines · William Roberts

Not enitrely sure if this is getting us any benefit, as in my environment, these contexts are getting free'd immediately.

Change-Id Ia0d432bc4aba8588840f0dc0026a1e9483e5b485
---
 kernel/auditsc.c |   48 +++++++++++++++++++++++++++++++++++++-----------
 1 file changed, 37 insertions(+), 11 deletions(-)
kernel/auditsc.c +0 -0
index 45fd3d0..4b30c5d 100644

kernel/auditsc.c struct audit_context { · line 273

Quoted 4 lines
kernel/auditsc.c +1 -0
@@ -270,6 +270,7 @@ struct audit_context {
 		} mmap;
 	};
 	int fds[2];
+	unsigned long cmdline;

Would this be better declared to avoid all the casts?:

	char *cmdline;

kernel/auditsc.c static inline void audit_free_aux(struct audit_context *context) · line 1053

Quoted 12 lines
kernel/auditsc.c +6 -0
 #if AUDIT_DEBUG
 	int		    put_count;
@@ -1044,6 +1045,14 @@ static inline void audit_free_aux(struct audit_context *context)
 	}
 }
 
+static inline void audit_cmdline_free(struct audit_context *ctx)
+{
+	if (!ctx->cmdline)
+		return;
+	free_page(ctx->cmdline);
+	ctx->cmdline = 0;
	ctx->cmdline = NULL;
Quoted 14 lines
kernel/auditsc.c +3 -0
+}
+
 static inline void audit_zero_context(struct audit_context *context,
 				      enum audit_state state)
 {
@@ -1118,6 +1127,7 @@ static inline void audit_free_context(struct audit_context *context)
 		audit_free_aux(context);
 		kfree(context->filterkey);
 		kfree(context->sockaddr);
+		audit_cmdline_free(context);
 		kfree(context);
 		context  = previous;
 	} while (context);

kernel/auditsc.c error_path: · line 1167

Quoted 5 lines
kernel/auditsc.c +1 -1
@@ -1154,35 +1164,51 @@ error_path:
 
 EXPORT_SYMBOL(audit_log_task_context);
 
-static void audit_log_add_cmdline(struct audit_buffer *ab,
+static unsigned long audit_cmdline_get_page(struct audit_buffer *ab,

static char* audit_cmdline_get_page(struct audit_buffer *ab,

Quoted 4 lines
kernel/auditsc.c +0 -0
 				  struct task_struct *tsk)
 {
 	int len;
 	unsigned long page;

kernel/auditsc.c error_path: · line 1176

Quoted 10 lines
kernel/auditsc.c +1 -5
-	char *msg = "(null)";
-
-	audit_log_format(ab, " cmdline=");
 
 	/* Get the process cmdline */
 	page = __get_free_page(GFP_TEMPORARY);
 	if (!page) {
-		audit_log_untrustedstring(ab, msg);
-		return;
+		return 0;
		return NULL;

kernel/auditsc.c error_path: · line 1181

Quoted 7 lines
kernel/auditsc.c +1 -2
 	}
 	len = proc_pid_cmdline(tsk, (char *)page);
 	if (len <= 0) {
 		free_page(page);
-		audit_log_untrustedstring(ab, msg);
-		return;
+		return 0;
		return NULL;
Quoted 12 lines
kernel/auditsc.c +5 -1
 	}
 	/*
 	 * Ensure NULL terminated! Application could
 	 * could be using setproctitle(3).
 	 */
 	((char *)page)[len-1] = '\0';
-	msg = (char *)page;
+
+	/* TODO: Re-alloc to something smaller then a page here? */
+	return page;
+}
+

kernel/auditsc.c error_path: · line 1202

Quoted 10 lines
kernel/auditsc.c +10 -0
+static void audit_log_cmdline(struct audit_buffer *ab, struct task_struct *tsk,
+			      struct audit_context *context)
+{
+	char *msg = "(null)";
+
+	audit_log_format(ab, " cmdline=");
+
+	/* Already cached */
+	if (context->cmdline) {
+		msg = (char *)context->cmdline;
		msg = context->cmdline;

kernel/auditsc.c error_path: · line 1209

Quoted 7 lines
kernel/auditsc.c +7 -0
+		goto out;
+	}
+	/* Not cached yet */
+	context->cmdline = audit_cmdline_get_page(ab, tsk);
+	if (!context->cmdline)
+		goto out;
+	msg = (char *)context->cmdline;
	msg = context->cmdline;
Quoted 25 lines
kernel/auditsc.c +2 -2
+out:
 	audit_log_untrustedstring(ab, msg);
-	free_page(page);
 }
 
 static void audit_log_task_info(struct audit_buffer *ab, struct task_struct *tsk)
@@ -1211,7 +1237,6 @@ static void audit_log_task_info(struct audit_buffer *ab, struct task_struct *tsk
 		}
 		up_read(&mm->mmap_sem);
 	}
-	audit_log_add_cmdline(ab, tsk);
 	audit_log_task_context(ab);
 }
 
@@ -1679,6 +1704,7 @@ static void audit_log_exit(struct audit_context *context, struct task_struct *ts
 
 
 	audit_log_task_info(ab, tsk);
+	audit_log_cmdline(ab, tsk, context);
 	audit_log_key(ab, context->filterkey);
 	audit_log_end(ab);
 
Signature · 1 line
1.7.9.5

- RGB

--
Richard Guy Briggs <[email protected]>
Senior Software Engineer, Kernel Security, AMER ENG Base Operating Systems, Red Hat Remote, Ottawa, Canada
Voice: +1.647.777.2635, Internal: (81) 32635, Alt: +1.613.693.0684x3545

#5 lore raw

Re: [PATCH 2/2] audit: Enable cacheing of cmdline in audit_context

Quoted 10 lines · Richard Guy Briggs
Quoted 9 lines · William Roberts

Not enitrely sure if this is getting us any benefit, as in my environment, these contexts are getting free'd immediately.

Change-Id Ia0d432bc4aba8588840f0dc0026a1e9483e5b485
---
 kernel/auditsc.c |   48 +++++++++++++++++++++++++++++++++++++-----------
 1 file changed, 37 insertions(+), 11 deletions(-)
Quoted 11 lines

kernel/auditsc.c struct audit_context { · line 273

Quoted 9 lines
kernel/auditsc.c +1 -0
index 45fd3d0..4b30c5d 100644
@@ -270,6 +270,7 @@ struct audit_context {
              } mmap;
      };
      int fds[2];
+     unsigned long cmdline;

Would this be better declared to avoid all the casts?:

This is indirectly coupled with my todo as well. So the value could be up to a page big, and I allocate and cache a page (which is returned as an usnigned long).
But, often times, the value is much smaller. Should I just alloc a seperate and smaller buffer with kmalloc, and
cache that as a char * to avoid the casts? Then free the page?

Either way, I have no objection to taking the interface to a char *, I battled that decision in my head while I was coding.

Quoted 135 lines
        char *cmdline;

kernel/auditsc.c static inline void audit_free_aux(struct audit_context *context) · line 1053

Quoted 12 lines
kernel/auditsc.c +6 -0
 #if AUDIT_DEBUG
      int                 put_count;
@@ -1044,6 +1045,14 @@ static inline void audit_free_aux(struct audit_context *context)
      }
 }
 
+static inline void audit_cmdline_free(struct audit_context *ctx)
+{
+     if (!ctx->cmdline)
+             return;
+     free_page(ctx->cmdline);
+     ctx->cmdline = 0;
        ctx->cmdline = NULL;
Quoted 14 lines
kernel/auditsc.c +3 -0
+}
+
 static inline void audit_zero_context(struct audit_context *context,
                                    enum audit_state state)
 {
@@ -1118,6 +1127,7 @@ static inline void audit_free_context(struct audit_context *context)
              audit_free_aux(context);
              kfree(context->filterkey);
              kfree(context->sockaddr);
+             audit_cmdline_free(context);
              kfree(context);
              context  = previous;
      } while (context);

kernel/auditsc.c error_path: · line 1167

Quoted 5 lines
kernel/auditsc.c +1 -1
@@ -1154,35 +1164,51 @@ error_path:
 
 EXPORT_SYMBOL(audit_log_task_context);
 
-static void audit_log_add_cmdline(struct audit_buffer *ab,
+static unsigned long audit_cmdline_get_page(struct audit_buffer *ab,

static char* audit_cmdline_get_page(struct audit_buffer *ab,

Quoted 4 lines
kernel/auditsc.c +0 -0
                                struct task_struct *tsk)
 {
      int len;
      unsigned long page;

kernel/auditsc.c error_path: · line 1176

Quoted 10 lines
kernel/auditsc.c +1 -5
-     char *msg = "(null)";
-
-     audit_log_format(ab, " cmdline=");
 
      /* Get the process cmdline */
      page = __get_free_page(GFP_TEMPORARY);
      if (!page) {
-             audit_log_untrustedstring(ab, msg);
-             return;
+             return 0;
                return NULL;

kernel/auditsc.c error_path: · line 1181

Quoted 7 lines
kernel/auditsc.c +1 -2
      }
      len = proc_pid_cmdline(tsk, (char *)page);
      if (len <= 0) {
              free_page(page);
-             audit_log_untrustedstring(ab, msg);
-             return;
+             return 0;
                return NULL;
Quoted 12 lines
kernel/auditsc.c +5 -1
      }
      /*
       * Ensure NULL terminated! Application could
       * could be using setproctitle(3).
       */
      ((char *)page)[len-1] = '\0';
-     msg = (char *)page;
+
+     /* TODO: Re-alloc to something smaller then a page here? */
+     return page;
+}
+

kernel/auditsc.c error_path: · line 1202

Quoted 10 lines
kernel/auditsc.c +10 -0
+static void audit_log_cmdline(struct audit_buffer *ab, struct task_struct *tsk,
+                           struct audit_context *context)
+{
+     char *msg = "(null)";
+
+     audit_log_format(ab, " cmdline=");
+
+     /* Already cached */
+     if (context->cmdline) {
+             msg = (char *)context->cmdline;
                msg = context->cmdline;

kernel/auditsc.c error_path: · line 1209

Quoted 7 lines
kernel/auditsc.c +7 -0
+             goto out;
+     }
+     /* Not cached yet */
+     context->cmdline = audit_cmdline_get_page(ab, tsk);
+     if (!context->cmdline)
+             goto out;
+     msg = (char *)context->cmdline;
        msg = context->cmdline;
Quoted 23 lines
kernel/auditsc.c +2 -3
+out:
      audit_log_untrustedstring(ab, msg);
-     free_page(page);
 }
 
 static void audit_log_task_info(struct audit_buffer *ab, struct task_struct *tsk)
@@ -1211,7 +1237,6 @@ static void audit_log_task_info(struct audit_buffer *ab, struct task_struct *tsk
              }
              up_read(&mm->mmap_sem);
      }
-     audit_log_add_cmdline(ab, tsk);
      audit_log_task_context(ab);
 }
 
@@ -1679,6 +1704,7 @@ static void audit_log_exit(struct audit_context *context, struct task_struct *ts
 
 
      audit_log_task_info(ab, tsk);
+     audit_log_cmdline(ab, tsk, context);
      audit_log_key(ab, context->filterkey);
      audit_log_end(ab);
 
--
Quoted 2 lines

1.7.9.5

- RGB

--
Richard Guy Briggs <[email protected]>
Senior Software Engineer, Kernel Security, AMER ENG Base Operating Systems, Red Hat Remote, Ottawa, Canada
Voice: +1.647.777.2635, Internal: (81) 32635, Alt: +1.613.693.0684x3545

Signature · 3 lines
Respectfully,

William C Roberts
#6 lore raw

Re: [PATCH 2/2] audit: Enable cacheing of cmdline in audit_context

Quoted 23 lines · William Roberts
Quoted 10 lines · Richard Guy Briggs
Quoted 9 lines · William Roberts

Not enitrely sure if this is getting us any benefit, as in my environment, these contexts are getting free'd immediately.

Change-Id Ia0d432bc4aba8588840f0dc0026a1e9483e5b485
---
 kernel/auditsc.c |   48 +++++++++++++++++++++++++++++++++++++-----------
 1 file changed, 37 insertions(+), 11 deletions(-)
Quoted 11 lines

kernel/auditsc.c struct audit_context { · line 273

Quoted 9 lines
kernel/auditsc.c +1 -0
index 45fd3d0..4b30c5d 100644
@@ -270,6 +270,7 @@ struct audit_context {
              } mmap;
      };
      int fds[2];
+     unsigned long cmdline;

Would this be better declared to avoid all the casts?:

Quoted 6 lines

This is indirectly coupled with my todo as well. So the value could be up to a page big, and I allocate and cache a page (which is returned as an usnigned long).
But, often times, the value is much smaller. Should I just alloc a seperate and smaller buffer with kmalloc, and
cache that as a char * to avoid the casts? Then free the page?

Do you know the size ahead of time? If so, just allocate that.

Quoted 2 lines

Either way, I have no objection to taking the interface to a char *, I battled that decision in my head while I was coding.

Perhaps I am being naive, but is there any benefit to referring to a page as an unsigned long rather than as a char*? If it is only ever used as a char* (except by free_page()) can it hurt?

For that matter, is there a benefit to doing a __get_free_page() rather than kmalloc()?

Quoted 129 lines
Quoted 129 lines
        char *cmdline;

kernel/auditsc.c static inline void audit_free_aux(struct audit_context *context) · line 1053

Quoted 12 lines
kernel/auditsc.c +6 -0
 #if AUDIT_DEBUG
      int                 put_count;
@@ -1044,6 +1045,14 @@ static inline void audit_free_aux(struct audit_context *context)
      }
 }
 
+static inline void audit_cmdline_free(struct audit_context *ctx)
+{
+     if (!ctx->cmdline)
+             return;
+     free_page(ctx->cmdline);
+     ctx->cmdline = 0;
        ctx->cmdline = NULL;
Quoted 14 lines
kernel/auditsc.c +3 -0
+}
+
 static inline void audit_zero_context(struct audit_context *context,
                                    enum audit_state state)
 {
@@ -1118,6 +1127,7 @@ static inline void audit_free_context(struct audit_context *context)
              audit_free_aux(context);
              kfree(context->filterkey);
              kfree(context->sockaddr);
+             audit_cmdline_free(context);
              kfree(context);
              context  = previous;
      } while (context);

kernel/auditsc.c error_path: · line 1167

Quoted 5 lines
kernel/auditsc.c +1 -1
@@ -1154,35 +1164,51 @@ error_path:
 
 EXPORT_SYMBOL(audit_log_task_context);
 
-static void audit_log_add_cmdline(struct audit_buffer *ab,
+static unsigned long audit_cmdline_get_page(struct audit_buffer *ab,

static char* audit_cmdline_get_page(struct audit_buffer *ab,

Quoted 4 lines
kernel/auditsc.c +0 -0
                                struct task_struct *tsk)
 {
      int len;
      unsigned long page;

kernel/auditsc.c error_path: · line 1176

Quoted 10 lines
kernel/auditsc.c +1 -5
-     char *msg = "(null)";
-
-     audit_log_format(ab, " cmdline=");
 
      /* Get the process cmdline */
      page = __get_free_page(GFP_TEMPORARY);
      if (!page) {
-             audit_log_untrustedstring(ab, msg);
-             return;
+             return 0;
                return NULL;

kernel/auditsc.c error_path: · line 1181

Quoted 7 lines
kernel/auditsc.c +1 -2
      }
      len = proc_pid_cmdline(tsk, (char *)page);
      if (len <= 0) {
              free_page(page);
-             audit_log_untrustedstring(ab, msg);
-             return;
+             return 0;
                return NULL;
Quoted 12 lines
kernel/auditsc.c +5 -1
      }
      /*
       * Ensure NULL terminated! Application could
       * could be using setproctitle(3).
       */
      ((char *)page)[len-1] = '\0';
-     msg = (char *)page;
+
+     /* TODO: Re-alloc to something smaller then a page here? */
+     return page;
+}
+

kernel/auditsc.c error_path: · line 1202

Quoted 10 lines
kernel/auditsc.c +10 -0
+static void audit_log_cmdline(struct audit_buffer *ab, struct task_struct *tsk,
+                           struct audit_context *context)
+{
+     char *msg = "(null)";
+
+     audit_log_format(ab, " cmdline=");
+
+     /* Already cached */
+     if (context->cmdline) {
+             msg = (char *)context->cmdline;
                msg = context->cmdline;

kernel/auditsc.c error_path: · line 1209

Quoted 7 lines
kernel/auditsc.c +7 -0
+             goto out;
+     }
+     /* Not cached yet */
+     context->cmdline = audit_cmdline_get_page(ab, tsk);
+     if (!context->cmdline)
+             goto out;
+     msg = (char *)context->cmdline;
        msg = context->cmdline;
Quoted 23 lines
kernel/auditsc.c +2 -3
+out:
      audit_log_untrustedstring(ab, msg);
-     free_page(page);
 }
 
 static void audit_log_task_info(struct audit_buffer *ab, struct task_struct *tsk)
@@ -1211,7 +1237,6 @@ static void audit_log_task_info(struct audit_buffer *ab, struct task_struct *tsk
              }
              up_read(&mm->mmap_sem);
      }
-     audit_log_add_cmdline(ab, tsk);
      audit_log_task_context(ab);
 }
 
@@ -1679,6 +1704,7 @@ static void audit_log_exit(struct audit_context *context, struct task_struct *ts
 
 
      audit_log_task_info(ab, tsk);
+     audit_log_cmdline(ab, tsk, context);
      audit_log_key(ab, context->filterkey);
      audit_log_end(ab);
 
--
Quoted 2 lines

1.7.9.5

- RGB

Quoted 12 lines
Quoted 5 lines

--
Richard Guy Briggs <[email protected]>
Senior Software Engineer, Kernel Security, AMER ENG Base Operating Systems, Red Hat Remote, Ottawa, Canada
Voice: +1.647.777.2635, Internal: (81) 32635, Alt: +1.613.693.0684x3545

Signature · 3 lines
Respectfully,

William C Roberts

- RGB

--
Richard Guy Briggs <[email protected]>
Senior Software Engineer, Kernel Security, AMER ENG Base Operating Systems, Red Hat Remote, Ottawa, Canada
Voice: +1.647.777.2635, Internal: (81) 32635, Alt: +1.613.693.0684x3545

#7 lore raw

Re: [PATCH 2/2] audit: Enable cacheing of cmdline in audit_context

Quoted 24 lines · Richard Guy Briggs
Quoted 23 lines · William Roberts
Quoted 21 lines · Richard Guy Briggs
Quoted 14 lines · William Roberts

Not enitrely sure if this is getting us any benefit, as in my environment, these contexts are getting free'd immediately.

Change-Id Ia0d432bc4aba8588840f0dc0026a1e9483e5b485
---
 kernel/auditsc.c |   48 +++++++++++++++++++++++++++++++++++++-----------
 1 file changed, 37 insertions(+), 11 deletions(-)
kernel/auditsc.c +0 -0
index 45fd3d0..4b30c5d 100644

kernel/auditsc.c struct audit_context { · line 273

Quoted 4 lines
kernel/auditsc.c +1 -0
@@ -270,6 +270,7 @@ struct audit_context {
              } mmap;
      };
      int fds[2];
+     unsigned long cmdline;

Would this be better declared to avoid all the casts?:

Quoted 8 lines
Quoted 6 lines

This is indirectly coupled with my todo as well. So the value could be up to a page big, and I allocate and cache a page (which is returned as an usnigned long).
But, often times, the value is much smaller. Should I just alloc a seperate and smaller buffer with kmalloc, and
cache that as a char * to avoid the casts? Then free the page?

Do you know the size ahead of time? If so, just allocate that.

Yes, I can get it, I would need to rafactor proc/fs/base.c proc_pid_cmdline() and separate it into a function to get the length, and one to fill in the buffer.

Quoted 7 lines
Quoted 3 lines

Either way, I have no objection to taking the interface to a char *, I battled that decision in my head while I was coding.

Perhaps I am being naive, but is there any benefit to referring to a page as an unsigned long rather than as a char*? If it is only ever used as a char* (except by free_page()) can it hurt?

no, i'm pretty sure unsigned long stays consistant with pointer width looking at at its usage elsewhere in the kernel.

Quoted 3 lines

For that matter, is there a benefit to doing a __get_free_page() rather than kmalloc()?

This was suggested by Stephen Smalley that I just do the same allocation scheme as what procfs was doing. But kmalloc can boil down to a get_free_page() depending on what parts of pages the kernel has free (i think). I'm not 100% sure of the allocation details I might be missing.

Quoted 150 lines
Quoted 138 lines
Quoted 130 lines
        char *cmdline;

kernel/auditsc.c static inline void audit_free_aux(struct audit_context *context) · line 1053

Quoted 12 lines
kernel/auditsc.c +6 -0
 #if AUDIT_DEBUG
      int                 put_count;
@@ -1044,6 +1045,14 @@ static inline void audit_free_aux(struct audit_context *context)
      }
 }
 
+static inline void audit_cmdline_free(struct audit_context *ctx)
+{
+     if (!ctx->cmdline)
+             return;
+     free_page(ctx->cmdline);
+     ctx->cmdline = 0;
        ctx->cmdline = NULL;
Quoted 14 lines
kernel/auditsc.c +3 -0
+}
+
 static inline void audit_zero_context(struct audit_context *context,
                                    enum audit_state state)
 {
@@ -1118,6 +1127,7 @@ static inline void audit_free_context(struct audit_context *context)
              audit_free_aux(context);
              kfree(context->filterkey);
              kfree(context->sockaddr);
+             audit_cmdline_free(context);
              kfree(context);
              context  = previous;
      } while (context);

kernel/auditsc.c error_path: · line 1167

Quoted 5 lines
kernel/auditsc.c +1 -1
@@ -1154,35 +1164,51 @@ error_path:
 
 EXPORT_SYMBOL(audit_log_task_context);
 
-static void audit_log_add_cmdline(struct audit_buffer *ab,
+static unsigned long audit_cmdline_get_page(struct audit_buffer *ab,

static char* audit_cmdline_get_page(struct audit_buffer *ab,

Quoted 4 lines
kernel/auditsc.c +0 -0
                                struct task_struct *tsk)
 {
      int len;
      unsigned long page;

kernel/auditsc.c error_path: · line 1176

Quoted 10 lines
kernel/auditsc.c +1 -5
-     char *msg = "(null)";
-
-     audit_log_format(ab, " cmdline=");
 
      /* Get the process cmdline */
      page = __get_free_page(GFP_TEMPORARY);
      if (!page) {
-             audit_log_untrustedstring(ab, msg);
-             return;
+             return 0;
                return NULL;

kernel/auditsc.c error_path: · line 1181

Quoted 7 lines
kernel/auditsc.c +1 -2
      }
      len = proc_pid_cmdline(tsk, (char *)page);
      if (len <= 0) {
              free_page(page);
-             audit_log_untrustedstring(ab, msg);
-             return;
+             return 0;
                return NULL;
Quoted 12 lines
kernel/auditsc.c +5 -1
      }
      /*
       * Ensure NULL terminated! Application could
       * could be using setproctitle(3).
       */
      ((char *)page)[len-1] = '\0';
-     msg = (char *)page;
+
+     /* TODO: Re-alloc to something smaller then a page here? */
+     return page;
+}
+

kernel/auditsc.c error_path: · line 1202

Quoted 10 lines
kernel/auditsc.c +10 -0
+static void audit_log_cmdline(struct audit_buffer *ab, struct task_struct *tsk,
+                           struct audit_context *context)
+{
+     char *msg = "(null)";
+
+     audit_log_format(ab, " cmdline=");
+
+     /* Already cached */
+     if (context->cmdline) {
+             msg = (char *)context->cmdline;
                msg = context->cmdline;

kernel/auditsc.c error_path: · line 1209

Quoted 7 lines
kernel/auditsc.c +7 -0
+             goto out;
+     }
+     /* Not cached yet */
+     context->cmdline = audit_cmdline_get_page(ab, tsk);
+     if (!context->cmdline)
+             goto out;
+     msg = (char *)context->cmdline;
        msg = context->cmdline;
Quoted 23 lines
kernel/auditsc.c +2 -3
+out:
      audit_log_untrustedstring(ab, msg);
-     free_page(page);
 }
 
 static void audit_log_task_info(struct audit_buffer *ab, struct task_struct *tsk)
@@ -1211,7 +1237,6 @@ static void audit_log_task_info(struct audit_buffer *ab, struct task_struct *tsk
              }
              up_read(&mm->mmap_sem);
      }
-     audit_log_add_cmdline(ab, tsk);
      audit_log_task_context(ab);
 }
 
@@ -1679,6 +1704,7 @@ static void audit_log_exit(struct audit_context *context, struct task_struct *ts
 
 
      audit_log_task_info(ab, tsk);
+     audit_log_cmdline(ab, tsk, context);
      audit_log_key(ab, context->filterkey);
      audit_log_end(ab);
 
--
Quoted 2 lines

1.7.9.5

- RGB

Quoted 5 lines

--
Richard Guy Briggs <[email protected]>
Senior Software Engineer, Kernel Security, AMER ENG Base Operating Systems, Red Hat Remote, Ottawa, Canada
Voice: +1.647.777.2635, Internal: (81) 32635, Alt: +1.613.693.0684x3545

Quoted 4 lines

--
Respectfully,

William C Roberts

- RGB

--
Richard Guy Briggs <[email protected]>
Senior Software Engineer, Kernel Security, AMER ENG Base Operating Systems, Red Hat Remote, Ottawa, Canada
Voice: +1.647.777.2635, Internal: (81) 32635, Alt: +1.613.693.0684x3545

Signature · 3 lines
Respectfully,

William C Roberts