From patchwork Mon Dec 16 17:47:47 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Greg Kroah-Hartman X-Patchwork-Id: 181778 Delivered-To: patch@linaro.org Received: by 2002:a92:3001:0:0:0:0:0 with SMTP id x1csp4663250ile; Mon, 16 Dec 2019 10:14:53 -0800 (PST) X-Google-Smtp-Source: APXvYqyGqHjlnOjjv5SKNT7On1bE1itIe3MWRCTP8rw6wyjiymTkH236aLtKddvbD+kRGGaa+Xw4 X-Received: by 2002:aca:5117:: with SMTP id f23mr236694oib.50.1576520093691; Mon, 16 Dec 2019 10:14:53 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1576520093; cv=none; d=google.com; s=arc-20160816; b=EUR777mL0eQBAia+/VeHNjwNveiXEiiu4WAPmMfeSQu0YJtOz/ToXxj0gNOrgN3pIQ q+omYfylxkWWA8WQ5lqgjCA+ZbIa5D+XZwHPcVZxrGDsPyXzp5vN5D8xCr9C1dMnYDU8 vFbtnaN1uRNK6YnsYTAG59LekX6+fUE1LbCnNpJzo3qKC3+OtnQ35cAkt5KTbBOxBiYT KSbn+dhYsKAyuHQNPd8qRwNuBj87ySn9REC3gr8QwwZF4SqbS3koxp+MZhewjKX/es+X Hmba302opAQMwgKQQfHOHUFqHsqSRthEo7FuSGUWVRf2NGsG6/ryaQBT+DWYPydFWqWd PLVg== 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=EM7iVA8l/6MKz4M1aHFVzsjvDxxB8/BHd8i5nRxB/t4=; b=xCShfMRWmUHSHwA2tHfPE1kZsOVK76Gm/oCt5qOOVMHO4GgHqIC04qupF82ncz8Hym GBePDirHsJwwrDXLYQf1DKKnLskeva80MbNWRm9pMHMAA1I1qcG2F7b0ykQq5ev4nLA0 HDrIcZFmA2hKDz4eDeOPHadPhkMXmZhoV8jMi4xwpD1VPqzeOgeCjkv0tnRqNFjrtpq5 leZV+Tuogowr8J9H5jJC7A13Dg5w39zyIb2vZiJ1ot+tVciZEFx1IYH0Tf5UgY05Aica PiRfDzP99hPVS1KET8w9dQiLdApQb//6s+orLyssn7oszIPWuhFTb56toDpApx6vmdLo SB/w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=YNApgVVB; spf=pass (google.com: best guess record for domain of stable-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=stable-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 w11si10151684oig.45.2019.12.16.10.14.53; Mon, 16 Dec 2019 10:14:53 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of stable-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=YNApgVVB; spf=pass (google.com: best guess record for domain of stable-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=stable-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731286AbfLPSOw (ORCPT + 14 others); Mon, 16 Dec 2019 13:14:52 -0500 Received: from mail.kernel.org ([198.145.29.99]:34628 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731274AbfLPSOr (ORCPT ); Mon, 16 Dec 2019 13:14:47 -0500 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 00968206B7; Mon, 16 Dec 2019 18:14:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1576520086; bh=7yDn2V6zu56CJCV4PFrB+CcvjiJrjdv3u4o/Atw8PsM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=YNApgVVBC7mKVFKUf9rRMSfW+CFYeZqJKYiJ389noSEENbxzejYL8sml/NF/c+tyB fS25xpJi0VhNO+GNMUDQUtYbSkjWJ/q/dMqNJAv+gea0W82W0En4POtL6onRfdtn3Y M+YsTSMR0VoLe7x9VlNrfPeXKnm8vctzHHvFGvp8= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Arnd Bergmann Subject: [PATCH 5.4 011/177] compat_ioctl: add compat_ptr_ioctl() Date: Mon, 16 Dec 2019 18:47:47 +0100 Message-Id: <20191216174815.127036832@linuxfoundation.org> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20191216174811.158424118@linuxfoundation.org> References: <20191216174811.158424118@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Sender: stable-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: stable@vger.kernel.org From: Arnd Bergmann commit 2952db0fd51b0890f728df94ac563c21407f4f43 upstream. Many drivers have ioctl() handlers that are completely compatible between 32-bit and 64-bit architectures, except for the argument that is passed down from user space and may have to be passed through compat_ptr() in order to become a valid 64-bit pointer. Using ".compat_ptr = compat_ptr_ioctl" in file operations should let us simplify a lot of those drivers to avoid #ifdef checks, and convert additional drivers that don't have proper compat handling yet. On most architectures, the compat_ptr_ioctl() just passes all arguments to the corresponding ->ioctl handler. The exception is arch/s390, where compat_ptr() clears the top bit of a 32-bit pointer value, so user space pointers to the second 2GB alias the first 2GB, as is the case for native 32-bit s390 user space. The compat_ptr_ioctl() function must therefore be used only with ioctl functions that either ignore the argument or pass a pointer to a compatible data type. If any ioctl command handled by fops->unlocked_ioctl passes a plain integer instead of a pointer, or any of the passed data types is incompatible between 32-bit and 64-bit architectures, a proper handler is required instead of compat_ptr_ioctl. Signed-off-by: Arnd Bergmann --- v3: add a better description v2: use compat_ptr_ioctl instead of generic_compat_ioctl_ptrarg, as suggested by Al Viro Signed-off-by: Greg Kroah-Hartman --- fs/ioctl.c | 35 +++++++++++++++++++++++++++++++++++ include/linux/fs.h | 7 +++++++ 2 files changed, 42 insertions(+) --- a/fs/ioctl.c +++ b/fs/ioctl.c @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include @@ -719,3 +720,37 @@ SYSCALL_DEFINE3(ioctl, unsigned int, fd, { return ksys_ioctl(fd, cmd, arg); } + +#ifdef CONFIG_COMPAT +/** + * compat_ptr_ioctl - generic implementation of .compat_ioctl file operation + * + * This is not normally called as a function, but instead set in struct + * file_operations as + * + * .compat_ioctl = compat_ptr_ioctl, + * + * On most architectures, the compat_ptr_ioctl() just passes all arguments + * to the corresponding ->ioctl handler. The exception is arch/s390, where + * compat_ptr() clears the top bit of a 32-bit pointer value, so user space + * pointers to the second 2GB alias the first 2GB, as is the case for + * native 32-bit s390 user space. + * + * The compat_ptr_ioctl() function must therefore be used only with ioctl + * functions that either ignore the argument or pass a pointer to a + * compatible data type. + * + * If any ioctl command handled by fops->unlocked_ioctl passes a plain + * integer instead of a pointer, or any of the passed data types + * is incompatible between 32-bit and 64-bit architectures, a proper + * handler is required instead of compat_ptr_ioctl. + */ +long compat_ptr_ioctl(struct file *file, unsigned int cmd, unsigned long arg) +{ + if (!file->f_op->unlocked_ioctl) + return -ENOIOCTLCMD; + + return file->f_op->unlocked_ioctl(file, cmd, (unsigned long)compat_ptr(arg)); +} +EXPORT_SYMBOL(compat_ptr_ioctl); +#endif --- a/include/linux/fs.h +++ b/include/linux/fs.h @@ -1727,6 +1727,13 @@ int vfs_mkobj(struct dentry *, umode_t, extern long vfs_ioctl(struct file *file, unsigned int cmd, unsigned long arg); +#ifdef CONFIG_COMPAT +extern long compat_ptr_ioctl(struct file *file, unsigned int cmd, + unsigned long arg); +#else +#define compat_ptr_ioctl NULL +#endif + /* * VFS file helper functions. */