From patchwork Thu Apr 14 08:57:27 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hyeonggon Yoo <42.hyeyoo@gmail.com> X-Patchwork-Id: 12813174 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 98871C433FE for ; Thu, 14 Apr 2022 09:00:02 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 38E0D6B007B; Thu, 14 Apr 2022 05:00:02 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 33DF56B007E; Thu, 14 Apr 2022 05:00:02 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2054D6B0080; Thu, 14 Apr 2022 05:00:02 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (relay.hostedemail.com [64.99.140.28]) by kanga.kvack.org (Postfix) with ESMTP id 147A86B007B for ; Thu, 14 Apr 2022 05:00:02 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id D8D022521D for ; Thu, 14 Apr 2022 09:00:01 +0000 (UTC) X-FDA: 79354887402.14.E5EA757 Received: from mail-pl1-f174.google.com (mail-pl1-f174.google.com [209.85.214.174]) by imf06.hostedemail.com (Postfix) with ESMTP id 72273180008 for ; Thu, 14 Apr 2022 09:00:01 +0000 (UTC) Received: by mail-pl1-f174.google.com with SMTP id s14so4119172plk.8 for ; Thu, 14 Apr 2022 02:00:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=B7gcZ2iThFka6LvykgF/itOBPX1z1QotaADm6wrPK4A=; b=aleL4N3zpF/Ea+70rZj2fCuVsct15PZsMcWwS03pX3yQiXe8ck7/m4zT6KNmGYgDvo mqEjk6mtV2uPs6Luaq/+ilfVnpYUeogNBGzEmh3vbyvT3C8ZsU4Qe9Ln6IGBRZeNH7QF 6WKwZNbxfUyLSKI2Ta2d+GF0DizhSqXlmAqPm9M0KhFv6bmyxSAigSZKIGWv9jNeETz7 44/WOwe7f8uK9yTDF1cWYFv8nFc9ZB590hw1Ka/eRUs/8mXqX2mUh8jIOZGvxxoYrCl/ h/O3OsN/P/VTP5eozp7LggK0Y9SO+ja0h87RlxoOuy97ZaBdRfGmt4vHdiCf7pUTz1fq FGnw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=B7gcZ2iThFka6LvykgF/itOBPX1z1QotaADm6wrPK4A=; b=ChvL+UKuJtBaEnkxAda4D9khQ5++heDVHNP79EZzgG8iQXqA8C8N58UaRlQqWEEead I6JWFKPPOeJZtaZWTlANFYB210UhNjEn7ql6bMdgDFsyPc6ksAN0S+pNuZpTCIwy31S6 YSBG5DjJ0OAefn/0KDnK5idy6ujcR5lF+rOHLHBq5CqoOTOyDa9FMhUQ475b8W/hGEut 5tFknUcBra496TFhtjOoZveEK0my7QvD6UOYIUaiGWP2ryZ3WMSRh0H22HhEbdF2MY/l 8yoll5Kgj8oDZeYp/mc3FA/dvLtkm7U91j0zyv2F8WWMSy9MmqDyMpqkFBPdxb+ls1s7 2ZjA== X-Gm-Message-State: AOAM530znsujPczb+3Y9kzHVm/omfliX/bEUS6GheGfx3JD4LPXZ3LiW fJK0t32ghx9MCaBzW0EvW6I= X-Google-Smtp-Source: ABdhPJz+MKOJRhVGRMjYjrgkLtZKyFXNHXy5iSwnzJ2XplH/gav0LkRz1Gf7Zci/tlCcRcywt3k2lg== X-Received: by 2002:a17:902:9684:b0:158:b28c:41e0 with SMTP id n4-20020a170902968400b00158b28c41e0mr3854458plp.85.1649926800535; Thu, 14 Apr 2022 02:00:00 -0700 (PDT) Received: from hyeyoo.. ([114.29.24.243]) by smtp.gmail.com with ESMTPSA id p9-20020aa79e89000000b00505fada20dfsm1403537pfq.117.2022.04.14.01.59.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Apr 2022 01:59:59 -0700 (PDT) From: Hyeonggon Yoo <42.hyeyoo@gmail.com> To: Vlastimil Babka Cc: Marco Elver , Matthew WilCox , Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Andrew Morton , Hyeonggon Yoo <42.hyeyoo@gmail.com>, Roman Gushchin , linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 23/23] mm/sl[au]b: check if large object is valid in __ksize() Date: Thu, 14 Apr 2022 17:57:27 +0900 Message-Id: <20220414085727.643099-24-42.hyeyoo@gmail.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220414085727.643099-1-42.hyeyoo@gmail.com> References: <20220414085727.643099-1-42.hyeyoo@gmail.com> MIME-Version: 1.0 X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 72273180008 X-Stat-Signature: huwyeta77nqynopkiy1it8zxs88z97wz Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=aleL4N3z; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf06.hostedemail.com: domain of 42.hyeyoo@gmail.com designates 209.85.214.174 as permitted sender) smtp.mailfrom=42.hyeyoo@gmail.com X-Rspam-User: X-HE-Tag: 1649926801-853614 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: __ksize() returns size of objects allocated from slab allocator. When invalid object is passed to __ksize(), returning zero prevents further memory corruption and makes caller be able to check if there is an error. If address of large object is not beginning of folio or size of the folio is too small, it must be invalid. Return zero in such cases. Suggested-by: Vlastimil Babka Signed-off-by: Hyeonggon Yoo <42.hyeyoo@gmail.com> --- mm/slab_common.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/mm/slab_common.c b/mm/slab_common.c index 8facade42bdd..a14f9990b159 100644 --- a/mm/slab_common.c +++ b/mm/slab_common.c @@ -994,8 +994,12 @@ size_t __ksize(const void *object) folio = virt_to_folio(object); - if (unlikely(!folio_test_slab(folio))) + if (unlikely(!folio_test_slab(folio))) { + if (object != folio_address(folio) || + folio_size(folio) <= KMALLOC_MAX_CACHE_SIZE) + return 0; return folio_size(folio); + } return slab_ksize(folio_slab(folio)->slab_cache); }