Received: by 2002:a25:86ce:0:0:0:0:0 with SMTP id y14csp2574069ybm; Thu, 23 May 2019 20:32:55 -0700 (PDT) X-Google-Smtp-Source: APXvYqyl/hAWVoftWuJMQSvsiNsGpJKd4ccj+k3dnHWMTJ/SGdKDY0JnfjhTTqB+6naW7a9T3xTa X-Received: by 2002:a17:902:f20b:: with SMTP id gn11mr102941807plb.126.1558668775413; Thu, 23 May 2019 20:32:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1558668775; cv=none; d=google.com; s=arc-20160816; b=DQ+Nymx64XNi6rnbYHI5fiiU4ihpdmUSrJveldEbnzMsxQ0t54OrUIf6mET8j2ZCQs GcQN4nvFJqPCMo4yu/hUSbdwshcXuwfayfDR0i79whBHcW0uGR6AWAtQzTVn6XNkzj6m hRxgcgbn3/6R+QjeveUu4qLvFVj/11GpTuBb37QM9MeDoY9o6JVCEgGteUKIZIQeYRWP y1m/PIcimX8e9GgYI7rnAY+8cOCr6QtqoqMyHv7NBBCIzlnNpdXKumUQDu0DFioG9471 zSPynRLERAiQZPq1Aepte0c2L+yWEZC+uNa6wPHJv0lg9cKZczQBQFJQel+2gfV5isH+ xcNg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:content-disposition :mime-version:message-id:subject:cc:to:from:date:dkim-signature; bh=LRKYo/Tg41WJvHbOGajmPW4pzDGr3pcsWkrKkayOv50=; b=NGJClwDN05Mh0ecn29VaES0Vkjx4CTOo58PdNWx0IjyjLt40OnAJnAl0RFwmiKeqIF P/BFecGX7V/nclWP6agc9MHlKHy57vWBKUxbHTr2M3sN1lbTYghidklSEBcZfj+dpFUK VzXObNGLXOsCA3+t9d0ZGpollLHQUR9QF2IfxVw/BjmGOHfMz63x4LCN02dmDS7S5X8t Op7JlA61oayRMZyhhHAZXz+ShcVTLg/1+DbVJlJ4aDR9CVtkqhlOeTgjZGHfGeCLUVBU IPWH5wsGCvD0rrCheY4iqkWWZVQ6Obq52gT6uxcICDrae5jIDaJ4tIXTWcmDmuuVD0tl w14w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=WoUczZgQ; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id j14si2086322pfh.232.2019.05.23.20.32.35; Thu, 23 May 2019 20:32:55 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=WoUczZgQ; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2388625AbfEXDbD (ORCPT + 99 others); Thu, 23 May 2019 23:31:03 -0400 Received: from mail-pg1-f193.google.com ([209.85.215.193]:38964 "EHLO mail-pg1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2387559AbfEXDbD (ORCPT ); Thu, 23 May 2019 23:31:03 -0400 Received: by mail-pg1-f193.google.com with SMTP id w22so4226427pgi.6 for ; Thu, 23 May 2019 20:31:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:cc:subject:message-id:mime-version:content-disposition :user-agent; bh=LRKYo/Tg41WJvHbOGajmPW4pzDGr3pcsWkrKkayOv50=; b=WoUczZgQgZDQ55ACG6f3fVnPyzmz++Cf8vYhPWnpmXrkc7NoQm2X67O4zsc2hQvJO/ 4qAw9vwUkInCqjPUss0GJDj8mUFv3L4eA/z5lVLkXJBtYuuw22XA0t1jMWc7WqjdX5vO 0Nw7xymG79C+iSX6/+X4i5F3aCZ3P/s8L5NnmMZFPLm6wLglZ3ZD6DZGzkasVM6TpQk5 JshBlRHx3IFZWs1xNvF/zKoHs6cfxUnwBYa1rMBWJqXPgC/t5olv4LxGIswYTft+OnhZ i/um3vXPXJvu7mT9pO63iPgxx6tFKrSDPgaMy5tKNOprM9CikJ+MhUDnHmVNUGJr9e/f IKXg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:mime-version :content-disposition:user-agent; bh=LRKYo/Tg41WJvHbOGajmPW4pzDGr3pcsWkrKkayOv50=; b=LXX+UHpPYA0Ydu1rNaI956FnJ+t3yEGUvG8VB0bMHKEfKVGeH0aALwDraRZGCtrK4e KatVFgJ5+3xGDhPH2vjHysZ/+LrXeDX3OyWwjnElFRe/smd0xm5BlwFjWZ0cd34qcX2F eeLtQrq+17X9z7SfkR1cjJcW9O+MnoiQKfuFdDhUbaS3qRXIE4pNXnvnU4hl8noK4qlZ PEABXfKo8uvZXzIF+TTkCyk2RwdYY2qWFUm2fcCkTgUNF4xpnRlCd5hzNxRKJTtN6T+w IRATGe22u92pI2HMxIG3zQEjFfYaZutapO6VRPjr4i1ZxPqRtmp2or9NkGl63G3gpTdr +7Gw== X-Gm-Message-State: APjAAAUhNXIkf9VjVfgJDGbV3sQYUIV0ETsJQRthA4ivMye9PNv43sXM 7gO1gmTQmhKBYlv9yaECaIY= X-Received: by 2002:a62:e803:: with SMTP id c3mr62918637pfi.58.1558668662118; Thu, 23 May 2019 20:31:02 -0700 (PDT) Received: from zhanggen-UX430UQ ([66.42.35.75]) by smtp.gmail.com with ESMTPSA id q17sm913522pfq.74.2019.05.23.20.30.52 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 23 May 2019 20:31:01 -0700 (PDT) Date: Fri, 24 May 2019 11:30:45 +0800 From: Gen Zhang To: akpm@linux-foundation.org, rppt@linux.ibm.com, david.engraf@sysgo.com, steven.price@arm.com, osandov@fb.com, luc.vanoostenryck@gmail.com, axboe@kernel.dk Cc: linux-kernel@vger.kernel.org Subject: [PATCH v2] initramfs: Fix a missing-chek bug in dir_add() Message-ID: <20190524033045.GA6628@zhanggen-UX430UQ> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.5.24 (2015-08-30) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org In dir_add() and do_name(), de->name and vcollected are allocated by kstrdup(). And de->name and vcollected are dereferenced in the following codes. However, memory allocation functions such as kstrdup() may fail. Dereferencing this null pointer may cause the kernel go wrong. Thus we should check these two kstrdup() operations. Further, if kstrdup() returns NULL, we should free de in dir_add(). Signed-off-by: Gen Zhang --- diff --git a/init/initramfs.c b/init/initramfs.c index 178130f..1421488 100644 --- a/init/initramfs.c +++ b/init/initramfs.c @@ -125,6 +125,10 @@ static void __init dir_add(const char *name, time64_t mtime) panic("can't allocate dir_entry buffer"); INIT_LIST_HEAD(&de->list); de->name = kstrdup(name, GFP_KERNEL); + if (!de->name) { + kfree(de); + panic("can't allocate dir_entry name buffer"); + } de->mtime = mtime; list_add(&de->list, &dir_list); } @@ -340,6 +344,10 @@ static int __init do_name(void) if (body_len) ksys_ftruncate(wfd, body_len); vcollected = kstrdup(collected, GFP_KERNEL); + if (!vcollected) { + panic("can't allocate vcollected buffer"); + return 0; + } state = CopyFile; } }