Received: by 2002:a25:ab43:0:0:0:0:0 with SMTP id u61csp6278905ybi; Wed, 29 May 2019 05:41:14 -0700 (PDT) X-Google-Smtp-Source: APXvYqwvYhyubF8gX5LrQ+ubkm8VbZrsQJaQvLZWUabUyCFbzvOEat1/cgOfdQG+oXsinJPbVdmQ X-Received: by 2002:a17:90a:7344:: with SMTP id j4mr12271950pjs.71.1559133674003; Wed, 29 May 2019 05:41:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1559133673; cv=none; d=google.com; s=arc-20160816; b=OfUpP0x+kRyYQB/e83gRt87JeC1wFXh/JllnXaAQ7QSdlLDySTjaIfNwkNe8YiRKzm ro3KwtF3pdf9ZuVdbGP6dn+WhTp60tq38R3bu/QLMK8mnCtPDCw6uoiv1xKUZmeGYtwT EpMF9FgB2aay1B33MRgv/OMAI53UAfD4KTKWBAW89fobaVPSY5a4VdiECUCW7byTkdPB diowlh8OH8ePf6lhM1xpS6lykUxTS6vZ1JPF+7JJiJ9sWa2BDbB0WioUWS+XNlwPXFE9 rC40QaTuZFZheqmgh6olyvk5+hbTOyfcK8dCgpIWd0Ais/cpSbLM9M2AeD8FsOGHCd3l /adQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from :dkim-signature; bh=/kUApmsHSgHDArihyUOta/cOfsm+uYS/iQzyr+N/nRo=; b=b4PrDuJmm2LcfGKAZNeqgexudM5vhKYkgVBydNLxfmcCW4U9mcFgGh5eAPnkjv6REp 7Dcrb2or2M9k0zfAi8V/mzEp0km/n0K6jV/Q7G+PuIsLMadOW3HZgVDrMpNXPdhRz0+K EcJziQMnIpWkWEe70VD5anjD6JC8Aih+AhQndki/dZvF/PyuCs6E+/zqm95vnMo9cxcq LmERN21niggRyaYhPwry1c1FZ8GbNnkrVTjhwN0B9MzVhAz1mfblmvFouCn2hk/KPNmB f+j1ApUzDOFMmIinBOIXHwaYzfsYXmbGgy7wrcxIj8XVjTnWnY4BCTbwuEQkWJ/s6DDR hTEg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=juZrse+P; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 204si29663549pfz.86.2019.05.29.05.40.57; Wed, 29 May 2019 05:41:13 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=juZrse+P; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727106AbfE2Mjh (ORCPT + 99 others); Wed, 29 May 2019 08:39:37 -0400 Received: from mail-pl1-f196.google.com ([209.85.214.196]:36793 "EHLO mail-pl1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727053AbfE2Mjg (ORCPT ); Wed, 29 May 2019 08:39:36 -0400 Received: by mail-pl1-f196.google.com with SMTP id d21so1025725plr.3 for ; Wed, 29 May 2019 05:39:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=/kUApmsHSgHDArihyUOta/cOfsm+uYS/iQzyr+N/nRo=; b=juZrse+P5GXwZ9Mr9TkKumoLScyloKiktxo5FhhDWNI7LcrTnkDaRqPI33pWp7lXci 4Ogtx/AQdl1KsvONOwuev6kNPUPM7CSDW1fzkPN+LGbHwi7km0sPP4OMeR93q1fgBzye acY72AJ8WkHQrDvdd8IHCMJnECysgVh5O2ooFvvhKi4JczqhdeEOyMJTbXpGqB8ammkC OTDsmGibDQ53bftt6AioXhgzAP2WDApUJVAb6tXG/l01IivWJOuWNv3mwvb48Xi5K71X XNHfJPlGTIDtNpYOj+rLNhh01dazq3y2O90/r7WVLu8jPnmVSDPbTRjhO3pUdkogLqLZ ISCw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=/kUApmsHSgHDArihyUOta/cOfsm+uYS/iQzyr+N/nRo=; b=o5IE+gtl4YimMPSj4fw5P8fMA8PfRtXIu4CYdNw9V6mAZGGNpVyhd7nGb2oaxPJSE8 B7lf55j7Fe03Qrpk48D0p3kpReTwSgqAELyHkJ0fn5iNDdnMpfCx5muqNirL9EL0Gc7f CAvhiOZBuXtuShEGPaGkUQcQsb6yOxd/6vbTHQOhIChxGLh1WJ6qaMrXg9zPE8s/cUo8 WsewDlYu6/jfzoeXkcXtMoPWj7eqjCia4pknVRfPza0OcLEezrq9UvRKjTK8NnloL1ZC eaMtSJh3jHkoQMGCoJfy3PGq8EYWy7GuCn4hr+OXgX2Kb2LbZ4MaRT9lQgPPLiOVCiDX ZFvA== X-Gm-Message-State: APjAAAWxR+bn+Nxqej3XBM1POpjATmDE1tOzBzJM+GR69vLkISDnmr3Z o6VqTjd8trQDOGIlj7nWIzQ= X-Received: by 2002:a17:902:bc86:: with SMTP id bb6mr3670959plb.129.1559133576415; Wed, 29 May 2019 05:39:36 -0700 (PDT) Received: from tom-pc.ipads-lab.se.sjtu.edu.cn ([202.120.40.82]) by smtp.gmail.com with ESMTPSA id 124sm19018905pfe.124.2019.05.29.05.39.32 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 29 May 2019 05:39:35 -0700 (PDT) From: Dianzhang Chen To: cl@linux.com Cc: penberg@kernel.org, rientjes@google.com, iamjoonsoo.kim@lge.com, akpm@linux-foundation.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Dianzhang Chen Subject: [PATCH] mm/slab_common.c: fix possible spectre-v1 in kmalloc_slab() Date: Wed, 29 May 2019 20:37:28 +0800 Message-Id: <1559133448-31779-1-git-send-email-dianzhangchen0@gmail.com> X-Mailer: git-send-email 2.7.4 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The `size` in kmalloc_slab() is indirectly controlled by userspace via syscall: poll(defined in fs/select.c), hence leading to a potential exploitation of the Spectre variant 1 vulnerability. The `size` can be controlled from: poll -> do_sys_poll -> kmalloc -> __kmalloc -> kmalloc_slab. Fix this by sanitizing `size` before using it to index size_index. Signed-off-by: Dianzhang Chen --- mm/slab_common.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/mm/slab_common.c b/mm/slab_common.c index 58251ba..41c7e34 100644 --- a/mm/slab_common.c +++ b/mm/slab_common.c @@ -21,6 +21,7 @@ #include #include #include +#include #define CREATE_TRACE_POINTS #include @@ -1056,6 +1057,7 @@ struct kmem_cache *kmalloc_slab(size_t size, gfp_t flags) if (!size) return ZERO_SIZE_PTR; + size = array_index_nospec(size, 193); index = size_index[size_index_elem(size)]; } else { if (WARN_ON_ONCE(size > KMALLOC_MAX_CACHE_SIZE)) -- 2.7.4