Received: by 2002:a89:d88:0:b0:1fa:5c73:8e2d with SMTP id eb8csp775125lqb; Fri, 24 May 2024 12:47:15 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWAeBrHDVQ/1RhBjYlwNHxBVS6WRO2unlc6pAyTFuIuM+mFCsi+QQJYKCRWGg7KcQkOvxQJEQ7jhczbZCkQPNHbX3owEjALS4pjtHaiMg== X-Google-Smtp-Source: AGHT+IEIph/AtKKRg0bgRapH6p0nr8whSUYoOqiZU176gjNO+NSc2ByJIdAJf8dlRu1etdHizcdk X-Received: by 2002:a05:6a00:a0a:b0:6f4:7113:5d0a with SMTP id d2e1a72fcca58-6f7727bdd7bmr10996671b3a.11.1716580035614; Fri, 24 May 2024 12:47:15 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1716580035; cv=pass; d=google.com; s=arc-20160816; b=vGYPkx+hVgwoLe/A2TKZ+qNGO9KUR4HJWRh9Vo4hTvDEyVBRAaXJN6vNFz7l6up5dT bu75LiAkZHfxNwCcB9OQ56IQsIwGzsYEvwFdj0rLVoSIdVrbOYc72dXq5UflX52Y5BSF kFyofxMkJBtQqPEjJk0/d7/q2WfOt8bo32pFdSn2EjrZzL2DEFbW4mZE3fOLhegNO5Sh e2kY8gFD+ZhEmtOczWTnemGznaqvGy2s22sH5U6h13IAmQqJpymSTQctvnP+Lg3Scfkg +bKGPRlMjdvar1r6A5uflNEFdMBn6b6N5tlyQ3gmkH2fsH8Wu+EarlGk274OIhmWi7Sk ummw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:cc :to:from:date:dkim-signature; bh=zfFKfQRl66NaHOBeukuApZN3TjnqGpMUm67PVQU9uwY=; fh=yL8v8g9KFVAaowFkxY8IvdesAclI4WPhSi80jbXULzk=; b=eSdXc04CW3yNi90RMMRA0kW953ou8Qk6ff96eGT0H4mTqlv1LetXV4OVF403tElDLN H4xnCYgW5NDvLCIJx8Og5M7fp9j/b5Vno8OnKMGYh3ulqQWGZ+pxmQCdI4zEfsKSLvIG o9gN/CnETnHFuQh0t8t9Sy7YQ/jtmGvAJeDdxJkIxGpiF01YbQfBHse9HMZxa5QvjnkM l/dwg+nNFiy5mXNoZ8gd7sVFTJIRIOCiwbYZQJxGTVRugYVwFulTJrehMpwHwkgWe0R1 7dA7IbLTdZqJ4b52EczPXbukAUA0to439zpTN6hd1/Kgf0T/h+IGHw78YbxRE5/suCc1 A/8Q==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=kDmL8cjM; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-189059-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-189059-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id d2e1a72fcca58-6f8fd9dd266si1879735b3a.353.2024.05.24.12.47.15 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 May 2024 12:47:15 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-189059-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=kDmL8cjM; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-189059-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-189059-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 3095B2824B6 for ; Fri, 24 May 2024 19:46:23 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 138F543AAE; Fri, 24 May 2024 19:46:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="kDmL8cjM" Received: from mail-pf1-f172.google.com (mail-pf1-f172.google.com [209.85.210.172]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 07D851BF3F for ; Fri, 24 May 2024 19:46:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.172 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716579977; cv=none; b=rN/RkbVITjzGVaDVajEG/MFuHdto1mPzEVtfi1929lst9tsO3ckuuNcc1iuiXL6t0QmSC0guZy89VUCOrC0ymgUV7W7rbEzofn/gaReDq2Z44sob7N9V2rFKcdvbetfmfNMw/9tECXK84rNWVXpLyyBWmhaYBJn5sQrUazRS+5M= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716579977; c=relaxed/simple; bh=v57WsHIXuyTLImPDj4QD2uCiQ9W6+aOrQYAEun7dCrQ=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=MG+xomM7acJLORQolgwaQyiEyTu0wB9lasiRJPSzXtTz7tfCEwuUA8zyt8hBC9Ra2b397cgrYquA8tkZo/NqZLnlo52jj2rUTr6LUrWj1t4T4wC6FUOTZdF6Qp5KBp6Mzupg8ZXQKJG1naXQBF2mmqAX/X7BvX/ZLdbkDxKM4Hk= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=kDmL8cjM; arc=none smtp.client-ip=209.85.210.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-pf1-f172.google.com with SMTP id d2e1a72fcca58-6f8e838cfbfso23315b3a.0 for ; Fri, 24 May 2024 12:46:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1716579975; x=1717184775; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=zfFKfQRl66NaHOBeukuApZN3TjnqGpMUm67PVQU9uwY=; b=kDmL8cjMYij08fya6tMdhuzA7TlrHcATM1QyByM/tXSprGVLrw+7R9AZj61403f69M nq4MkBRin4u7LlGB8ckCT0mKn0oOGbYuM1T+sp21skgLGTHuiCT5QSE0SOQmCNfkcekz cQIFJz29bfIYrO6Cv84bTgmPBdzuEIU9djqobIFx3b0X4Pz9WHD9dbjL7zrKWTB0ZE26 a+VQwqSYw7bpenVVC6u6L9zTJyFIaVq8FrQHphu6qL+UTyQ5Qi6VpyPtP36HWpUR2wfT 45I8YbPSDp7uTnKjP3A91CDwdo2Ps++ghVeCTL1YK77g2+ReStAS8DqDYoViHPRG1jLG WKsg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716579975; x=1717184775; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=zfFKfQRl66NaHOBeukuApZN3TjnqGpMUm67PVQU9uwY=; b=UM4igYaek3xa7e/D0T52J922ndRsgEX+WGzqeVZ/itKeCxXvLEIKBZo4UjcUbAlNwc xMnSyGZSMHjvwx5dOGBnW9ktfueSVHdEdtEZKD6q/iKjM3eUXWWiCXQpKz84hhkFydBe AWyjR/7FP4iQI8k814tnOr4vV8c9g+KNIwwkGHKmcdblOOrKbUJzrv5xCUt1zwyr0T5u 305P2OhNemO/io1VSUtHzbnrANQJzTEa12SJNd+auvaKLM1g82rXCnPK2wlZpa+8v+Uc 4fgZOAZmW+uKBCGYdhyCM+NbDTw3yT0LfLaypXhrwMJ5/M3N7C0+LfwcyuBRNFfJevWr MKlw== X-Forwarded-Encrypted: i=1; AJvYcCWbY1rnxyxXkxTIReVljxi1wTwiEW0h/2aAW8rqUmpdnjLTmUbr0amftZKxrA0r5OxlbCkx6z3dye/Fi/wKOK8Naaxp6sJhL9x9mrTC X-Gm-Message-State: AOJu0YwWmZAKWawuIuyZLn0cZV3zuOLfry3+zgVSVrZOatkFU4hcJCBj tsAtLeMvbWdXmRxen/n6IxEGaYG/GQZKCYJYqg0xHE6N95WfxljrbZnpew== X-Received: by 2002:a05:6a00:11d3:b0:6f4:c946:5584 with SMTP id d2e1a72fcca58-6f8f3a231afmr3361088b3a.2.1716579975152; Fri, 24 May 2024 12:46:15 -0700 (PDT) Received: from visitorckw-System-Product-Name ([140.113.216.168]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-6f8fc15b169sm1455694b3a.75.2024.05.24.12.46.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 May 2024 12:46:14 -0700 (PDT) Date: Sat, 25 May 2024 03:46:12 +0800 From: Kuan-Wei Chiu To: Andrew Morton Cc: jserv@ccns.ncku.edu.tw, linux-kernel@vger.kernel.org Subject: Re: [PATCH] tools/lib/slab: Fix potential NULL pointer dereference in kmalloc() Message-ID: References: <20240524191459.949731-1-visitorckw@gmail.com> <20240524122350.a22ca8dfe07a21f3eb862159@linux-foundation.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20240524122350.a22ca8dfe07a21f3eb862159@linux-foundation.org> On Fri, May 24, 2024 at 12:23:50PM -0700, Andrew Morton wrote: > On Sat, 25 May 2024 03:14:59 +0800 Kuan-Wei Chiu wrote: > > > In kmalloc(), add a check to ensure that the pointer 'ret' is not NULL > > before attempting to memset it when the __GFP_ZERO flag is set. This > > prevents a potential NULL pointer dereference. > > > > ... > > > > --- a/tools/lib/slab.c > > +++ b/tools/lib/slab.c > > @@ -22,7 +22,7 @@ void *kmalloc(size_t size, gfp_t gfp) > > uatomic_inc(&kmalloc_nr_allocated); > > if (kmalloc_verbose) > > printf("Allocating %p from malloc\n", ret); > > - if (gfp & __GFP_ZERO) > > + if (gfp & __GFP_ZERO && ret) > > memset(ret, 0, size); > > return ret; > > } > > I suspect we have a lot of unchecked mallocs in our userspace code. If > there's an argument for fixing them all(?) then it would be best to do > this in a wholesale fashion rather than patch-at-a-time piecemeal. > It seems likely that I'm not the first to notice the unchecked mallocs in our userspace code if they're indeed widespread. Are there specific reasons or guidelines indicating when malloc checks are necessary, and when they can be omitted? Regards, Kuan-Wei