PERFORCE change 45191 for review

Robert Watson rwatson at FreeBSD.org
Mon Jan 12 05:30:13 GMT 2004


http://perforce.freebsd.org/chv.cgi?CH=45191

Change 45191 by rwatson at rwatson_paprika on 2004/01/11 21:30:12

	More adaption for FreeBSDisms vs Machisms:
	- Use FreeBSD malloc() for audit record storage, path strings,
	  etc.  Eventually, we'll want to use a UMA zone for audit
	  records.  Note that, unlike on Darwin, with M_WAITOK, these
	  calls can't fail.
	- Use FreeBSD free() to release allocated memory.
	- Use td->td_proc in more places to get access to process
	  information.
	- Use td instead of p as argument to VFS calls.
	- Use td->td_ucred instead of p->p_ucred for credential
	  externalization.

Affected files ...

.. //depot/projects/trustedbsd/audit2/sys/security/audit/audit.c#9 edit

Differences ...

==== //depot/projects/trustedbsd/audit2/sys/security/audit/audit.c#9 (text+ko) ====

@@ -31,6 +31,7 @@
 #include <sys/ipc.h>
 #include <sys/kernel.h>
 #include <sys/kthread.h>
+#include <sys/malloc.h>
 #include <sys/namei.h>
 #include <sys/proc.h>
 #include <sys/queue.h>
@@ -45,8 +46,7 @@
 #include <security/audit/kern_audit.h>
 #include <security/audit/bsm_klib.h>
 
-#define	kmem_alloc(map, ptrref, size)
-#define	kmem_free(map, ptr, size)
+MALLOC_DEFINE(M_AUDIT, "audit", "Audit event records");
 
 #ifdef AUDIT
 
@@ -136,24 +136,24 @@
 audit_free(struct kaudit_record *ar)
 {
 	if (ar->k_ar.ar_arg_upath1 != NULL) {
-		kmem_free(kernel_map, ar->k_ar.ar_arg_upath1, MAXPATHLEN);
+		free(ar->k_ar.ar_arg_upath1, M_AUDIT);
 	}
 	if (ar->k_ar.ar_arg_upath2 != NULL) {
-		kmem_free(kernel_map, ar->k_ar.ar_arg_upath2, MAXPATHLEN);
+		free(ar->k_ar.ar_arg_upath2, M_AUDIT);
 	}
 	if (ar->k_ar.ar_arg_kpath1 != NULL) {
-		kmem_free(kernel_map, ar->k_ar.ar_arg_kpath1, MAXPATHLEN);
+		free(ar->k_ar.ar_arg_kpath1, M_AUDIT);
 	}
 	if (ar->k_ar.ar_arg_kpath2 != NULL) {
-		kmem_free(kernel_map, ar->k_ar.ar_arg_kpath2, MAXPATHLEN);
+		free(ar->k_ar.ar_arg_kpath2, M_AUDIT);
 	}
 	if (ar->k_ar.ar_arg_text != NULL) {
-		kmem_free(kernel_map, ar->k_ar.ar_arg_text, MAXPATHLEN);
+		free(ar->k_ar.ar_arg_text, M_AUDIT);
 	}
 	if (ar->k_udata != NULL) {
-		kmem_free(kernel_map, ar->k_udata, ar->k_ulen);
+		free(ar->k_udata, M_AUDIT);
 	}
-	kmem_free(kernel_map, ar, sizeof(*ar));
+	free(ar, M_AUDIT);
 }
 
 static int
@@ -512,10 +512,7 @@
 	if (uap->length > MAX_AUDIT_RECORD_SIZE) 
 		return (EINVAL);
 
-	error = kmem_alloc(kernel_map, (vm_offset_t *)&rec, uap->length);
-	if (error != KERN_SUCCESS)
-		return(ENOMEM);
-
+	rec = malloc(uap->length, M_AUDIT, M_WAITOK);
 	error = copyin(uap->record, rec, uap->length);
 	if (error)
 		goto free_out;
@@ -535,7 +532,7 @@
 	return (0);
 
 free_out:
-	kmem_free(kernel_map, (vm_offset_t)rec, uap->length);
+	free(rec, M_AUDIT);
 	return (error);
 }
 
@@ -583,7 +580,7 @@
 	if (error)
 		return (error);
 
-	error = copyout((void *)&p->p_au->ai_auid, (void *)uap->auid, 
+	error = copyout((void *)&td->td_proc->p_au->ai_auid, (void *)uap->auid, 
 				sizeof(*uap->auid));
 	if (error)
 		return (error);
@@ -601,12 +598,12 @@
 	if (error)
 		return (error);
 
-	error = copyin((void *)uap->auid, (void *)&p->p_au->ai_auid, 
-				sizeof(p->p_au->ai_auid));
+	error = copyin((void *)uap->auid, (void *)&td->td_proc->p_au->ai_auid, 
+				sizeof(td->td_proc->p_au->ai_auid));
 	if (error)
 		return (error);
 
-	audit_arg_auid(p->p_au->ai_auid);
+	audit_arg_auid(td->td_proc->p_au->ai_auid);
 	return (0);
 }
 
@@ -622,7 +619,7 @@
 	error = suser(td);
 	if (error)
 		return (error);
-	error = copyout((void *)p->p_au, (void *)uap->auditinfo, 
+	error = copyout((void *)td->td_proc->p_au, (void *)uap->auditinfo, 
 				sizeof(*uap->auditinfo));
 	if (error)
 		return (error);
@@ -639,8 +636,8 @@
 	error = suser(td);
 	if (error)
 		return (error);
-	error = copyin((void *)uap->auditinfo, (void *)p->p_au, 
-				sizeof(*p->p_au));
+	error = copyin((void *)uap->auditinfo, (void *)td->td_proc->p_au, 
+				sizeof(*td->td_proc->p_au));
 	if (error)
 		return (error);
 
@@ -699,15 +696,15 @@
 	 */
 	if (uap->path != NULL) {
 		NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
-		    uap->path, p);
+		    uap->path, td);
 		flags = audit_open_flags;
 		error = vn_open(&nd, &flags, 0, -1);
 		if (error)
 			goto out;
-		VOP_UNLOCK(nd.ni_vp, 0, p);
+		VOP_UNLOCK(nd.ni_vp, 0, td);
 		vp = nd.ni_vp;
 		if (vp->v_type != VREG) {
-			vn_close(vp, audit_close_flags, td->td_ucred, p);
+			vn_close(vp, audit_close_flags, td->td_ucred, td);
 			error = EINVAL;
 			goto out;
 		}
@@ -769,25 +766,22 @@
 	 * safe to call from the non-BSD side of the house.
 	 * XXX: We may want to fail-stop if allocation fails.
 	 */
-	(void)kmem_alloc(kernel_map, &ar, sizeof(*ar));
-	if (ar == NULL)
-		return NULL;
-
+	ar = malloc(sizeof(*ar), M_AUDIT, M_WAITOK);
 	bzero(ar, sizeof(*ar));
 	ar->k_ar.ar_magic = AUDIT_RECORD_MAGIC;
 	ar->k_ar.ar_event = event;
 	nanotime(&ar->k_ar.ar_starttime);
 
 	/* Export the subject credential. */
-	cru2x(p->p_ucred, &ar->k_ar.ar_subj_cred);
+	cru2x(td->td_ucred, &ar->k_ar.ar_subj_cred);
 	ar->k_ar.ar_subj_ruid = td->td_ucred->cr_ruid;
 	ar->k_ar.ar_subj_rgid = td->td_ucred->cr_rgid;
 	ar->k_ar.ar_subj_egid = td->td_ucred->cr_groups[0];
-	ar->k_ar.ar_subj_auid = p->p_au->ai_auid;
+	ar->k_ar.ar_subj_auid = td->td_proc->p_au->ai_auid;
 	ar->k_ar.ar_subj_pid = td->td_proc->p_pid;
-	bcopy(p->p_comm, ar->k_ar.ar_subj_comm, MAXCOMLEN);
-	bcopy(&p->p_au->ai_mask, &ar->k_ar.ar_subj_amask, 
-			sizeof(p->p_au->ai_mask));
+	bcopy(td->td_proc->p_comm, ar->k_ar.ar_subj_comm, MAXCOMLEN);
+	bcopy(&td->td_proc->p_au->ai_mask, &ar->k_ar.ar_subj_amask, 
+			sizeof(td->td_proc->p_au->ai_mask));
 
 	return (ar);
 }
@@ -1183,11 +1177,8 @@
 	if (text == NULL)
 		return;	
 
-	if (ar->k_ar.ar_arg_text == NULL) {
-		kmem_alloc(kernel_map, &ar->k_ar.ar_arg_text, MAXPATHLEN);
-		if (ar->k_ar.ar_arg_text == NULL)
-			return;	
-	}
+	if (ar->k_ar.ar_arg_text == NULL)
+		ar->k_ar.ar_arg_text = malloc(MAXPATHLEN, M_AUDIT, M_WAITOK);
 
 	strcpy(ar->k_ar.ar_arg_text, text);
 	ar->k_ar.ar_valid_arg |= ARG_TEXT;
@@ -1268,9 +1259,8 @@
 void
 audit_proc_init(struct proc *p)
 {
-	MALLOC_ZONE(p->p_au, struct auditinfo *, sizeof(*p->p_au), 
-			M_SUBPROC, M_WAITOK);
 
+	p->p_au = malloc(sizeof(*p->p_au), M_AUDIT, M_WAITOK);
 	bzero((void *)p->p_au, sizeof(*p->p_au));
 }
 
@@ -1283,13 +1273,12 @@
 void
 audit_proc_fork(struct proc *parent, struct proc *child)
 {
+
 	/* Always set up the audit information pointer as this function
 	 * should only be called when the proc is new. If proc structures
 	 * are ever cached and reused, then this behavior will leak memory.
 	 */
-	MALLOC_ZONE(child->p_au, struct auditinfo *, sizeof(*child->p_au), 
-			M_SUBPROC, M_WAITOK);
-
+	child->p_au = malloc(sizeof(*child->p_au), M_AUDIT, M_WAITOK);
 	bcopy(parent->p_au, child->p_au, sizeof(*child->p_au));
 }
 
@@ -1299,7 +1288,8 @@
 void
 audit_proc_free(struct proc *p)
 {
-	FREE_ZONE((void *)p->p_au, sizeof(*p->p_au), M_SUBPROC);
+
+	free(p->p_au, M_AUDIT);
 	p->p_au = NULL;
 }
 
@@ -1334,11 +1324,8 @@
 		pathp = &ar->k_ar.ar_arg_upath2;
 	}
 
-	if (*pathp == NULL) {
-		kmem_alloc(kernel_map, pathp, MAXPATHLEN);
-		if (*pathp == NULL)
-			return;
-	}
+	if (*pathp == NULL)
+		pathp = malloc(MAXPATHLEN, M_AUDIT, M_WAITOK);
 
 	canon_path(p, upath, *pathp);
 
@@ -1398,11 +1385,8 @@
 		vnp = &ar->k_ar.ar_arg_vnode2;
 	}
 
-	if (*pathp == NULL) {
-		kmem_alloc(kernel_map, pathp, MAXPATHLEN);
-		if (*pathp == NULL)
-			return;
-	}
+	if (*pathp == NULL)
+		pathp = malloc(MAXPATHLEN, M_AUDIT, M_WAITOK);
 
 	/* Copy the path looked up by the vn_getpath() function */
 	len = MAXPATHLEN;
To Unsubscribe: send mail to majordomo at trustedbsd.org
with "unsubscribe trustedbsd-cvs" in the body of the message



More information about the trustedbsd-cvs mailing list