From patchwork Tue Oct 29 16:13:41 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marc Dionne X-Patchwork-Id: 13855136 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id A0FD0D3A66D for ; Tue, 29 Oct 2024 16:13:46 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 116156B0098; Tue, 29 Oct 2024 12:13:46 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 0C5A46B0099; Tue, 29 Oct 2024 12:13:46 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id ECEEB6B009A; Tue, 29 Oct 2024 12:13:45 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id C624C6B0098 for ; Tue, 29 Oct 2024 12:13:45 -0400 (EDT) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 568711C2A5C for ; Tue, 29 Oct 2024 16:13:45 +0000 (UTC) X-FDA: 82727134896.22.C89CD3C Received: from mail-qk1-f175.google.com (mail-qk1-f175.google.com [209.85.222.175]) by imf11.hostedemail.com (Postfix) with ESMTP id 334A540021 for ; Tue, 29 Oct 2024 16:13:13 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=jrQtckcb; spf=pass (imf11.hostedemail.com: domain of marc.c.dionne@gmail.com designates 209.85.222.175 as permitted sender) smtp.mailfrom=marc.c.dionne@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1730218263; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:references:dkim-signature; bh=i/+rVpMU3LGFLQSvsBt+1W5qp/kMGu20FPFZx0ofHIA=; b=hYjAW0XiRbXeWubyWR6HDdVnYFOEJZWXVUEUussJomLcrbSXlH+AxFMKgooiKqorT35Aey U9IUVTpE/hPObrVmhAu2SxtbAikqjdXXVg/FFZ6+QTdyl55byMniuPSZCG4JMjOMDxX6+0 3cVAnDO3RI8bbuUbCP89vUOb4cevzI0= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1730218263; a=rsa-sha256; cv=none; b=PYkznX3QFvwhOAoH8okkRe6ZuUC6RonkicMcUNS4LsMGuO22njTq7Qy8b3z4IaGO6btecL 7KIe6U0iBPVy/y5zKPLaugA+dI/Vcu3275qw/SyPiorz8c2ewh9RSq6bSYSH8GEHL73mWl TSNapBdBoWtgHLIkOwwU0s/ASkZtoPs= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=jrQtckcb; spf=pass (imf11.hostedemail.com: domain of marc.c.dionne@gmail.com designates 209.85.222.175 as permitted sender) smtp.mailfrom=marc.c.dionne@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-qk1-f175.google.com with SMTP id af79cd13be357-7b13bf566c0so341802085a.3 for ; Tue, 29 Oct 2024 09:13:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1730218422; x=1730823222; darn=kvack.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=i/+rVpMU3LGFLQSvsBt+1W5qp/kMGu20FPFZx0ofHIA=; b=jrQtckcb62beetnQGs2avfz+haoXIOvVlkUIsVgjguS4j50/W3vPG0zKplueGa9yL7 BLnPo4j4YV+8s+XtQsgpDcgpvP7AwfyVD1mvEnWyQ6DdXN0nxrVjWTII4wjTxVqLr/i5 vWzjzU9sfPXuE+i5oHkVc07OMVoPPQ1U8/GXaIv37YSCOrnCd6bNtjUIEhtSr2PrQAj3 FwdIXWjp1mVuHD1kQ6Ynp1DqNNYBo7MkHOI6SLNE5tORnzwRTw/b/HOIUql7yatua5Vp JA2VvDr94ev6Z4u5fKAV25aivKnoHBHe4swkWhkVRH5fZBbeK6ZHIpVO1dRJrX9FlPDQ n7sw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730218422; x=1730823222; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=i/+rVpMU3LGFLQSvsBt+1W5qp/kMGu20FPFZx0ofHIA=; b=ojaZk8JL11OzN4Q+OAJ62cXQhUF+mtRxRSLgVDnBPGe7ZXa2eoPR3rP5zNQYc60qur TykgoDrA9MYRL7bwz35Ewrjly1fkI9GO1WQbCxuYtrqBldP6lBnnopXJKrkFxagCUI0m teHbWCXw4RykEMi0bXlm1L6dR8enh5mfObWzsdoy2G6bZiDkvZjAB7bIIj4aARTUIFAT NBCCZyx2YMd+5syZQ2hXeDthWdiz7BfpyBILTpzdcmMYpgsk9gqs3Ui9pmuO1kZwYGuO SHQkJ2ZOE28YlnuJbGUPAZM/ykVJXi6DFfZ7NXZqeAfHBlYPnJ7pQrs7adwH4VDWw3mv PS9A== X-Forwarded-Encrypted: i=1; AJvYcCUuDABCYUW/AvL0r8R1g9nEBJgqXXkucIES/F5f9hRDgX8DRpwn6Zcjdbl/1w1cHSnGm7ZI/iFEKw==@kvack.org X-Gm-Message-State: AOJu0YyYn9TsiyRKS45AWHzDwfRNmoKbVJVlS/An/dxt+D/LeEiTG7SG hwe5n30qhnyNWiwyZkOHKAwL4U2Fc2RD8FDyEg1dqrB3mIqiM+Sm X-Google-Smtp-Source: AGHT+IGrgqBvYt59b34IHtFVMfSaUoubxtwBkyNT463/rxysr1T9JgF2E31yqnXXOWqx3YO32LK43w== X-Received: by 2002:a05:620a:170c:b0:7b1:880c:57fa with SMTP id af79cd13be357-7b1aee4b8a3mr17150285a.52.1730218422576; Tue, 29 Oct 2024 09:13:42 -0700 (PDT) Received: from hemlock.fiveisland.rocks (dhcp-213-15-2-159.pbband.net. [159.2.15.213]) by smtp.gmail.com with ESMTPSA id af79cd13be357-7b18d359411sm424685185a.130.2024.10.29.09.13.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 29 Oct 2024 09:13:42 -0700 (PDT) From: Marc Dionne To: Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Marc Dionne Subject: [PATCH v2] tools/mm: Fix slabinfo crash when MAX_SLABS is exceeded Date: Tue, 29 Oct 2024 13:13:41 -0300 Message-ID: <20241029161341.14063-1-marc.c.dionne@gmail.com> X-Mailer: git-send-email 2.47.0 MIME-Version: 1.0 X-Stat-Signature: kn4hsdwzddd6gecbbsan8f1sxasnua5t X-Rspamd-Queue-Id: 334A540021 X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1730218393-327864 X-HE-Meta: U2FsdGVkX185HHdYj4qWxvx5yh5Ss/jwHX5Aesc03QVLesS0NaS1bWyVqt5cFa+vIUCb3+lAbVpfubp6UiBzCZvoNyK3Vx7tqyKVTHQWe0hNYKs8YrtstdCqd+IY8731Khi5g4CHQzy85wO738AiNDzbut9PTuvy9R9qQ+2T4y6jqRuFkIjT1Kd8UIjwy1UXiCGr/hgdknnCbo1oGkM9dxceQ8bU31JwBu0mHgwylT227XpQYG0rCsTfL6RrtiEwCyw0p82o/5SvRd2ysaMaRDZECRhcBzVOaNmJw7lAIEqGZsKLO+nJlzlQq+H9XXHavHVSD81j3SUBgS0NosjzMqwAYVtMxd8ysZMVjhTI6LsU6JIut0vG5sK0jlrzoGl4cNHWlc2IEJHYRMq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URKH/l5Eh90g= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Marc Dionne The number of slabs can easily exceed the hard coded MAX_SLABS in the slabinfo tool, causing it to overwrite memory and crash. Increase the value of MAX_SLABS, and check if that has been exceeded for each new slab, instead of at the end when it's already too late. Also move the check for MAX_ALIASES into the loop body. Signed-off-by: Marc Dionne --- tools/mm/slabinfo.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tools/mm/slabinfo.c b/tools/mm/slabinfo.c index cfaeaea71042..2e0a1890dbbe 100644 --- a/tools/mm/slabinfo.c +++ b/tools/mm/slabinfo.c @@ -21,7 +21,7 @@ #include #include -#define MAX_SLABS 500 +#define MAX_SLABS 1000 #define MAX_ALIASES 500 #define MAX_NODES 1024 @@ -1240,6 +1240,8 @@ static void read_slab_dir(void) p--; alias->ref = strdup(p); alias++; + if (alias - aliasinfo > MAX_ALIASES) + fatal("Too many aliases\n"); break; case DT_DIR: if (chdir(de->d_name)) @@ -1301,6 +1303,8 @@ static void read_slab_dir(void) if (slab->name[0] == ':') alias_targets++; slab++; + if (slab - slabinfo > MAX_SLABS) + fatal("Too many slabs\n"); break; default : fatal("Unknown file type %lx\n", de->d_type); @@ -1310,10 +1314,6 @@ static void read_slab_dir(void) slabs = slab - slabinfo; actual_slabs = slabs; aliases = alias - aliasinfo; - if (slabs > MAX_SLABS) - fatal("Too many slabs\n"); - if (aliases > MAX_ALIASES) - fatal("Too many aliases\n"); } static void output_slabs(void)