From patchwork Tue Oct 29 16:06:09 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marc Dionne X-Patchwork-Id: 13855131 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 635C5D3A66D for ; Tue, 29 Oct 2024 16:06:17 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CF4A16B008C; Tue, 29 Oct 2024 12:06:16 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id CA3E76B0092; Tue, 29 Oct 2024 12:06:16 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B6C0A6B0093; Tue, 29 Oct 2024 12:06:16 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 9EC546B008C for ; Tue, 29 Oct 2024 12:06:16 -0400 (EDT) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 408611C7B0F for ; Tue, 29 Oct 2024 16:06:16 +0000 (UTC) X-FDA: 82727116458.02.B29A85F Received: from mail-qv1-f41.google.com (mail-qv1-f41.google.com [209.85.219.41]) by imf11.hostedemail.com (Postfix) with ESMTP id 3FE7A4002E for ; Tue, 29 Oct 2024 16:05:44 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=mGEI8Dxe; spf=pass (imf11.hostedemail.com: domain of marc.c.dionne@gmail.com designates 209.85.219.41 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=1730217815; 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=JxqDfsTZfxgb3cilRe8hoCrBEVjF1sBAyNYF4pj84bA=; b=OF1mRHGi7BXwox1AQxo5KeMmVALIUnSuueOppdu367evnFErU5GM3LOuBHlIBRJm47iP89 0mN2a9cXTUpVTHE/nutpMp47bGk4w0IKmEWGGX/NQm8jTttAoHIFBjrHvHwCBJi1+I9Oap g7ebeElk1/PxjvJfvfVe+wMCQBViKgc= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1730217815; a=rsa-sha256; cv=none; b=qKocoAP3IyU+DviVwDX6aT14o+XtAc4b6r8Q9rjlFCkUG3w15k1A+brM6qckeJkiRTmYq4 /CNh0JgSdrtuXmpkoXBmQFw3XcVMkx9lfL+WTy2na/1N83edFvyckftDMlK3wp+CYdlJqI ePeYgsk7f1xrAOE4OXIxsgx/ZLXZ+aE= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=mGEI8Dxe; spf=pass (imf11.hostedemail.com: domain of marc.c.dionne@gmail.com designates 209.85.219.41 as permitted sender) smtp.mailfrom=marc.c.dionne@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-qv1-f41.google.com with SMTP id 6a1803df08f44-6cbd550b648so41690096d6.0 for ; Tue, 29 Oct 2024 09:06:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1730217973; x=1730822773; 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=JxqDfsTZfxgb3cilRe8hoCrBEVjF1sBAyNYF4pj84bA=; b=mGEI8DxeclgMrFYdYNPruoBmFxt/syoUuCP4W1fjYF5p77AJ5XG6ToS0cz7Fhhnhpz F3HBdHVr6AbrFK1dEcOH0ZlIL8Rb0L5nZ87LNQyA/4eLRW/gMaF96NKk/pQh9UhOTenO BBZVkLOff0mqPs0EBGtg7+mj47H83G7r4QOQ2D8jPzRUlPXisNdiCMpGIGnuWFIfm8Yd odlpep2wKTBYD95aTNc7daTMRDbnpKnz74qWkP5VBvVXK7ovn+hOeXpBtfM4jNywOQry d+vGjfRX96TDKwlyuhhU6mW0PSaspvriBVTDulFxW1gxuUJoZQmx0lM28uzMJXtCovmK 9z2g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730217973; x=1730822773; 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=JxqDfsTZfxgb3cilRe8hoCrBEVjF1sBAyNYF4pj84bA=; b=mffjtu2QaOQLw1cvXeb45zliwdl3LSOTTxSJqpWrphln80hSMohq8fXaWrKUkv89B3 DS+bNwn+3deDCgphcR5fvi9EA7B2wTRMwcIeBkpdBFj6LzcAFTNDL/Ro10MT2BMuiYHI Kx2/g2II0d3CWjoyeOCDXHKwqHlo6DWnRQIyXgFGfLj9UWUh+wELHS5RuUi60+1bdUF4 U5YHoz5jopcTkipedYfoVtIeogAeIMMM46JtKj8GrUImULIZfJAfaVVafdFqL+i+qBxb 5LgbthoRQIDjf5n1aQn6Kc+xQZTLjjAFJ+uJAbEWzkmZRFKzjOaAQDd2m149lVHH2T6o uXdg== X-Forwarded-Encrypted: i=1; AJvYcCU4tlIRMfNcMi+8r4svpI4NBIJAsN+UDXy8SqrwIedV0eDcfwpNz7taktbg0Y80qwByz8+kivL57w==@kvack.org X-Gm-Message-State: AOJu0Yy5md5OdE4NVw8styzssd64FoRyOe1uQBMECUR8tErqWOFJ+J7X kE4A0aXF6AJe3ed3VlVWerPgkrubDSm6pw+hKcST4c3qCnt/Bk6B X-Google-Smtp-Source: AGHT+IE/qbLC9OX8BeHdYRbXF0ot9ym22z0B2Ed7vUvRq09Q1GEpYyHNEHqdaG9BO5Ed5KamyQVKow== X-Received: by 2002:a05:6214:5404:b0:6cd:faea:9f78 with SMTP id 6a1803df08f44-6d1856bf3e7mr214160526d6.12.1730217971752; Tue, 29 Oct 2024 09:06:11 -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-6d179a2b208sm43280476d6.111.2024.10.29.09.06.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 29 Oct 2024 09:06:11 -0700 (PDT) From: Marc Dionne To: Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Marc Dionne Subject: [PATCH] tools/mm: Fix slabinfo crash when MAX_SLABS is exceeded Date: Tue, 29 Oct 2024 13:06:09 -0300 Message-ID: <20241029160609.13725-1-marc.c.dionne@gmail.com> X-Mailer: git-send-email 2.47.0 MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 3FE7A4002E X-Stat-Signature: qupndsi36g8mqzyeduqygfz4w1zfehpw X-HE-Tag: 1730217944-543472 X-HE-Meta: U2FsdGVkX18t1pcqpfdfIKAQBREr0FKPQ8ii1dZnubIZgps9eZvqiCOfmVifhKHgg9DkAyrBTys2ZrLArmhQjIduv1rTREt8LrsjkIZz1hWE1FT7kNNjdyA2KNdxfYH3Wsmj2OwZn9ln+vf/PIVqqJz7Od//yoU0Bvy7ylzmkNuXFg9lTHZN/YRDyzig8u+hJMmZTrvpr7mFZ8YQ97qURh5evfiLK3xs9pe8sLCmbOKVx79/bmXUgiJCQHV1IsybmeteEynFVnjv+IMfdw9oQy1e4Sgmy7jkVi1u+fpvCweZn7b3GHyDwNqfgVpjafu+Ljsw/b0dqdHLShBBDyuv7CW4CWci2WM45mMH8VYWNHWXmAdrWnstVjGg2gPxwj2uc5VH2bLZkoS/uPD3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zNOiI= X-Bogosity: Ham, tests=bogofilter, spamicity=0.001242, 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..2c9c86d677ed 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 (aliases > 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)