Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp4406816pxa; Mon, 10 Aug 2020 08:22:52 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwt9Eo/+ni0CqrLIK+h9tRIE/JdxvYjaHv5KCovyzBnlmZ3BJAbXBwR9eySAyXkM+b/ppif X-Received: by 2002:a17:906:73d9:: with SMTP id n25mr4085070ejl.412.1597072972403; Mon, 10 Aug 2020 08:22:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1597072972; cv=none; d=google.com; s=arc-20160816; b=P8CdgFKhNKYmzd+yOq6GsphHY9arckpAiQygK542lFqN1fsCMN1oRjL//IdQ9o/GHe 4tjzFn0YjQbEbfgYMH8zyy/fzAiahfSdvXYxXpNT3GCOA8b/JTyl6EM8U7wRB1+nSSP9 4YU5Ax01xfstSQ3GUvBUjeAyZ7ga1mD5eqvDKK9t3pwtmat16qDo87qyXvvwjam85zoo sGABdV7JhXnMZ68Wx2FMNxVU+G7wT9FHs55yJBCJBsPtK4nCP/EdrXK/W6qTSL6Od/oW BrEc1hNyHnvkGTGbbaM+wZoR611zgAzM+6Rqwpn1qmT+3rBdFRqGhUK7zscazBXkFQAS zIKQ== 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:dkim-signature; bh=+ex0HSDqvvKCiFbXC3KvruyFyWkJ7Nan6zEarBilo8Y=; b=SGfDv7EzZ2zoUnL1ICI9gj512hDgbtyrjarOpbTXYDRNWziEcxijdcFuIjX6DpAgiP rbaLeg+aSEvbWM2Yf89lT1HdOoY+COV3eMeTt7xQmuS6LDQ30tULbUPDTqq4aUb7EhoW XHqayvGI5X52lp7l+EzKNWukb4S76b/Hym22HA8/HliBhQM3gcE0+NHUmAO4BbVo3L7g GXkGtPO3qyN7Pk8ofnk4tIGDX8f3ypoYRDDZwqvhDuo0WWe1hqzKJIMM+soFXZRwcbsU Ydy0AxXlI9WjmpCocA6idwJoYauPG2RN472mL5Qd5WcqesXORLc0RIYWQLDjPLgDjNSe Trqg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=m8FM7LNm; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id a22si11024388ejj.163.2020.08.10.08.22.29; Mon, 10 Aug 2020 08:22:52 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=m8FM7LNm; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728144AbgHJPVJ (ORCPT + 99 others); Mon, 10 Aug 2020 11:21:09 -0400 Received: from mail.kernel.org ([198.145.29.99]:51780 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728105AbgHJPVC (ORCPT ); Mon, 10 Aug 2020 11:21:02 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 060D92075D; Mon, 10 Aug 2020 15:21:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1597072861; bh=Nn+bQBHPyRbZAZycSa5N0hcKvQctVS7s3iooCst5c9A=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=m8FM7LNmKXNofWeNkh/Wiph+cKP/JG7q81PrLF2u5ubWPpXwMIsXi1ShPXvmNt6oY 8PvV8zv9zodJ72VrIW9csrr/JcgQiLP5lB79Bgdy0wrahYBJUIC7JfcMUFDZEl/QPP W4n2RJKiyBiTLXDeEvmPtsnGxBGBOotPusObsLbU= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Miquel Raynal , Richard Weinberger , Vignesh Raghavendra , stable Subject: [PATCH 5.8 24/38] mtd: properly check all write ioctls for permissions Date: Mon, 10 Aug 2020 17:19:14 +0200 Message-Id: <20200810151805.080238265@linuxfoundation.org> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200810151803.920113428@linuxfoundation.org> References: <20200810151803.920113428@linuxfoundation.org> User-Agent: quilt/0.66 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 From: Greg Kroah-Hartman commit f7e6b19bc76471ba03725fe58e0c218a3d6266c3 upstream. When doing a "write" ioctl call, properly check that we have permissions to do so before copying anything from userspace or anything else so we can "fail fast". This includes also covering the MEMWRITE ioctl which previously missed checking for this. Cc: Miquel Raynal Cc: Richard Weinberger Cc: Vignesh Raghavendra Cc: stable Signed-off-by: Greg Kroah-Hartman [rw: Fixed locking issue] Signed-off-by: Richard Weinberger Signed-off-by: Greg Kroah-Hartman --- drivers/mtd/mtdchar.c | 56 +++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 47 insertions(+), 9 deletions(-) --- a/drivers/mtd/mtdchar.c +++ b/drivers/mtd/mtdchar.c @@ -355,9 +355,6 @@ static int mtdchar_writeoob(struct file uint32_t retlen; int ret = 0; - if (!(file->f_mode & FMODE_WRITE)) - return -EPERM; - if (length > 4096) return -EINVAL; @@ -643,6 +640,48 @@ static int mtdchar_ioctl(struct file *fi pr_debug("MTD_ioctl\n"); + /* + * Check the file mode to require "dangerous" commands to have write + * permissions. + */ + switch (cmd) { + /* "safe" commands */ + case MEMGETREGIONCOUNT: + case MEMGETREGIONINFO: + case MEMGETINFO: + case MEMREADOOB: + case MEMREADOOB64: + case MEMLOCK: + case MEMUNLOCK: + case MEMISLOCKED: + case MEMGETOOBSEL: + case MEMGETBADBLOCK: + case MEMSETBADBLOCK: + case OTPSELECT: + case OTPGETREGIONCOUNT: + case OTPGETREGIONINFO: + case OTPLOCK: + case ECCGETLAYOUT: + case ECCGETSTATS: + case MTDFILEMODE: + case BLKPG: + case BLKRRPART: + break; + + /* "dangerous" commands */ + case MEMERASE: + case MEMERASE64: + case MEMWRITEOOB: + case MEMWRITEOOB64: + case MEMWRITE: + if (!(file->f_mode & FMODE_WRITE)) + return -EPERM; + break; + + default: + return -ENOTTY; + } + switch (cmd) { case MEMGETREGIONCOUNT: if (copy_to_user(argp, &(mtd->numeraseregions), sizeof(int))) @@ -690,9 +729,6 @@ static int mtdchar_ioctl(struct file *fi { struct erase_info *erase; - if(!(file->f_mode & FMODE_WRITE)) - return -EPERM; - erase=kzalloc(sizeof(struct erase_info),GFP_KERNEL); if (!erase) ret = -ENOMEM; @@ -985,9 +1021,6 @@ static int mtdchar_ioctl(struct file *fi ret = 0; break; } - - default: - ret = -ENOTTY; } return ret; @@ -1031,6 +1064,11 @@ static long mtdchar_compat_ioctl(struct struct mtd_oob_buf32 buf; struct mtd_oob_buf32 __user *buf_user = argp; + if (!(file->f_mode & FMODE_WRITE)) { + ret = -EPERM; + break; + } + if (copy_from_user(&buf, argp, sizeof(buf))) ret = -EFAULT; else