Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp2519453imm; Mon, 24 Sep 2018 05:50:42 -0700 (PDT) X-Google-Smtp-Source: ACcGV624ojoaoTOpp3BpX9FEMfN33w4m6QnUgvW6tgXtoar5Fiz0KeiRUToo+gMM6aZfy1IOG4p8 X-Received: by 2002:a17:902:6501:: with SMTP id b1-v6mr10645444plk.31.1537793442501; Mon, 24 Sep 2018 05:50:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1537793442; cv=none; d=google.com; s=arc-20160816; b=pKJOSV6ODprXq2OQwUOMDLqkKEs03zcTacJowyPYx5rtnX9WAuyRApgPgldgy8+HO3 yKB//HAQufeFdMH/cApzxdiVT4kHIEqyxjm357vdsWDedei4nfv2IotoPEWBnt+9whA7 30hzMDF1zeNpgTLkFLHVLvg1JEFxS/+92ldMkqunntpNpEeXKugJ0drXWOXvD/TgJ3DY 4ZrUz5ySA4p3b0u0NDaDEaeZ6qg79U+xqgarij7eatEDNxtK48O8Xso11vLmHK5h23lU 8Du9PeuYCsQ4sUbS8Qb39vCXHJNkjbTlOg4ocbFA3/uwOLhR56bpK1HXqMcm7yP8IML5 PjWQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from; bh=4ZyXTwPvjUF4D6Ce3PXrgbJtB6wszgA5i7vpjGB5VjE=; b=UAI55A4c/nL+YhRpZepP7S4i+egJ+ajZNkhD4J589x21IbEXmAOIawje1/2VOQfeHh w/jIRj4nsT4aY3i76nZ+7JDLzNRe0E5yPXrvj30EBHrdCpLbE1LgXL/T/RSt2oZj1lrQ FwRNJ76E38ah6ePQuNoraYcwXaZgvWNhf1sGSzMDUlp+gDDBfIQqf6qs1HhDh/FsSI8h Jz0d8Dhg4HIghEZfAQHEzpJmT5nn+ovlZIFmujfs+GFpskcuPs3nmlXLgyvZCu+qKJIa B3YxZHtoHoqOn4udYt1QhYH+F+GVjJnCHJxkkdrK6x9RKvOVV5ghqi3vWgR/IjOyRga3 MT8w== 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 d4-v6si37469117pfc.219.2018.09.24.05.50.27; Mon, 24 Sep 2018 05:50:42 -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 S2388501AbeIXSlq (ORCPT + 99 others); Mon, 24 Sep 2018 14:41:46 -0400 Received: from mail.linuxfoundation.org ([140.211.169.12]:58884 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731437AbeIXSlp (ORCPT ); Mon, 24 Sep 2018 14:41:45 -0400 Received: from localhost (ip-213-127-77-73.ip.prioritytelecom.net [213.127.77.73]) by mail.linuxfoundation.org (Postfix) with ESMTPSA id B05491099; Mon, 24 Sep 2018 12:39:46 +0000 (UTC) From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Jann Horn , Boris Brezillon , Sasha Levin Subject: [PATCH 4.18 150/235] mtdchar: fix overflows in adjustment of `count` Date: Mon, 24 Sep 2018 13:52:16 +0200 Message-Id: <20180924113120.400607752@linuxfoundation.org> X-Mailer: git-send-email 2.19.0 In-Reply-To: <20180924113103.999624566@linuxfoundation.org> References: <20180924113103.999624566@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.18-stable review patch. If anyone has any objections, please let me know. ------------------ From: Jann Horn [ Upstream commit 6c6bc9ea84d0008024606bf5ba10519e20d851bf ] The first checks in mtdchar_read() and mtdchar_write() attempt to limit `count` such that `*ppos + count <= mtd->size`. However, they ignore the possibility of `*ppos > mtd->size`, allowing the calculation of `count` to wrap around. `mtdchar_lseek()` prevents seeking beyond mtd->size, but the pread/pwrite syscalls bypass this. I haven't found any codepath on which this actually causes dangerous behavior, but it seems like a sensible change anyway. Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2") Signed-off-by: Jann Horn Signed-off-by: Boris Brezillon Signed-off-by: Sasha Levin Signed-off-by: Greg Kroah-Hartman --- drivers/mtd/mtdchar.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) --- a/drivers/mtd/mtdchar.c +++ b/drivers/mtd/mtdchar.c @@ -160,8 +160,12 @@ static ssize_t mtdchar_read(struct file pr_debug("MTD_read\n"); - if (*ppos + count > mtd->size) - count = mtd->size - *ppos; + if (*ppos + count > mtd->size) { + if (*ppos < mtd->size) + count = mtd->size - *ppos; + else + count = 0; + } if (!count) return 0; @@ -246,7 +250,7 @@ static ssize_t mtdchar_write(struct file pr_debug("MTD_write\n"); - if (*ppos == mtd->size) + if (*ppos >= mtd->size) return -ENOSPC; if (*ppos + count > mtd->size)