Received: by 2002:ab2:7903:0:b0:1fb:b500:807b with SMTP id a3csp1121559lqj; Mon, 3 Jun 2024 10:41:54 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUFyiAe+vUdnSOTPNxS7PBPi7LhGTbHPm23VlkJ79CfPKN22HsUSB3lB+pHESyrjveo1I+YtS5klNprolEuBDzoqTpipryEzFIRxaEi9Q== X-Google-Smtp-Source: AGHT+IHs38u0+VbcQouw6FX7WtHdHZCY/q6JBv8MOp3F2uEH48n+G5LCYs0/2ZwRZFJyVWxsd94x X-Received: by 2002:a17:90a:ab11:b0:2c2:27a8:9a6a with SMTP id 98e67ed59e1d1-2c227a89b83mr3954551a91.25.1717436514445; Mon, 03 Jun 2024 10:41:54 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1717436514; cv=pass; d=google.com; s=arc-20160816; b=HGR2TeBiu1WeeXG05/4ImXuSGK9W+UHi1gZsuVUzcVdAOn3+7nXisu8uRYL2UWPFJ7 sjZm7EHye2hSACNwthE4a7LsFlSLX5nr9gcdZA9mex6Oq0xz7CcXIXGm6amfYNW89HIi 23C/HfA3mI7VTpZua+kyU/twdF3fTn7CYhbVbZNae/l7oefhShoBlyMlvOsf1WBhUIyw 86syukdrSmIj1Z1Q/E1D2iJnBsiTjpEGjePcPlJvNbgWp9ZE7aIqmT4DVRne8yC12XO4 9ozXrIWaxvEewx3soN54fnhMEaF39mnwfpfYEjgK5K6fLpGwfjvsVMPTXZ1pdIkLMYME xy8g== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:list-unsubscribe:list-subscribe :list-id:precedence:dkim-signature; bh=I6lZeLqXe3ejNVp6NkHTGb2agg9ZhMGoWIbA4FuiQCE=; fh=7sX/Cj9HQK1xOrBjN4SiX6kg2JF+5g2WuBKmPelI97w=; b=PP4xETMbF9505/WW0R5ZDeKLkc837n+7xGgmyh6lId2U9qIUVk37FTUXcgclvOS5Vn ajTv8I8lFjr825kg2Zgzfb1zXw9Aaja7bB0pd70HTVQp1R9MEgJ8UR508L1zBILQcotR lujlXa9LGGmROxFiRMI2mhGGt39CZMrdKSsXIAOnJ8DvlAQyuvV3lxNU7E+cTac8QOh9 AndeX+FXJ6YiwAyLrh+ttSAm6HWz5JHsuIdFAr3XoEBS6eNqZHTsOIAPVFL+DJEuQSEk V7SkmRUCFN/kY4ASCPKp1jxcmj6uj52gx6jWxQq9rcDID4v8RT8FrqxDln48APhXVl+5 M1VA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=opYuUqrK; arc=pass (i=1 spf=pass spfdomain=google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-199516-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-199516-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id d9443c01a7336-1f63233cefbsi4503425ad.36.2024.06.03.10.41.54 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 03 Jun 2024 10:41:54 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-199516-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=opYuUqrK; arc=pass (i=1 spf=pass spfdomain=google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-199516-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-199516-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.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 B75D028B2E1 for ; Mon, 3 Jun 2024 17:32:31 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 6A713137914; Mon, 3 Jun 2024 17:32:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="opYuUqrK" Received: from mail-wm1-f53.google.com (mail-wm1-f53.google.com [209.85.128.53]) (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 DEF34136E28 for ; Mon, 3 Jun 2024 17:32:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.53 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717435940; cv=none; b=mwpUdrEvdwdxMEOofhSyzMch/Db1dZrF1x4rHNKDJpF8obEYYe816s5twr8lkpZhkdUzLmHZC0uEf8+zkW9uQimtJjwfKuBxrTES1B5EMVqVg870SvWREnkySPBap9DXwJw6iKs1p1cGoASFDzN5y4oVA3qUQNH80IqUxkz1VcY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717435940; c=relaxed/simple; bh=O6GhZotd2BYZDip5JDutiTFpsVWFbSZQ3i1oM5jDl/o=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=tK9ebUPD0cv4M5w6RIyQCV140GMt9KbypCAZLR22+UScR1BEb7wh4BnZCw+GjNT8DUBceb9DwuEwppfL47sg2zkv2pbYRRWAhnsHi/hg8BJu7pCBe+tV86wc9ZXdBDHbLhtDIwKwBKKXPte3QOistTPD2wFdm6ZmO5VaUS+wc/A= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=opYuUqrK; arc=none smtp.client-ip=209.85.128.53 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=google.com Received: by mail-wm1-f53.google.com with SMTP id 5b1f17b1804b1-42108856c33so27278265e9.1 for ; Mon, 03 Jun 2024 10:32:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1717435937; x=1718040737; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=I6lZeLqXe3ejNVp6NkHTGb2agg9ZhMGoWIbA4FuiQCE=; b=opYuUqrKr/AS8k2ujbYCyKLJ4qNZwoSoXnSY9mwfj5D42Ysa12qJvSeBwNz2uxEVYn CEbn/mMrY38vZ2CLDSxFasJmkaziEYTvk4PPjYna1AIRB2ycy/MtnYouzFrRnnByAsJ8 R654BfuxVdIOnSRsg1cmjJV/agsjqrme09qtYjAQEPLPPlgHG5meJhRlAE+OfAFQs/wg 6rLae9ZN265E18yVgFUUsjFS1kDv1zJjM8U23sxNwRH37pNPNEaCEhfDhJuxUr0kHXAG qG8T8HWcj+NcJQQWKVLgFx29DcstJ47Q3O9SdqXNLVSakffYZW9EEptcBifvOfwX2CLy +eoA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717435937; x=1718040737; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=I6lZeLqXe3ejNVp6NkHTGb2agg9ZhMGoWIbA4FuiQCE=; b=AJVVIFctjgD36jhZ3V5B449GskTnIymBHQxqZDmbsdXQGcCFuf7u+scHrU2eszpktC WHMOgPJL+WnSfmo3UA5xhStYQxbdHx0OoEjyYPzlZDzwg61SJG876zIxxmuL1Tmr2Mhw GCEkC9E6WiBRQzxdzo3TPusSJEB5WoCjGACHkrlUQlVFASGqN1ggFAYDX4gp0PvE69WG dF3woAEhV7E7dfCVhHrjZ28ntmab3JJQU4+NYYJokHIFhvIDqhUrTcLDgD/jgAPtcU8T cieUB/k4sIXyQMwQ+O7D5S4pY1zlqmf3r2NFU4cVROZFr61jIJ9x9S/gMGmyoHuIxwfP WbZg== X-Forwarded-Encrypted: i=1; AJvYcCXXP9D6aN+m2OURGPO1JBI3Py2dqVZwmgyCXS3am1qFtrusrTrAmnHdyBT5c1n1Ohl33y/aDF8CdiEnKpSRzGWqklNinIGkDVc0XfXY X-Gm-Message-State: AOJu0YxUPVPXAnI/v9+4AIkJ7qEhZMkjkSeDd1ov0IxpLemcp+hNQ2QW cO7CUewidupz2fLSpAkI+r3ALR9SF4bh1Eo1ZORS03vWMI8RTJC0/5XatNLCsKKmmfRAnaT3vY1 O7Wd+ZLWnL8VRXI7BPRphIo6zEGs8HygtY7yc X-Received: by 2002:adf:e3d1:0:b0:354:db90:6df4 with SMTP id ffacd0b85a97d-35e7c56ba79mr346084f8f.23.1717435936987; Mon, 03 Jun 2024 10:32:16 -0700 (PDT) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: In-Reply-To: From: Axel Rasmussen Date: Mon, 3 Jun 2024 10:31:38 -0700 Message-ID: Subject: Re: [PATCH] fix: Prevent memory leak by checking for NULL buffer before calling css_put() To: Geunsik Lim Cc: Vlastimil Babka , Ingo Molnar , Andrew Morton , Thomas Gleixner , Peter Zijlstra , Hugh Dickins , "H. Peter Anvin" , Steven Rostedt , linux-kernel , linux-mm Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Mon, Jun 3, 2024 at 5:33=E2=80=AFAM Geunsik Lim = wrote: > > This commit addresses a potential memory leak in the > `get_mm_memcg_path()` function > by explicitly checking if the allocated buffer (`buf`) is NULL before > calling the > `css_put()` function. The prefix 'css' means abbreviation of cgroup_subsy= s_state > > Previously, the code would directly call `css_put()` without checking > the value of > `buf`, which could lead to a memory leak if the buffer allocation failed. > This commit introduces a conditional check to ensure that `css_put()` > is only called > if `buf` is not NULL. > > This change enhances the code's robustness and prevents memory leaks, imp= roving > overall system stability. > > **Specific Changes:** > > * In the `out_put` label, an `if` statement is added to check > if `buf` is not NULL before calling `css_put()`. > > **Benefits:** > > * Prevents potential memory leaks > * Enhances code robustness > * Improves system stability > > Signed-off-by: Geunsik Lim > Signed-off-by: Geunsik Lim > --- > mm/mmap_lock.c | 3 ++- > 1 file changed, 2 insertions(+), 1 deletion(-) > > diff --git a/mm/mmap_lock.c b/mm/mmap_lock.c > index 1854850b4b89..7314045b0e3b 100644 > --- a/mm/mmap_lock.c > +++ b/mm/mmap_lock.c > @@ -213,7 +213,8 @@ static const char *get_mm_memcg_path(struct mm_struct= *mm) > cgroup_path(memcg->css.cgroup, buf, MEMCG_PATH_BUF_SIZE); > > out_put: > - css_put(&memcg->css); > + if (buf !=3D NULL) > + css_put(&memcg->css); > out: > return buf; > } I think the existing code is correct, and this change actually introduces a memory leak where there was none before. In the case where get_memcg_path_buf() returns NULL, we *still* need to css_put() what we got from get_mem_cgroup_from_mm() before. NAK, unless I'm missing something. > -- > 2.34.1 > ---- > To unsubscribe from this list: send the line "unsubscribe linux-kernel" i= n > the body of a message to majordomo@vger.kernel.org > More majordomo info at http://vger.kernel.org/majordomo-info.html > Please read the FAQ at http://www.tux.org/lkml/