From patchwork Wed Oct 21 06:02:30 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wojciech Nizinski X-Patchwork-Id: 533608 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from silver.osuosl.org (smtp3.osuosl.org [140.211.166.136]) by ozlabs.org (Postfix) with ESMTP id 46354140787 for ; Wed, 21 Oct 2015 17:05:22 +1100 (AEDT) Received: from localhost (localhost [127.0.0.1]) by silver.osuosl.org (Postfix) with ESMTP id B8D3D2623E; Wed, 21 Oct 2015 06:05:20 +0000 (UTC) X-Virus-Scanned: amavisd-new at osuosl.org Received: from silver.osuosl.org ([127.0.0.1]) by localhost (.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 4vyWqM5y+XIp; Wed, 21 Oct 2015 06:05:19 +0000 (UTC) Received: from ash.osuosl.org (ash.osuosl.org [140.211.166.34]) by silver.osuosl.org (Postfix) with ESMTP id 0ECAC266A5; Wed, 21 Oct 2015 06:05:19 +0000 (UTC) X-Original-To: uclibc@lists.busybox.net Delivered-To: uclibc@osuosl.org Received: from whitealder.osuosl.org (smtp1.osuosl.org [140.211.166.138]) by ash.osuosl.org (Postfix) with ESMTP id 87D5F1C44B1 for ; Wed, 21 Oct 2015 06:05:18 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by whitealder.osuosl.org (Postfix) with ESMTP id 7F42E8CDBD for ; Wed, 21 Oct 2015 06:05:18 +0000 (UTC) X-Virus-Scanned: amavisd-new at osuosl.org Received: from whitealder.osuosl.org ([127.0.0.1]) by localhost (.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 56m9Yq2RCWFS for ; Wed, 21 Oct 2015 06:05:17 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from plane.gmane.org (plane.gmane.org [80.91.229.3]) by whitealder.osuosl.org (Postfix) with ESMTPS id F390D8CBB1 for ; Wed, 21 Oct 2015 06:05:16 +0000 (UTC) Received: from list by plane.gmane.org with local (Exim 4.69) (envelope-from ) id 1ZomWG-0006iS-SJ for uclibc@uclibc.org; Wed, 21 Oct 2015 08:05:05 +0200 Received: from 95.143.241.142.ultranet.net.pl ([95.143.241.142.ultranet.net.pl]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Wed, 21 Oct 2015 08:05:04 +0200 Received: from niziak by 95.143.241.142.ultranet.net.pl with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Wed, 21 Oct 2015 08:05:04 +0200 X-Injected-Via-Gmane: http://gmane.org/ To: uclibc@uclibc.org From: Wojciech =?utf-8?b?Tml6acWEc2tp?= Subject: [PATCH]libc/stdlib: =?utf-8?b?Y2Fub25pY2FsaXplX2ZpbGVfbmFtZSgp?= memory leak Date: Wed, 21 Oct 2015 06:02:30 +0000 (UTC) Lines: 86 Message-ID: Mime-Version: 1.0 X-Complaints-To: usenet@ger.gmane.org X-Gmane-NNTP-Posting-Host: sea.gmane.org User-Agent: Loom/3.14 (http://gmane.org/) X-Loom-IP: 95.143.241.142 (Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Firefox/38.0 Iceweasel/38.3.0) X-BeenThere: uclibc@uclibc.org X-Mailman-Version: 2.1.18-1 Precedence: list List-Id: "Discussion and development of uClibc \(the embedded C library\)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: uclibc-bounces@uclibc.org Sender: "uClibc" System based on Buildroot 2014.11 Linux 3.10.88 uclibc 0.9.33.2 (also with 1.0.2) systemd 216 gcc 4.8.3 (also with 4.9.2) Bug: After 2 days system is out of memory. PID 1 (systemd) is allocating. over 120MB od RAM.. Just after reboot PID 1 is taking only about 600kB. How to reproduce: With every systemd service reload or restart, heap of PID 1 grows. Try with command: watch -n1 \ 'systemctl stop systemd-sysctl ; grep heap /proc/1/smaps -A15; free' Source of bug: Uclibc's canonicalize_file_name() is allocating temprary buffer of. 4kB (PATH_MAX), and passing it to realpath() as second argument.. Function canonicalize... is not checking if realpath() fails and. memory is lost. Backtrace: #0 malloc (bytes=4096) at libc/stdlib/malloc-standard/malloc.c:844 #1 canonicalize_file_name. (name="/etc/systemd/system/systemd-sysctl.service.d") at. libc/stdlib/canonicalize.c:30 #2 path_strv_resolve (...) at src/shared/path-util.c:275 Solution: Do not use temporary buffer like in eglibc. Function realpath() will be responsible for allocation. From: Wojciech Nizinski Date: Tue, 20 Oct 2015 14:08:09 +0200 Subject: [PATCH]libc/stdlib: canonicalize_file_name() memory leak Uclibc's canonicalize_file_name() is allocating temprary buffer of 4kB (PATH_MAX), and passing it to realpath() as second argument. Function is not checking if realpath() fails and memory is lost. --- libc/stdlib/canonicalize.c | 21 +-------------------- 1 file changed, 1 insertion(+), 20 deletions(-) diff --git a/libc/stdlib/canonicalize.c b/libc/stdlib/canonicalize.c index 06e710a..da09d58 100644 --- a/libc/stdlib/canonicalize.c +++ b/libc/stdlib/canonicalize.c @@ -9,30 +9,11 @@ */ #include -#include #ifdef __USE_GNU -#ifndef PATH_MAX -# ifdef _POSIX_VERSION -# define PATH_MAX _POSIX_PATH_MAX -# else -# ifdef MAXPATHLEN -# define PATH_MAX MAXPATHLEN -# else -# define PATH_MAX 1024 -# endif -# endif -#endif - char * canonicalize_file_name (const char *name) { - char *buf = (char *) malloc(PATH_MAX); - - if(unlikely(buf == NULL)) - return NULL; - - *buf='\0'; - return realpath (name, buf); + return realpath (name, NULL); } #endif