svn commit: r301148 - stable/10/bin/ps

Don Lewis truckman at FreeBSD.org
Wed Jun 1 17:33:04 UTC 2016


Author: truckman
Date: Wed Jun  1 17:33:02 2016
New Revision: 301148
URL: https://svnweb.freebsd.org/changeset/base/301148

Log:
  MFC r300648
  
  Fix CID 1011370 (Resource leak) in ps.
  
  There is no need to to call strdup() on the value returned by fmt().
  The latter calls fmt_argv() which always returns a dynamically
  allocated string, and calling strdup() on that leaks the memory
  allocated by fmt_argv().  Wave some const magic on ki_args and
  ki_env to make the direct assignment happy.  This requires a tweak
  to the asprintf() case to avoid a const vs. non-const mismatch.
  
  Reported by:	Coverity
  CID:		1011370

Modified:
  stable/10/bin/ps/ps.c
  stable/10/bin/ps/ps.h
Directory Properties:
  stable/10/   (props changed)

Modified: stable/10/bin/ps/ps.c
==============================================================================
--- stable/10/bin/ps/ps.c	Wed Jun  1 17:30:50 2016	(r301147)
+++ stable/10/bin/ps/ps.c	Wed Jun  1 17:33:02 2016	(r301148)
@@ -1217,6 +1217,7 @@ fmt(char **(*fn)(kvm_t *, const struct k
 static void
 saveuser(KINFO *ki)
 {
+	char *argsp;
 
 	if (ki->ki_p->ki_flag & P_INMEM) {
 		/*
@@ -1235,10 +1236,12 @@ saveuser(KINFO *ki)
 		if (ki->ki_p->ki_stat == SZOMB)
 			ki->ki_args = strdup("<defunct>");
 		else if (UREADOK(ki) || (ki->ki_p->ki_args != NULL))
-			ki->ki_args = strdup(fmt(kvm_getargv, ki,
-			    ki->ki_p->ki_comm, ki->ki_p->ki_tdname, MAXCOMLEN));
-		else
-			asprintf(&ki->ki_args, "(%s)", ki->ki_p->ki_comm);
+			ki->ki_args = fmt(kvm_getargv, ki,
+			    ki->ki_p->ki_comm, ki->ki_p->ki_tdname, MAXCOMLEN);
+		else {
+			asprintf(&argsp, "(%s)", ki->ki_p->ki_comm);
+			ki->ki_args = argsp;
+		}
 		if (ki->ki_args == NULL)
 			errx(1, "malloc failed");
 	} else {
@@ -1246,8 +1249,8 @@ saveuser(KINFO *ki)
 	}
 	if (needenv) {
 		if (UREADOK(ki))
-			ki->ki_env = strdup(fmt(kvm_getenvv, ki,
-			    (char *)NULL, (char *)NULL, 0));
+			ki->ki_env = fmt(kvm_getenvv, ki,
+			    (char *)NULL, (char *)NULL, 0);
 		else
 			ki->ki_env = strdup("()");
 		if (ki->ki_env == NULL)

Modified: stable/10/bin/ps/ps.h
==============================================================================
--- stable/10/bin/ps/ps.h	Wed Jun  1 17:30:50 2016	(r301147)
+++ stable/10/bin/ps/ps.h	Wed Jun  1 17:33:02 2016	(r301148)
@@ -42,8 +42,8 @@ typedef struct kinfo_str {
 
 typedef struct kinfo {
 	struct kinfo_proc *ki_p;	/* kinfo_proc structure */
-	char *ki_args;		/* exec args */
-	char *ki_env;		/* environment */
+	const char *ki_args;	/* exec args */
+	const char *ki_env;	/* environment */
 	int ki_valid;		/* 1 => uarea stuff valid */
 	double	 ki_pcpu;	/* calculated in main() */
 	segsz_t	 ki_memsize;	/* calculated in main() */


More information about the svn-src-all mailing list