From owner-svn-src-all@freebsd.org Wed May 25 05:12:58 2016 Return-Path: Delivered-To: svn-src-all@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 0C6FFB48C99; Wed, 25 May 2016 05:12:58 +0000 (UTC) (envelope-from truckman@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id B0AFD1977; Wed, 25 May 2016 05:12:57 +0000 (UTC) (envelope-from truckman@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id u4P5CubY084604; Wed, 25 May 2016 05:12:56 GMT (envelope-from truckman@FreeBSD.org) Received: (from truckman@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id u4P5CuvH084602; Wed, 25 May 2016 05:12:56 GMT (envelope-from truckman@FreeBSD.org) Message-Id: <201605250512.u4P5CuvH084602@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: truckman set sender to truckman@FreeBSD.org using -f From: Don Lewis Date: Wed, 25 May 2016 05:12:56 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r300648 - head/bin/ps X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 25 May 2016 05:12:58 -0000 Author: truckman Date: Wed May 25 05:12:56 2016 New Revision: 300648 URL: https://svnweb.freebsd.org/changeset/base/300648 Log: 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 MFC after: 1 week Modified: head/bin/ps/ps.c head/bin/ps/ps.h Modified: head/bin/ps/ps.c ============================================================================== --- head/bin/ps/ps.c Wed May 25 05:06:15 2016 (r300647) +++ head/bin/ps/ps.c Wed May 25 05:12:56 2016 (r300648) @@ -1235,6 +1235,7 @@ fmt(char **(*fn)(kvm_t *, const struct k static void saveuser(KINFO *ki) { + char *argsp; if (ki->ki_p->ki_flag & P_INMEM) { /* @@ -1253,10 +1254,12 @@ saveuser(KINFO *ki) if (ki->ki_p->ki_stat == SZOMB) ki->ki_args = strdup(""); 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) xo_errx(1, "malloc failed"); } else { @@ -1264,8 +1267,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: head/bin/ps/ps.h ============================================================================== --- head/bin/ps/ps.h Wed May 25 05:06:15 2016 (r300647) +++ head/bin/ps/ps.h Wed May 25 05:12:56 2016 (r300648) @@ -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() */