From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 83387EB64DB for ; Wed, 14 Jun 2023 19:01:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233436AbjFNTBf (ORCPT ); Wed, 14 Jun 2023 15:01:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38334 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232129AbjFNTBd (ORCPT ); Wed, 14 Jun 2023 15:01:33 -0400 Received: from mail-pl1-x634.google.com (mail-pl1-x634.google.com [IPv6:2607:f8b0:4864:20::634]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 12E662126 for ; Wed, 14 Jun 2023 12:01:32 -0700 (PDT) Received: by mail-pl1-x634.google.com with SMTP id d9443c01a7336-1b50d7b4aaaso3887035ad.3 for ; Wed, 14 Jun 2023 12:01:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1686769291; x=1689361291; 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=t9JGyDkQDIJSZxIxX7L6Ls/rWDUQJXrgQn5UA88eCfU=; b=P6ZnwSXpzz67icMrJ5H8fdnb2YBf5iQQudM/bE6d3qPEVh9oC8jmWeABsRwaPqayRj +QwYxJBZLQp+frbZkirHo6qLXc+Cm4PHiJ4E+JqzOEApjfnMnIV/CpTO8CrVmmSca065 TD+EVOYS962ygJN2WmEkCUZLSxuMACdDPDl0o= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686769291; x=1689361291; 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=t9JGyDkQDIJSZxIxX7L6Ls/rWDUQJXrgQn5UA88eCfU=; b=ZNnRC3xtsNG5NLQJsgE/jfg5DACk3PoUFH5RrUT4zvfIVRFcM3dlqH/wz5eZat8Ajs QdDNxWPDJ2rwBoJfzOmUZDydFHBMlBQjhsfFnLLxfQe+xrDAu7mqENuIrZPCZhVBNJju KP2msBXLN0AfghCYFv2znxb7PzeqpZUl1qZ+k0SFNANedVeP6KkE3xkbiKaiCe9vY54+ L59RUXxmNxr6i+O7YX9LguRRJpaHKHVuDeA7BVnmCmc/bYOo4+Yw0YuJ94YBoTP1cXNe vii3KwNt53qOLR0bknojUiitPDgOEgb/lkROHxcLw4ZJnVgQCKi1tacp8Q9sG0e4TIhu gcFg== X-Gm-Message-State: AC+VfDxYhrQTlCgocAeH4J+hDEYHz5Ey7H1sVv93zTWofgF5PLBkVr72 HfG8wgygrMgsPlLejYRxKk9zJg== X-Google-Smtp-Source: ACHHUZ5zxehGOSUhHcheUe8CJME1qgKj4ggeCpe2UfyLuyTis6fe0kfuB6R0zMb3qzUzSObR93C64Q== X-Received: by 2002:a17:902:cec9:b0:1b1:dfbd:a192 with SMTP id d9-20020a170902cec900b001b1dfbda192mr15049762plg.57.1686769291559; Wed, 14 Jun 2023 12:01:31 -0700 (PDT) Received: from www.outflux.net (198-0-35-241-static.hfc.comcastbusiness.net. [198.0.35.241]) by smtp.gmail.com with ESMTPSA id n10-20020a170902e54a00b001aaf5dcd762sm12454978plf.214.2023.06.14.12.01.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Jun 2023 12:01:31 -0700 (PDT) Date: Wed, 14 Jun 2023 12:01:30 -0700 From: Kees Cook To: Jiasheng Jiang Cc: tony.luck@intel.com, gpiccoli@igalia.com, linux-hardening@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] pstore/platform: Add check for kstrdup Message-ID: <202306141155.FCA5D6F@keescook> References: <20230614100020.39020-1-jiasheng@iscas.ac.cn> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230614100020.39020-1-jiasheng@iscas.ac.cn> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Jun 14, 2023 at 06:00:20PM +0800, Jiasheng Jiang wrote: > Add check for the return value of kstrdup() and return the error > if it fails in order to avoid NULL pointer dereference. > > Fixes: 563ca40ddf40 ("pstore/platform: Switch pstore_info::name to const") > Signed-off-by: Jiasheng Jiang > --- > fs/pstore/platform.c | 4 ++++ > 1 file changed, 4 insertions(+) > > diff --git a/fs/pstore/platform.c b/fs/pstore/platform.c > index cbc0b468c1ab..afe07f0d1216 100644 > --- a/fs/pstore/platform.c > +++ b/fs/pstore/platform.c > @@ -631,6 +631,10 @@ int pstore_register(struct pstore_info *psi) > * through /sys/module/pstore/parameters/backend > */ > backend = kstrdup(psi->name, GFP_KERNEL); > + if (!backend) { > + mutex_unlock(&psinfo_lock); > + return -ENOMEM; > + } Hmm, I think this isn't the right place since there's been a bunch of other allocations and registrations. I think it would be better to allocate a copy (but not assign to "backend" yet) earlier, perhaps before the taking the psinfo_lock lock? Like: char *new_backend; ... new_backend = kstrdup(psi->name, GFP_KERNEL); if (!new_backend) return -ENOMEM; mutex_lock(&psinfo_lock); if (psinfo) { ... mutex_unlock(&psinfo_lock); kfree(new_backend); return -EBUSY; } ... backend = new_backend; -Kees -- Kees Cook