Received: by 2002:a05:6a10:1d13:0:0:0:0 with SMTP id pp19csp243654pxb; Thu, 26 Aug 2021 02:01:30 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzpG10XHSBGYykNnn0eIaImp2Lkdw6hk58bqpITELIcmzwggIST6KLmixFWQwPSoRSsVMDB X-Received: by 2002:a17:906:f15:: with SMTP id z21mr3139832eji.177.1629968490772; Thu, 26 Aug 2021 02:01:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1629968490; cv=none; d=google.com; s=arc-20160816; b=ymJwC+SYJ1B3KUDsvJW8leN7vqGEq1d1tW3iKrxE9YXhBwECDYFpYEtBLTtCsIqV3N xtMXQlWlRUZEhgcJrC59iSwVKXeEtzjhpwvgG5B7xjhQs+Dk88/sSZ/Eitj4gPsSOnPL yqk1T5vjf4VSjKG1Gd147wZzhDwWbKXlrPwFgWLH/TtGxjFstKpgXGLhL+N6pKI/89mi vDZy9liGFXZSVZu6jJazmbtuLjP+bgw0xqSeWIzj4KwAHqibygWoF266GrUGypic9h9T yZ737KK7xsfa0M1VJt4yVVbIWRMCLQQAmhyhtSwNhEW9nvU9N0MLmCrIfHu7CKQ2E32B PSHg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=SkSM+eipzAiLCDXSidzjvOtoRle8x/ViDdx5kiZiLdc=; b=cwsaHwOgkFtdY5IAmPViD+Sk3A6xUIEN3Fc59KDo+V469z4jZsBc8P62BCYFuQLT5F jof9joVwR/uIu5gunYRPPNrFcHAPm+YUfv+tgyE3AgClA24Lbkah9Gub8QapUCHvr6D/ ew3x4H3z/SlAn6aQSe1tCypuqQrcupzhdToanNXwL3xmVtIHuka9fPbifx4DkZ1FNR8z QtiU7MW5lLJraNnZO4VJdpFtrbwx5RWW8Z0Y9iIBXOjgvF95nKw879GbmuypwJ/XBxLf L8ehLQtfSLMq1KxSYt2mueYLVGXslCtoYnKngcitGuMgqhE8z8n0cf6Du1Nr9mqIZ5z/ 5hdg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=uYUXDnss; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id m1si2416506edc.299.2021.08.26.02.01.02; Thu, 26 Aug 2021 02:01:30 -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=k20201202 header.b=uYUXDnss; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240598AbhHZI7t (ORCPT + 99 others); Thu, 26 Aug 2021 04:59:49 -0400 Received: from mail.kernel.org ([198.145.29.99]:53406 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240351AbhHZI7s (ORCPT ); Thu, 26 Aug 2021 04:59:48 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 4C8B06109F; Thu, 26 Aug 2021 08:59:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1629968341; bh=zJGos2uCxfN4MJ+pMOdh+9WxhFJAzd4XefzJgxvmsY0=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=uYUXDnssrl/MJ6BBhvlyYlaoEOQ/Lm/gsRXdjxk0IzR7wQ2XDs3ng9SSAw5q5y9i9 OLHYypmvctowYZ5EhGJBc32FKnF2OLiBHlVlZEzU8kIL0IfmB/XaOrGWEo3wHOdqLy VTuZ/q/7prF353IFXYjDetNf6Q+KXfh1HVJqnsE9RoZTn7XTKtmyV3bqnRCs6UAZAR xAyAoZv5TAJv3ZAkmTSc+Q3dfiodFW77x2916o7EYLe23hmUvrpUeldD8G1Ct1YqDm PpPcri/E45ahheQDNcTQ93S36YM4WgTNJeEEk3zSWyXbuov23kCgDLVL6kI9Ijn/VM d/upSmckY3Z3Q== Received: by mail-wr1-f53.google.com with SMTP id x12so3787426wrr.11; Thu, 26 Aug 2021 01:59:01 -0700 (PDT) X-Gm-Message-State: AOAM532mye+RFa/4DT9Dr/iLrJFYDA2uXWucA6ZM3Dxsf2XCb9TJ3XF0 RkCih9WngM8u/O6wjoo+s1taWcHzI9SBPEU9dGs= X-Received: by 2002:adf:e107:: with SMTP id t7mr2589248wrz.165.1629968339982; Thu, 26 Aug 2021 01:58:59 -0700 (PDT) MIME-Version: 1.0 References: <20210826012722.3210359-1-pcc@google.com> In-Reply-To: <20210826012722.3210359-1-pcc@google.com> From: Arnd Bergmann Date: Thu, 26 Aug 2021 10:58:44 +0200 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH] net: don't unconditionally copy_from_user a struct ifreq for socket ioctls To: Peter Collingbourne Cc: "David S. Miller" , Jakub Kicinski , Colin Ian King , Cong Wang , Al Viro , Networking , Linux Kernel Mailing List , "# 3.4.x" Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Aug 26, 2021 at 3:28 AM Peter Collingbourne wrote: > - } else { > + } else if (is_dev_ioctl_cmd(cmd)) { > struct ifreq ifr; > bool need_copyout; > if (copy_from_user(&ifr, argp, sizeof(struct ifreq))) > @@ -1118,6 +1118,8 @@ static long sock_do_ioctl(struct net *net, struct socket *sock, > if (!err && need_copyout) > if (copy_to_user(argp, &ifr, sizeof(struct ifreq))) > return -EFAULT; > + } else { > + err = -ENOTTY; > } > return err; > } > @@ -3306,6 +3308,8 @@ static int compat_ifr_data_ioctl(struct net *net, unsigned int cmd, > struct ifreq ifreq; > u32 data32; > > + if (!is_dev_ioctl_cmd(cmd)) > + return -ENOTTY; > if (copy_from_user(ifreq.ifr_name, u_ifreq32->ifr_name, IFNAMSIZ)) > return -EFAULT; > if (get_user(data32, &u_ifreq32->ifr_data)) This adds yet another long switch() statement into the socket ioctl case, when there is already one in compat_sock_ioctl_trans(), one in dev_ifsioc() and one in dev_ioctl(), all with roughly the same set of ioctl command codes. If any of them are called frequently, that makes it all even slower, so I wonder if there should be a larger rework altogether. Maybe something based on a single lookup table that we search through directly from sock_ioctl()/compat_sock_ioctl() to deal with the differences in handling (ifreq based, compat handler, proto_ops override, dev_load, rtnl_lock, rcu_read_lock, CAP_NET_ADMIN, copyout, ...). You are also adding the checks into different places for native and compat mode, which makes them diverge more when we should be trying to make them more common. I think based on my recent changes, some other simplifications are possible, based on how the compat path already enumerates all the dev ioctls. Arnd