Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp2021652imm; Fri, 7 Sep 2018 09:36:32 -0700 (PDT) X-Google-Smtp-Source: ANB0Vdb0wo1F/3eyuChudq44MNfSv4RWRHflgREqFxSrf0YNr6lWixtf/d34g7naQJ7x/Ltc46pq X-Received: by 2002:a62:1f11:: with SMTP id f17-v6mr9451352pff.168.1536338192223; Fri, 07 Sep 2018 09:36:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1536338192; cv=none; d=google.com; s=arc-20160816; b=BIrOovyu3WzeSTb3mUywY5LggvJsgdSZX0rmG5ZNYRYUO/5W8CKPSxrQEfMBvWQWhS 8gxsvTUThhHjfl6yadvtD5sPXEjMOdErad1rUc54SSEbIZB1FBLPKiThivJFeqr1uZNs evIYCTN+vEwQj/LU36aCMxXEOHtlGtcArxnSuOLg+2eTvWNReWWaIT+QxhwbcnCIWedz Y7MAhLL5S9sybDA7ungAqIGLDnzF5j3s3WiqY6dUUONbhwCC9ioKqg8ZWl7O8WflMyaD U63X1GdNJsutoTdf3bth2YFfQ2Loy4K25WN55f+i5rfYTeyaGvYqyIQB/89S5lBJhU1M CEBg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date; bh=LXfSEQeLEWHL9544fZMf5kl+L3YDPkw9V9ym3mmGIgM=; b=eCZyN4Vqbs2sGIIIFMzGb67O3dCJ7y9LNel6PjvKYzQi+grz1cDcW8DsAL1OOeSJPR BTE7S/wsR/9lDr1tn7Z4GuYRqfSJSMkjsiDgOfWMhbjMkqHv3aDDYinpw/k5VxYi6eSq hLTQlZv7jYCfK6dM8zJks+xX6ZLik2BIzuqLPHoHCglMI9gWqTR9T5wKAzXVm6aGETuI 5qqv/XW9WmSTqs40xnyafO1YEXe0UqZqzJuS7IOFbE6fSaIkTLkrvW0R9fqM8BJdv8OD nte2bJhN6sYQaW5bQ1ukou+3BobjD0Pb0kR8BYf+eVUolrxwv+Xe3iQLB3if+FsHNx/G W/Lw== 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 x17-v6si8929713pgi.243.2018.09.07.09.36.16; Fri, 07 Sep 2018 09:36:32 -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 S1730081AbeIGUHc (ORCPT + 99 others); Fri, 7 Sep 2018 16:07:32 -0400 Received: from foss.arm.com ([217.140.101.70]:33542 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728248AbeIGUHc (ORCPT ); Fri, 7 Sep 2018 16:07:32 -0400 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.72.51.249]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id A666718A; Fri, 7 Sep 2018 08:26:07 -0700 (PDT) Received: from armageddon.cambridge.arm.com (armageddon.Emea.Arm.com [10.4.13.16]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 47D8D3F614; Fri, 7 Sep 2018 08:26:03 -0700 (PDT) Date: Fri, 7 Sep 2018 16:26:00 +0100 From: Catalin Marinas To: Linus Torvalds Cc: Andrey Konovalov , Mark Rutland , Kate Stewart , "open list:DOCUMENTATION" , Will Deacon , Kostya Serebryany , "open list:KERNEL SELFTEST FRAMEWORK" , cpandya@codeaurora.org, Shuah Khan , Ingo Molnar , linux-arch , Jacob.Bramley@arm.com, linux-arm-kernel , eugenis@google.com, Kees Cook , Ruben.Ayrapetyan@arm.com, Ramana Radhakrishnan , Al Viro , Dmitry Vyukov , linux-mm , Greg Kroah-Hartman , Linux Kernel Mailing List , Lee.Smith@arm.com, Andrew Morton , Robin Murphy , "Kirill A. Shutemov" Subject: Re: [PATCH v6 11/11] arm64: annotate user pointers casts detected by sparse Message-ID: <20180907152600.myidisza5o4kdmvf@armageddon.cambridge.arm.com> References: <5d54526e5ff2e5ad63d0dfdd9ab17cf359afa4f2.1535629099.git.andreyknvl@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: NeoMutt/20170113 (1.7.2) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Sep 06, 2018 at 02:16:19PM -0700, Linus Torvalds wrote: > On Thu, Sep 6, 2018 at 2:13 PM Linus Torvalds > wrote: > > > > So for example: > > > > > static inline compat_uptr_t ptr_to_compat(void __user *uptr) > > > { > > > - return (u32)(unsigned long)uptr; > > > + return (u32)(__force unsigned long)uptr; > > > } > > > > this actually looks correct. > > Side note: I do think that while the above is correct, the rest of the > patch shows that we might be better off simply not havign the warning > for address space changes at all for the "cast a pointer to an integer > type" case. > > When you cast to a non-pointer type, the address space issue simply > doesn't exist at all, so the warning makes less sense. That's actually a new (potential) issue introduced by these patches. The arm64 architecture has a feature called Top Byte Ignore (TBI, a.k.a. tagged pointers) where the top 8-bit of a 64-bit pointer can be set to anything and the hardware automatically ignores it when dereferencing. The arm64 user/kernel ABI currently mandates that any pointer passed from user space to the kernel must have the top byte 0. This patchset is proposing to relax the ABI so that user pointers with a non-zero top byte can be actually passed via kernel syscalls. It basically moves the responsibility to remove the pointer tag (where needed) from user to the kernel (and for some good reasons, user space can't always do it given the way hwasan is implemented in LLVM). The downside is that now a tagged user pointer may not represent just a virtual address but address|tag, so things like access_ok() or find_extended_vma() need to untag (clear the top byte of) the pointer before use. Note that copy_from_user() etc. can safely dereference a tagged user pointer as the tag is automatically ignored by the hardware. The arm64 maintainers asked for a more reliable approach to identifying existing and new cases where such explicit untagging is required and one of the proposals was a sparse option. Based on some observations, it seems that untagging is needed when a pointer is cast to a long and the pointer tag information can be dropped. With the sparse patch, there are lots of warnings where we actually can preserve the tag (e.g. compat user pointers should be ignored since the top 32-bit are always 0), so Andrey is trying to mask such warnings out so that we can detect new potential issues as the kernel evolves. So it's not about casting to another pointer; it's rather about no longer using the value as a user pointer but as an actual (untyped, untagged) virtual address. There may be better options to address this but I haven't seen any concrete proposal so far. Or we could simply consider that we've found all places where it matters and not bother with any static analysis tools (but for the time being it's still worth investigating whether we can do better than this). > It's really just he "pointer to one address space" being cast to > "pointer to another address space" that should really warn, and that > might need that "__force" thing. I think sparse already warns if changing the address space of a pointer. -- Catalin