Received: by 2002:a25:ab43:0:0:0:0:0 with SMTP id u61csp2414983ybi; Sun, 9 Jun 2019 11:11:02 -0700 (PDT) X-Google-Smtp-Source: APXvYqx5yFLRawdCHhKnM8577LLOKWf7/thUbPeTJRuza6o9QkNcRb4hkag4TOjMVWG3fdNc6hKa X-Received: by 2002:aa7:8157:: with SMTP id d23mr70402167pfn.92.1560103861919; Sun, 09 Jun 2019 11:11:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1560103861; cv=none; d=google.com; s=arc-20160816; b=egZt4W1JuZ0eTwy9nh3juIadroZBU+4+1FnYNFtRsUygH5Sr7xKGGRAsL+uiWEzvI2 LxkrdMl4MrnWK46btDN478ho7W5tVy7iJACawSZhW5Wck4lq8osFgCHFlD12/7F12rc7 DWM2taNDySDjnYYqEtqF8NWGYTioU+xswSihez3s3DkBHOkRKP2XXCgCviwQqVk8NHjJ I99zGCoVOdhU0sJHunoymSuUzMFvGMt1b8cSnxd3MQvA4O3qqhqs0w/Y9MGTUaqkiEQe 5wAqVUEQ96s9CEirqKqd9LuNiinxjopwwtUWWFMr5Hok21HdICmZB8Kns28aHdvCrc6T +hgg== 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=6eO8AC9tdJ9amjaqNQx6lB+fBCNEhLNrPZCWo8E7pGw=; b=Ca/3B3E/P6iqLkz7i9p3JRJ00WbL12sVcASBBPmaux7oFOzUhqCEuCeDpgUyE11Le0 UP4pAXUmKtBUiQJhaP0SaggxtpUtjT2F37sCoxuOlddLZlkeTJjQ3YoYHCVfxLEMS7E+ aXK8PXXv55Q2E2WRhqwkoC7iOTxYmaeUa3e2sxzrshLQaXHAq8RSGuK4q1+GW4LswhjZ juoWn2WHWlpiY4N/q3w84tkDs/nTvoDNsN3ZcTkyCNDYVUCqq1ClqPt+V/XzPQySABEl QU9t+ldRKYK04v+dEcdVTmsReDwmDZZq0JOX/8sb5fPG68S8nGMhSOmK9n8QsFq6bUZZ XeOg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=h5BVxtHT; 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 p19si7527391plo.212.2019.06.09.11.10.46; Sun, 09 Jun 2019 11:11:01 -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=@kernel.org header.s=default header.b=h5BVxtHT; 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 S1732769AbfFIQzV (ORCPT + 99 others); Sun, 9 Jun 2019 12:55:21 -0400 Received: from mail.kernel.org ([198.145.29.99]:57400 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732748AbfFIQzT (ORCPT ); Sun, 9 Jun 2019 12:55:19 -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 98BC2207E0; Sun, 9 Jun 2019 16:55:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1560099319; bh=lTiKP8Sokrx2urBRTyq5x4MBbdM2mVCqeQs98ICc6i8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=h5BVxtHT8Sl7f15lJLC9QqG8yHPpw//QXz5LpUw9VWOm5ALofPLy6hGWGhec844Xi IElgodtRGYuClN/UDFkxe11ugbAMob5gvYcrVXjkbo7SXOYkFVwwCGjuK+OCtwrW5k RHgc2LlfjG0XGaViwqf5E2Hf5boZkEMysl8vnATc= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Vivien Didelot , Michal Kubecek , "David S. Miller" Subject: [PATCH 4.9 62/83] ethtool: fix potential userspace buffer overflow Date: Sun, 9 Jun 2019 18:42:32 +0200 Message-Id: <20190609164133.122061942@linuxfoundation.org> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190609164127.843327870@linuxfoundation.org> References: <20190609164127.843327870@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: Vivien Didelot [ Upstream commit 0ee4e76937d69128a6a66861ba393ebdc2ffc8a2 ] ethtool_get_regs() allocates a buffer of size ops->get_regs_len(), and pass it to the kernel driver via ops->get_regs() for filling. There is no restriction about what the kernel drivers can or cannot do with the open ethtool_regs structure. They usually set regs->version and ignore regs->len or set it to the same size as ops->get_regs_len(). But if userspace allocates a smaller buffer for the registers dump, we would cause a userspace buffer overflow in the final copy_to_user() call, which uses the regs.len value potentially reset by the driver. To fix this, make this case obvious and store regs.len before calling ops->get_regs(), to only copy as much data as requested by userspace, up to the value returned by ops->get_regs_len(). While at it, remove the redundant check for non-null regbuf. Signed-off-by: Vivien Didelot Reviewed-by: Michal Kubecek Signed-off-by: David S. Miller Signed-off-by: Greg Kroah-Hartman --- net/core/ethtool.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) --- a/net/core/ethtool.c +++ b/net/core/ethtool.c @@ -1390,13 +1390,16 @@ static int ethtool_get_regs(struct net_d return -ENOMEM; } + if (regs.len < reglen) + reglen = regs.len; + ops->get_regs(dev, ®s, regbuf); ret = -EFAULT; if (copy_to_user(useraddr, ®s, sizeof(regs))) goto out; useraddr += offsetof(struct ethtool_regs, data); - if (regbuf && copy_to_user(useraddr, regbuf, regs.len)) + if (copy_to_user(useraddr, regbuf, reglen)) goto out; ret = 0;