Received: by 2002:ac0:a5b6:0:0:0:0:0 with SMTP id m51-v6csp2267601imm; Thu, 7 Jun 2018 07:59:06 -0700 (PDT) X-Google-Smtp-Source: ADUXVKK+tMQR2DQ4J8nevvNJNRTEjUJOWXuHFe4y4ELoZAwnsf/tEUbzkuwtz5l6RrOwZlZmu9XW X-Received: by 2002:a65:46cb:: with SMTP id n11-v6mr1820210pgr.193.1528383546650; Thu, 07 Jun 2018 07:59:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1528383546; cv=none; d=google.com; s=arc-20160816; b=pkYK5z6zerN4M6yJGyuX1CNlyHkJF8XusS+Kub45fdKbijFqUsG7UuSwyOjMSMwqoH aeaewv5hEyMg3WhggwEAcxCWEDgpfp2u2pTqDvQ0RUNBKiVJcJhYE632lXYol1HenJJG wtHj1zdvp9lFi+rWUX4er3/Gk1pzl8mvyi7Vru8B/7FeVAvS/qADRHqW9D0IjVPESlE6 8uE/yUNBO6d5/7fdnpRWcvCSDqSzjZjoLFJt7DPO1X2Xptv9OuVRPz3tixhGc+LVG2lO DtJtH35uIIEWKZA/VkP3GSxcYiXN/o2pI18bE+RWYyiFeRLi581kzkjLuHguaIMyeSWq kgOA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:subject:message-id:date:cc:to :from:mime-version:content-transfer-encoding:content-disposition :arc-authentication-results; bh=88GWlsFQIrwLABEO8j6dEYGkEFHP9VUPab49F3h6nkg=; b=mPfrr3Ft1nPhDrHMY5gYCmG6W4c+23LoxYPCFlArPYFYIovgeSdYWxT98Y9aY9ZXkf mSaCIyRv6I1sySkGQBNwYXro24LXl7JeelojTZMznmtnMmHcQuPJaS0NbE2g5WG0vySm OGE1iCjxz21GC3SeRKF0B3Yio5IgSOfJUCiwlByv8LTk32OVzpAuIT4Y7k1cSZO6/A06 vaDa2KetkcDD3Z4sb+daDbZM3OZgWAenHk25fmrG3E6oJweUOBYsSYP+DrQxmvwhMptR 2lTMqMm+HxordGlJN+m5UejXCozWcTtcYH4THzFT/LIO9DjijY+6euyApa4k8EX0eCU4 cNgw== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id j193-v6si28721977pge.371.2018.06.07.07.58.51; Thu, 07 Jun 2018 07:59:06 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S935189AbeFGO5l (ORCPT + 99 others); Thu, 7 Jun 2018 10:57:41 -0400 Received: from shadbolt.e.decadent.org.uk ([88.96.1.126]:41128 "EHLO shadbolt.e.decadent.org.uk" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S933308AbeFGO5j (ORCPT ); Thu, 7 Jun 2018 10:57:39 -0400 Received: from [148.252.241.226] (helo=deadeye) by shadbolt.decadent.org.uk with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1fQvbg-0005Zl-FG; Thu, 07 Jun 2018 15:09:40 +0100 Received: from ben by deadeye with local (Exim 4.91) (envelope-from ) id 1fQvb6-000303-8O; Thu, 07 Jun 2018 15:09:04 +0100 Content-Type: text/plain; charset="UTF-8" Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 From: Ben Hutchings To: linux-kernel@vger.kernel.org, stable@vger.kernel.org CC: akpm@linux-foundation.org, "Josh Poimboeuf" , "Linus Torvalds" , "Jens Axboe" , "Mikulas Patocka" , "Joe Lawrence" , "Al Viro" , "Randy Dunlap" , "Michael Kerrisk" Date: Thu, 07 Jun 2018 15:05:21 +0100 Message-ID: X-Mailer: LinuxStableQueue (scripts by bwh) Subject: [PATCH 3.16 207/410] pipe: avoid round_pipe_size() nr_pages overflow on 32-bit In-Reply-To: X-SA-Exim-Connect-IP: 148.252.241.226 X-SA-Exim-Mail-From: ben@decadent.org.uk X-SA-Exim-Scanned: No (on shadbolt.decadent.org.uk); SAEximRunCond expanded to false Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 3.16.57-rc1 review patch. If anyone has any objections, please let me know. ------------------ From: Joe Lawrence commit d3f14c485867cfb2e0c48aa88c41d0ef4bf5209c upstream. round_pipe_size() contains a right-bit-shift expression which may overflow, which would cause undefined results in a subsequent roundup_pow_of_two() call. static inline unsigned int round_pipe_size(unsigned int size) { unsigned long nr_pages; nr_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT; return roundup_pow_of_two(nr_pages) << PAGE_SHIFT; } PAGE_SIZE is defined as (1UL << PAGE_SHIFT), so: - 4 bytes wide on 32-bit (0 to 0xffffffff) - 8 bytes wide on 64-bit (0 to 0xffffffffffffffff) That means that 32-bit round_pipe_size(), nr_pages may overflow to 0: size=0x00000000 nr_pages=0x0 size=0x00000001 nr_pages=0x1 size=0xfffff000 nr_pages=0xfffff size=0xfffff001 nr_pages=0x0 << ! size=0xffffffff nr_pages=0x0 << ! This is bad because roundup_pow_of_two(n) is undefined when n == 0! 64-bit is not a problem as the unsigned int size is 4 bytes wide (similar to 32-bit) and the larger, 8 byte wide unsigned long, is sufficient to handle the largest value of the bit shift expression: size=0xffffffff nr_pages=100000 Modify round_pipe_size() to return 0 if n == 0 and updates its callers to handle accordingly. Link: http://lkml.kernel.org/r/1507658689-11669-3-git-send-email-joe.lawrence@redhat.com Signed-off-by: Joe Lawrence Reported-by: Mikulas Patocka Reviewed-by: Mikulas Patocka Cc: Al Viro Cc: Jens Axboe Cc: Michael Kerrisk Cc: Randy Dunlap Cc: Josh Poimboeuf Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds [bwh: Backported to 3.16: adjust context] Signed-off-by: Ben Hutchings --- fs/pipe.c | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) --- a/fs/pipe.c +++ b/fs/pipe.c @@ -1006,13 +1006,19 @@ const struct file_operations pipefifo_fo /* * Currently we rely on the pipe array holding a power-of-2 number - * of pages. + * of pages. Returns 0 on error. */ static inline unsigned int round_pipe_size(unsigned int size) { unsigned long nr_pages; + if (size < pipe_min_size) + size = pipe_min_size; + nr_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT; + if (nr_pages == 0) + return 0; + return roundup_pow_of_two(nr_pages) << PAGE_SHIFT; } @@ -1028,6 +1034,8 @@ static long pipe_set_size(struct pipe_in long ret = 0; size = round_pipe_size(arg); + if (size == 0) + return -EINVAL; nr_pages = size >> PAGE_SHIFT; if (!nr_pages) @@ -1110,13 +1118,18 @@ out_revert_acct: int pipe_proc_fn(struct ctl_table *table, int write, void __user *buf, size_t *lenp, loff_t *ppos) { + unsigned int rounded_pipe_max_size; int ret; ret = proc_dointvec_minmax(table, write, buf, lenp, ppos); if (ret < 0 || !write) return ret; - pipe_max_size = round_pipe_size(pipe_max_size); + rounded_pipe_max_size = round_pipe_size(pipe_max_size); + if (rounded_pipe_max_size == 0) + return -EINVAL; + + pipe_max_size = rounded_pipe_max_size; return ret; }