From patchwork Thu Oct 31 10:55:34 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marc Dionne X-Patchwork-Id: 13857806 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 62AFFE68974 for ; Thu, 31 Oct 2024 10:55:41 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CFDE26B008A; Thu, 31 Oct 2024 06:55:40 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id CAD906B008C; Thu, 31 Oct 2024 06:55:40 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B754F6B0092; Thu, 31 Oct 2024 06:55:40 -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 989516B008A for ; Thu, 31 Oct 2024 06:55:40 -0400 (EDT) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 3A3011C5DD6 for ; Thu, 31 Oct 2024 10:55:40 +0000 (UTC) X-FDA: 82733590884.12.B97E533 Received: from mail-yw1-f169.google.com (mail-yw1-f169.google.com [209.85.128.169]) by imf01.hostedemail.com (Postfix) with ESMTP id 4706B4000A for ; Thu, 31 Oct 2024 10:55:17 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=GxbzStij; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf01.hostedemail.com: domain of marc.c.dionne@gmail.com designates 209.85.128.169 as permitted sender) smtp.mailfrom=marc.c.dionne@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1730372057; 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=vHNRb+kQOoIzK3RlYlUwYxMLFnjUn6/EMjgF1j51zD8=; b=EvQaU5aSuHtHVBB1lqt8YbOU0+slbzJ2TZnVhcC1Y/RAGmfm3EOnvOZogYjOo+OXn7blPD 1fcb181ALbLhUXH85WyVSIEz2Dpp96TBKRceGfEi//Gc15eHpB+MKhsAj9Vbtww7q9VFWR a54FjfiGud1S7HGIwwCuuC3xyxFAtoY= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=GxbzStij; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf01.hostedemail.com: domain of marc.c.dionne@gmail.com designates 209.85.128.169 as permitted sender) smtp.mailfrom=marc.c.dionne@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1730372057; a=rsa-sha256; cv=none; b=MBa0Bw662wafhqXhscme7rO59b0jwg+OQc19Md6few9O4veVseMXycAIvkNrkffe/m05hf eIyQGMjRMe3MYm6jnbUjG9V3QJj0CyCbvnV+/M+wv3YDknce8oPwJmQ0xj8fu+u6Zknh9b l/KZzbsp5VB+fCY4M2XNpil4CJmuqog= Received: by mail-yw1-f169.google.com with SMTP id 00721157ae682-6ea15a72087so6207437b3.1 for ; Thu, 31 Oct 2024 03:55:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1730372137; x=1730976937; 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=vHNRb+kQOoIzK3RlYlUwYxMLFnjUn6/EMjgF1j51zD8=; b=GxbzStijavam793t/Iu6xkjizjHo/FOOW6zUeORBXNPE4alvVmWAQkiPqUaUEs4ety VmQFEVMtAM2qsGuSLPRkNeOYrGAQ0Jp16WxuGypEJVlw1VuDlrSQqVbBO6BfXFkVoNAu sppGDRLMBbqaCZOu0qGU9Vq0LnhCHrghyylgJgzmBeJC0e3caKi59k1b1Q/liveAjg2s JiciR6pKTyvUemI3IpY3OvlqYMxfSdbRQf/8fzzJdkWv82KvFp1K93WSRZzp3OY0RRsF UtYil5t7epC50XhdOszjIgj6Kae5HwSCmE/uTLIxfZwuJ+00d6BkWV/7oyQecqsSCxgu tcQA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730372137; x=1730976937; 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=vHNRb+kQOoIzK3RlYlUwYxMLFnjUn6/EMjgF1j51zD8=; b=VGKiMQLL4UrkjcPHd8kIso6kXiVRFsOiFyF9cDZhkOAw1ldonwwVGQxSyQlWbFimQX HKiy1XH+wqQwIPgRO0Pwsb6eEAgZB3CUjHUaNoq6Ms3ulraEVjf3fBYdZ0b0KNSdncaZ cN1aX3gipBOdurSs//w0ZEA7sMGaxUj1F8OG9oQfVuD4greugbn2bX8zhg6IqJvD6KjL RGNtHani6l7gM5ZKej9ZtmHb/zs7hK8X0onvPXpggTRCaNu/5X749vsgZrRoa/pLh7op AT2IeRGa+0zBVZxu+BX5eN2QAxdRS8jtpSEly5ql1pHrU73xBuBCkerqFzpEw8Hr6G03 bdcA== X-Forwarded-Encrypted: i=1; AJvYcCVfHDmYHNwzky8hgUP0Flx+cPJhNv/jFip6NoTMxQLQnTgFKbEVOcHAKTm1NC+CvyJuc2Xicv3VHQ==@kvack.org X-Gm-Message-State: AOJu0YyWql/TYOViWIpHcRiBU6jPfAfJEmMawsq6lLL3twH5zpnCpfIE JDnJ6DkXt2ECltcNOS+uuWI5tlJjfwGJjBcthFRAw1e2R20hgNw7KMABhA== X-Google-Smtp-Source: AGHT+IEkuvKipKcZUa/w/oJLu7XXuSvnRybQdKUGtgrHvr1s3fNV5JdwZUYSaRSJ6dd/HO8FSlWZmQ== X-Received: by 2002:a05:690c:4c01:b0:6e3:14b0:ff86 with SMTP id 00721157ae682-6ea524e28c9mr27632317b3.27.1730372137224; Thu, 31 Oct 2024 03:55:37 -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 6a1803df08f44-6d353f9f201sm6618476d6.16.2024.10.31.03.55.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 31 Oct 2024 03:55:36 -0700 (PDT) From: Marc Dionne To: Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Vlastimil Babka , Marc Dionne Subject: [PATCH v4] tools/mm: Fix slabinfo crash when MAX_SLABS is exceeded Date: Thu, 31 Oct 2024 07:55:34 -0300 Message-ID: <20241031105534.565533-1-marc.c.dionne@gmail.com> X-Mailer: git-send-email 2.47.0 MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: 4706B4000A X-Stat-Signature: c7yois67dno57i7bqqxdrxe3ijdwxr8a X-HE-Tag: 1730372117-616664 X-HE-Meta: U2FsdGVkX19RsAwkFu1dim7Ffpx1gE1xR3AA1LXJ4Q0zHzOhXMoJX6BXAn0tc/IFzysI47sS1tHB9j3R2E9wMyyZ8tYKCx3Tni9WT3c4RTklPjiV9/acFAJfhWHTox9YIhyTu7cRY8Yz6+Q4rDLKDWLQR+3z4ALu9HxN3B9VCK7F3cel0j6n3ngLjJBzSKXuGWgb11K1JIHcFRR7UsIp6jYBLnYL5f+G+SpaSscc/Rstj8C0w6THCeXNbWYbIPTy3Tg+7mPT6lKkqj46LPT3s7euZ/TENt+KegeBDZWiNMEDT64pitlypyuf2zsG8PrJ+fm5YDt3Yr3rorSR70jJ7RUwNvgIXAdNyqUQzoMir72EBQAlCWJv1t21+pbDpGGwT7o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zD66Mtdmbi2dDk= 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..1a9b807a48c3 100644 --- a/tools/mm/slabinfo.c +++ b/tools/mm/slabinfo.c @@ -21,7 +21,7 @@ #include #include -#define MAX_SLABS 500 +#define MAX_SLABS 2000 #define MAX_ALIASES 500 #define MAX_NODES 1024 @@ -1228,6 +1228,8 @@ static void read_slab_dir(void) continue; switch (de->d_type) { case DT_LNK: + if (alias - aliasinfo == MAX_ALIASES) + fatal("Too many aliases\n"); alias->name = strdup(de->d_name); count = readlink(de->d_name, buffer, sizeof(buffer)-1); @@ -1242,6 +1244,8 @@ static void read_slab_dir(void) alias++; break; case DT_DIR: + if (slab - slabinfo == MAX_SLABS) + fatal("Too many slabs\n"); if (chdir(de->d_name)) fatal("Unable to access slab %s\n", slab->name); slab->name = strdup(de->d_name); @@ -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)