linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v10 00/12] arm64: untag user pointers passed to the kernel
@ 2019-02-22 12:53 Andrey Konovalov
  2019-02-22 12:53 ` [PATCH v10 01/12] uaccess: add untagged_addr definition for other arches Andrey Konovalov
                   ` (13 more replies)
  0 siblings, 14 replies; 30+ messages in thread
From: Andrey Konovalov @ 2019-02-22 12:53 UTC (permalink / raw)
  To: Catalin Marinas, Will Deacon, Mark Rutland, Robin Murphy,
	Kees Cook, Kate Stewart, Greg Kroah-Hartman, Andrew Morton,
	Ingo Molnar, Kirill A . Shutemov, Shuah Khan, Vincenzo Frascino,
	linux-arm-kernel, linux-doc, linux-mm, linux-arch,
	linux-kselftest, linux-kernel
  Cc: Dmitry Vyukov, Kostya Serebryany, Evgeniy Stepanov, Lee Smith,
	Ramana Radhakrishnan, Jacob Bramley, Ruben Ayrapetyan,
	Chintan Pandya, Luc Van Oostenryck, Dave Martin, Kevin Brodsky,
	Szabolcs Nagy, Andrey Konovalov

This patchset is meant to be merged together with "arm64 relaxed ABI" [1].

arm64 has a feature called Top Byte Ignore, which allows to embed pointer
tags into the top byte of each pointer. Userspace programs (such as
HWASan, a memory debugging tool [2]) might use this feature and pass
tagged user pointers to the kernel through syscalls or other interfaces.

Right now the kernel is already able to handle user faults with tagged
pointers, due to these patches:

1. 81cddd65 ("arm64: traps: fix userspace cache maintenance emulation on a
             tagged pointer")
2. 7dcd9dd8 ("arm64: hw_breakpoint: fix watchpoint matching for tagged
	      pointers")
3. 276e9327 ("arm64: entry: improve data abort handling of tagged
	      pointers")

This patchset extends tagged pointer support to syscall arguments.

For non-memory syscalls this is done by untaging user pointers when the
kernel performs pointer checking to find out whether the pointer comes
from userspace (most notably in access_ok). The untagging is done only
when the pointer is being checked, the tag is preserved as the pointer
makes its way through the kernel.

Since memory syscalls (mmap, mprotect, etc.) don't do memory accesses but
rather deal with memory ranges, untagged pointers are better suited to
describe memory ranges internally. Thus for memory syscalls we untag
pointers completely when they enter the kernel.

One of the alternative approaches to untagging that was considered is to
completely strip the pointer tag as the pointer enters the kernel with
some kind of a syscall wrapper, but that won't work with the countless
number of different ioctl calls. With this approach we would need a custom
wrapper for each ioctl variation, which doesn't seem practical.

The following testing approaches has been taken to find potential issues
with user pointer untagging:

1. Static testing (with sparse [3] and separately with a custom static
   analyzer based on Clang) to track casts of __user pointers to integer
   types to find places where untagging needs to be done.

2. Static testing with grep to find parts of the kernel that call
   find_vma() (and other similar functions) or directly compare against
   vm_start/vm_end fields of vma.

3. Static testing with grep to find parts of the kernel that compare
   user pointers with TASK_SIZE or other similar consts and macros.

4. Dynamic testing: adding BUG_ON(has_tag(addr)) to find_vma() and running
   a modified syzkaller version that passes tagged pointers to the kernel.

Based on the results of the testing the requried patches have been added
to the patchset.

This patchset has been merged into the Pixel 2 kernel tree and is now
being used to enable testing of Pixel 2 phones with HWASan.

This patchset is a prerequisite for ARM's memory tagging hardware feature
support [4].

Thanks!

[1] https://lkml.org/lkml/2018/12/10/402

[2] http://clang.llvm.org/docs/HardwareAssistedAddressSanitizerDesign.html

[3] https://github.com/lucvoo/sparse-dev/commit/5f960cb10f56ec2017c128ef9d16060e0145f292

[4] https://community.arm.com/processors/b/blog/posts/arm-a-profile-architecture-2018-developments-armv85a

Changes in v10:
- Added "mm, arm64: untag user pointers passed to memory syscalls" back.
- New patch "fs, arm64: untag user pointers in fs/userfaultfd.c".
- New patch "net, arm64: untag user pointers in tcp_zerocopy_receive".
- New patch "kernel, arm64: untag user pointers in prctl_set_mm*".
- New patch "tracing, arm64: untag user pointers in seq_print_user_ip".

Changes in v9:
- Rebased onto 4.20-rc6.
- Used u64 instead of __u64 in type casts in the untagged_addr macro for
  arm64.
- Added braces around (addr) in the untagged_addr macro for other arches.

Changes in v8:
- Rebased onto 65102238 (4.20-rc1).
- Added a note to the cover letter on why syscall wrappers/shims that untag
  user pointers won't work.
- Added a note to the cover letter that this patchset has been merged into
  the Pixel 2 kernel tree.
- Documentation fixes, in particular added a list of syscalls that don't
  support tagged user pointers.

Changes in v7:
- Rebased onto 17b57b18 (4.19-rc6).
- Dropped the "arm64: untag user address in __do_user_fault" patch, since
  the existing patches already handle user faults properly.
- Dropped the "usb, arm64: untag user addresses in devio" patch, since the
  passed pointer must come from a vma and therefore be untagged.
- Dropped the "arm64: annotate user pointers casts detected by sparse"
  patch (see the discussion to the replies of the v6 of this patchset).
- Added more context to the cover letter.
- Updated Documentation/arm64/tagged-pointers.txt.

Changes in v6:
- Added annotations for user pointer casts found by sparse.
- Rebased onto 050cdc6c (4.19-rc1+).

Changes in v5:
- Added 3 new patches that add untagging to places found with static
  analysis.
- Rebased onto 44c929e1 (4.18-rc8).

Changes in v4:
- Added a selftest for checking that passing tagged pointers to the
  kernel succeeds.
- Rebased onto 81e97f013 (4.18-rc1+).

Changes in v3:
- Rebased onto e5c51f30 (4.17-rc6+).
- Added linux-arch@ to the list of recipients.

Changes in v2:
- Rebased onto 2d618bdf (4.17-rc3+).
- Removed excessive untagging in gup.c.
- Removed untagging pointers returned from __uaccess_mask_ptr.

Changes in v1:
- Rebased onto 4.17-rc1.

Changes in RFC v2:
- Added "#ifndef untagged_addr..." fallback in linux/uaccess.h instead of
  defining it for each arch individually.
- Updated Documentation/arm64/tagged-pointers.txt.
- Dropped "mm, arm64: untag user addresses in memory syscalls".
- Rebased onto 3eb2ce82 (4.16-rc7).

Reviewed-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
Signed-off-by: Andrey Konovalov <andreyknvl@google.com>

Andrey Konovalov (12):
  uaccess: add untagged_addr definition for other arches
  arm64: untag user pointers in access_ok and __uaccess_mask_ptr
  lib, arm64: untag user pointers in strn*_user
  mm, arm64: untag user pointers passed to memory syscalls
  mm, arm64: untag user pointers in mm/gup.c
  fs, arm64: untag user pointers in copy_mount_options
  fs, arm64: untag user pointers in fs/userfaultfd.c
  net, arm64: untag user pointers in tcp_zerocopy_receive
  kernel, arm64: untag user pointers in prctl_set_mm*
  tracing, arm64: untag user pointers in seq_print_user_ip
  arm64: update Documentation/arm64/tagged-pointers.txt
  selftests, arm64: add a selftest for passing tagged pointers to kernel

 Documentation/arm64/tagged-pointers.txt       | 25 +++++++++++--------
 arch/arm64/include/asm/uaccess.h              | 10 +++++---
 fs/namespace.c                                |  2 +-
 fs/userfaultfd.c                              |  5 ++++
 include/linux/memory.h                        |  4 +++
 ipc/shm.c                                     |  2 ++
 kernel/sys.c                                  | 14 +++++++++++
 kernel/trace/trace_output.c                   |  2 +-
 lib/strncpy_from_user.c                       |  2 ++
 lib/strnlen_user.c                            |  2 ++
 mm/gup.c                                      |  4 +++
 mm/madvise.c                                  |  2 ++
 mm/mempolicy.c                                |  5 ++++
 mm/migrate.c                                  |  1 +
 mm/mincore.c                                  |  2 ++
 mm/mlock.c                                    |  5 ++++
 mm/mmap.c                                     |  7 ++++++
 mm/mprotect.c                                 |  2 ++
 mm/mremap.c                                   |  2 ++
 mm/msync.c                                    |  2 ++
 net/ipv4/tcp.c                                |  2 ++
 tools/testing/selftests/arm64/.gitignore      |  1 +
 tools/testing/selftests/arm64/Makefile        | 11 ++++++++
 .../testing/selftests/arm64/run_tags_test.sh  | 12 +++++++++
 tools/testing/selftests/arm64/tags_test.c     | 19 ++++++++++++++
 25 files changed, 129 insertions(+), 16 deletions(-)
 create mode 100644 tools/testing/selftests/arm64/.gitignore
 create mode 100644 tools/testing/selftests/arm64/Makefile
 create mode 100755 tools/testing/selftests/arm64/run_tags_test.sh
 create mode 100644 tools/testing/selftests/arm64/tags_test.c

-- 
2.21.0.rc0.258.g878e2cd30e-goog


^ permalink raw reply	[flat|nested] 30+ messages in thread

* [PATCH v10 01/12] uaccess: add untagged_addr definition for other arches
  2019-02-22 12:53 [PATCH v10 00/12] arm64: untag user pointers passed to the kernel Andrey Konovalov
@ 2019-02-22 12:53 ` Andrey Konovalov
  2019-02-22 12:53 ` [PATCH v10 02/12] arm64: untag user pointers in access_ok and __uaccess_mask_ptr Andrey Konovalov
                   ` (12 subsequent siblings)
  13 siblings, 0 replies; 30+ messages in thread
From: Andrey Konovalov @ 2019-02-22 12:53 UTC (permalink / raw)
  To: Catalin Marinas, Will Deacon, Mark Rutland, Robin Murphy,
	Kees Cook, Kate Stewart, Greg Kroah-Hartman, Andrew Morton,
	Ingo Molnar, Kirill A . Shutemov, Shuah Khan, Vincenzo Frascino,
	linux-arm-kernel, linux-doc, linux-mm, linux-arch,
	linux-kselftest, linux-kernel
  Cc: Dmitry Vyukov, Kostya Serebryany, Evgeniy Stepanov, Lee Smith,
	Ramana Radhakrishnan, Jacob Bramley, Ruben Ayrapetyan,
	Chintan Pandya, Luc Van Oostenryck, Dave Martin, Kevin Brodsky,
	Szabolcs Nagy, Andrey Konovalov

To allow arm64 syscalls to accept tagged pointers from userspace, we must
untag them when they are passed to the kernel. Since untagging is done in
generic parts of the kernel, the untagged_addr macro needs to be defined
for all architectures.

Define it as a noop for architectures other than arm64.

Acked-by: Catalin Marinas <catalin.marinas@arm.com>
Signed-off-by: Andrey Konovalov <andreyknvl@google.com>
---
 include/linux/memory.h | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/include/linux/memory.h b/include/linux/memory.h
index a6ddefc60517..fc383bc39ab8 100644
--- a/include/linux/memory.h
+++ b/include/linux/memory.h
@@ -21,6 +21,10 @@
 #include <linux/mutex.h>
 #include <linux/notifier.h>
 
+#ifndef untagged_addr
+#define untagged_addr(addr) (addr)
+#endif
+
 #define MIN_MEMORY_BLOCK_SIZE     (1UL << SECTION_SIZE_BITS)
 
 struct memory_block {
-- 
2.21.0.rc0.258.g878e2cd30e-goog


^ permalink raw reply related	[flat|nested] 30+ messages in thread

* [PATCH v10 02/12] arm64: untag user pointers in access_ok and __uaccess_mask_ptr
  2019-02-22 12:53 [PATCH v10 00/12] arm64: untag user pointers passed to the kernel Andrey Konovalov
  2019-02-22 12:53 ` [PATCH v10 01/12] uaccess: add untagged_addr definition for other arches Andrey Konovalov
@ 2019-02-22 12:53 ` Andrey Konovalov
  2019-02-22 12:53 ` [PATCH v10 03/12] lib, arm64: untag user pointers in strn*_user Andrey Konovalov
                   ` (11 subsequent siblings)
  13 siblings, 0 replies; 30+ messages in thread
From: Andrey Konovalov @ 2019-02-22 12:53 UTC (permalink / raw)
  To: Catalin Marinas, Will Deacon, Mark Rutland, Robin Murphy,
	Kees Cook, Kate Stewart, Greg Kroah-Hartman, Andrew Morton,
	Ingo Molnar, Kirill A . Shutemov, Shuah Khan, Vincenzo Frascino,
	linux-arm-kernel, linux-doc, linux-mm, linux-arch,
	linux-kselftest, linux-kernel
  Cc: Dmitry Vyukov, Kostya Serebryany, Evgeniy Stepanov, Lee Smith,
	Ramana Radhakrishnan, Jacob Bramley, Ruben Ayrapetyan,
	Chintan Pandya, Luc Van Oostenryck, Dave Martin, Kevin Brodsky,
	Szabolcs Nagy, Andrey Konovalov

copy_from_user (and a few other similar functions) are used to copy data
from user memory into the kernel memory or vice versa. Since a user can
provided a tagged pointer to one of the syscalls that use copy_from_user,
we need to correctly handle such pointers.

Do this by untagging user pointers in access_ok and in __uaccess_mask_ptr,
before performing access validity checks.

Note, that this patch only temporarily untags the pointers to perform the
checks, but then passes them as is into the kernel internals.

Reviewed-by: Catalin Marinas <catalin.marinas@arm.com>
Signed-off-by: Andrey Konovalov <andreyknvl@google.com>
---
 arch/arm64/include/asm/uaccess.h | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/arch/arm64/include/asm/uaccess.h b/arch/arm64/include/asm/uaccess.h
index 547d7a0c9d05..9b9291abda88 100644
--- a/arch/arm64/include/asm/uaccess.h
+++ b/arch/arm64/include/asm/uaccess.h
@@ -95,7 +95,7 @@ static inline unsigned long __range_ok(const void __user *addr, unsigned long si
 	return ret;
 }
 
-#define access_ok(addr, size)	__range_ok(addr, size)
+#define access_ok(addr, size)	__range_ok(untagged_addr(addr), size)
 #define user_addr_max			get_fs
 
 #define _ASM_EXTABLE(from, to)						\
@@ -227,7 +227,8 @@ static inline void uaccess_enable_not_uao(void)
 
 /*
  * Sanitise a uaccess pointer such that it becomes NULL if above the
- * current addr_limit.
+ * current addr_limit. In case the pointer is tagged (has the top byte set),
+ * untag the pointer before checking.
  */
 #define uaccess_mask_ptr(ptr) (__typeof__(ptr))__uaccess_mask_ptr(ptr)
 static inline void __user *__uaccess_mask_ptr(const void __user *ptr)
@@ -235,10 +236,11 @@ static inline void __user *__uaccess_mask_ptr(const void __user *ptr)
 	void __user *safe_ptr;
 
 	asm volatile(
-	"	bics	xzr, %1, %2\n"
+	"	bics	xzr, %3, %2\n"
 	"	csel	%0, %1, xzr, eq\n"
 	: "=&r" (safe_ptr)
-	: "r" (ptr), "r" (current_thread_info()->addr_limit)
+	: "r" (ptr), "r" (current_thread_info()->addr_limit),
+	  "r" (untagged_addr(ptr))
 	: "cc");
 
 	csdb();
-- 
2.21.0.rc0.258.g878e2cd30e-goog


^ permalink raw reply related	[flat|nested] 30+ messages in thread

* [PATCH v10 03/12] lib, arm64: untag user pointers in strn*_user
  2019-02-22 12:53 [PATCH v10 00/12] arm64: untag user pointers passed to the kernel Andrey Konovalov
  2019-02-22 12:53 ` [PATCH v10 01/12] uaccess: add untagged_addr definition for other arches Andrey Konovalov
  2019-02-22 12:53 ` [PATCH v10 02/12] arm64: untag user pointers in access_ok and __uaccess_mask_ptr Andrey Konovalov
@ 2019-02-22 12:53 ` Andrey Konovalov
  2019-02-22 12:53 ` [PATCH v10 04/12] mm, arm64: untag user pointers passed to memory syscalls Andrey Konovalov
                   ` (10 subsequent siblings)
  13 siblings, 0 replies; 30+ messages in thread
From: Andrey Konovalov @ 2019-02-22 12:53 UTC (permalink / raw)
  To: Catalin Marinas, Will Deacon, Mark Rutland, Robin Murphy,
	Kees Cook, Kate Stewart, Greg Kroah-Hartman, Andrew Morton,
	Ingo Molnar, Kirill A . Shutemov, Shuah Khan, Vincenzo Frascino,
	linux-arm-kernel, linux-doc, linux-mm, linux-arch,
	linux-kselftest, linux-kernel
  Cc: Dmitry Vyukov, Kostya Serebryany, Evgeniy Stepanov, Lee Smith,
	Ramana Radhakrishnan, Jacob Bramley, Ruben Ayrapetyan,
	Chintan Pandya, Luc Van Oostenryck, Dave Martin, Kevin Brodsky,
	Szabolcs Nagy, Andrey Konovalov

strncpy_from_user and strnlen_user accept user addresses as arguments, and
do not go through the same path as copy_from_user and others, so here we
need to handle the case of tagged user addresses separately.

Untag user pointers passed to these functions.

Signed-off-by: Andrey Konovalov <andreyknvl@google.com>
---
 lib/strncpy_from_user.c | 2 ++
 lib/strnlen_user.c      | 2 ++
 2 files changed, 4 insertions(+)

diff --git a/lib/strncpy_from_user.c b/lib/strncpy_from_user.c
index 58eacd41526c..c6adfad39016 100644
--- a/lib/strncpy_from_user.c
+++ b/lib/strncpy_from_user.c
@@ -106,6 +106,8 @@ long strncpy_from_user(char *dst, const char __user *src, long count)
 	if (unlikely(count <= 0))
 		return 0;
 
+	src = untagged_addr(src);
+
 	max_addr = user_addr_max();
 	src_addr = (unsigned long)src;
 	if (likely(src_addr < max_addr)) {
diff --git a/lib/strnlen_user.c b/lib/strnlen_user.c
index 1c1a1b0e38a5..26a6a2a1a963 100644
--- a/lib/strnlen_user.c
+++ b/lib/strnlen_user.c
@@ -108,6 +108,8 @@ long strnlen_user(const char __user *str, long count)
 	if (unlikely(count <= 0))
 		return 0;
 
+	str = untagged_addr(str);
+
 	max_addr = user_addr_max();
 	src_addr = (unsigned long)str;
 	if (likely(src_addr < max_addr)) {
-- 
2.21.0.rc0.258.g878e2cd30e-goog


^ permalink raw reply related	[flat|nested] 30+ messages in thread

* [PATCH v10 04/12] mm, arm64: untag user pointers passed to memory syscalls
  2019-02-22 12:53 [PATCH v10 00/12] arm64: untag user pointers passed to the kernel Andrey Konovalov
                   ` (2 preceding siblings ...)
  2019-02-22 12:53 ` [PATCH v10 03/12] lib, arm64: untag user pointers in strn*_user Andrey Konovalov
@ 2019-02-22 12:53 ` Andrey Konovalov
  2019-02-22 23:07   ` Dave Hansen
  2019-02-22 12:53 ` [PATCH v10 05/12] mm, arm64: untag user pointers in mm/gup.c Andrey Konovalov
                   ` (9 subsequent siblings)
  13 siblings, 1 reply; 30+ messages in thread
From: Andrey Konovalov @ 2019-02-22 12:53 UTC (permalink / raw)
  To: Catalin Marinas, Will Deacon, Mark Rutland, Robin Murphy,
	Kees Cook, Kate Stewart, Greg Kroah-Hartman, Andrew Morton,
	Ingo Molnar, Kirill A . Shutemov, Shuah Khan, Vincenzo Frascino,
	linux-arm-kernel, linux-doc, linux-mm, linux-arch,
	linux-kselftest, linux-kernel
  Cc: Dmitry Vyukov, Kostya Serebryany, Evgeniy Stepanov, Lee Smith,
	Ramana Radhakrishnan, Jacob Bramley, Ruben Ayrapetyan,
	Chintan Pandya, Luc Van Oostenryck, Dave Martin, Kevin Brodsky,
	Szabolcs Nagy, Andrey Konovalov

This commit allows tagged pointers to be passed to the following memory
syscalls: madvise, mbind, get_mempolicy, mincore, mlock, mlock2, brk,
mmap_pgoff, old_mmap, munmap, remap_file_pages, mprotect, pkey_mprotect,
mremap, msync and shmdt.

Signed-off-by: Andrey Konovalov <andreyknvl@google.com>
---
 ipc/shm.c      | 2 ++
 mm/madvise.c   | 2 ++
 mm/mempolicy.c | 5 +++++
 mm/migrate.c   | 1 +
 mm/mincore.c   | 2 ++
 mm/mlock.c     | 5 +++++
 mm/mmap.c      | 7 +++++++
 mm/mprotect.c  | 2 ++
 mm/mremap.c    | 2 ++
 mm/msync.c     | 2 ++
 10 files changed, 30 insertions(+)

diff --git a/ipc/shm.c b/ipc/shm.c
index 0842411cb0e9..f0fd9591d28f 100644
--- a/ipc/shm.c
+++ b/ipc/shm.c
@@ -1567,6 +1567,7 @@ SYSCALL_DEFINE3(shmat, int, shmid, char __user *, shmaddr, int, shmflg)
 	unsigned long ret;
 	long err;
 
+	shmaddr = untagged_addr(shmaddr);
 	err = do_shmat(shmid, shmaddr, shmflg, &ret, SHMLBA);
 	if (err)
 		return err;
@@ -1706,6 +1707,7 @@ long ksys_shmdt(char __user *shmaddr)
 
 SYSCALL_DEFINE1(shmdt, char __user *, shmaddr)
 {
+	shmaddr = untagged_addr(shmaddr);
 	return ksys_shmdt(shmaddr);
 }
 
diff --git a/mm/madvise.c b/mm/madvise.c
index 21a7881a2db4..64e6d34a7f9b 100644
--- a/mm/madvise.c
+++ b/mm/madvise.c
@@ -809,6 +809,8 @@ SYSCALL_DEFINE3(madvise, unsigned long, start, size_t, len_in, int, behavior)
 	size_t len;
 	struct blk_plug plug;
 
+	start = untagged_addr(start);
+
 	if (!madvise_behavior_valid(behavior))
 		return error;
 
diff --git a/mm/mempolicy.c b/mm/mempolicy.c
index ee2bce59d2bf..0b5d5f794f4e 100644
--- a/mm/mempolicy.c
+++ b/mm/mempolicy.c
@@ -1334,6 +1334,7 @@ static long kernel_mbind(unsigned long start, unsigned long len,
 	int err;
 	unsigned short mode_flags;
 
+	start = untagged_addr(start);
 	mode_flags = mode & MPOL_MODE_FLAGS;
 	mode &= ~MPOL_MODE_FLAGS;
 	if (mode >= MPOL_MAX)
@@ -1491,6 +1492,8 @@ static int kernel_get_mempolicy(int __user *policy,
 	int uninitialized_var(pval);
 	nodemask_t nodes;
 
+	addr = untagged_addr(addr);
+
 	if (nmask != NULL && maxnode < nr_node_ids)
 		return -EINVAL;
 
@@ -1576,6 +1579,8 @@ COMPAT_SYSCALL_DEFINE6(mbind, compat_ulong_t, start, compat_ulong_t, len,
 	unsigned long nr_bits, alloc_size;
 	nodemask_t bm;
 
+	start = untagged_addr(start);
+
 	nr_bits = min_t(unsigned long, maxnode-1, MAX_NUMNODES);
 	alloc_size = ALIGN(nr_bits, BITS_PER_LONG) / 8;
 
diff --git a/mm/migrate.c b/mm/migrate.c
index d4fd680be3b0..b9f414e66af1 100644
--- a/mm/migrate.c
+++ b/mm/migrate.c
@@ -1601,6 +1601,7 @@ static int do_pages_move(struct mm_struct *mm, nodemask_t task_nodes,
 		if (get_user(node, nodes + i))
 			goto out_flush;
 		addr = (unsigned long)p;
+		addr = untagged_addr(addr);
 
 		err = -ENODEV;
 		if (node < 0 || node >= MAX_NUMNODES)
diff --git a/mm/mincore.c b/mm/mincore.c
index 218099b5ed31..c4a3f4484b6b 100644
--- a/mm/mincore.c
+++ b/mm/mincore.c
@@ -228,6 +228,8 @@ SYSCALL_DEFINE3(mincore, unsigned long, start, size_t, len,
 	unsigned long pages;
 	unsigned char *tmp;
 
+	start = untagged_addr(start);
+
 	/* Check the start address: needs to be page-aligned.. */
 	if (start & ~PAGE_MASK)
 		return -EINVAL;
diff --git a/mm/mlock.c b/mm/mlock.c
index 41cc47e28ad6..8fa29e7c0e73 100644
--- a/mm/mlock.c
+++ b/mm/mlock.c
@@ -715,6 +715,7 @@ static __must_check int do_mlock(unsigned long start, size_t len, vm_flags_t fla
 
 SYSCALL_DEFINE2(mlock, unsigned long, start, size_t, len)
 {
+	start = untagged_addr(start);
 	return do_mlock(start, len, VM_LOCKED);
 }
 
@@ -722,6 +723,8 @@ SYSCALL_DEFINE3(mlock2, unsigned long, start, size_t, len, int, flags)
 {
 	vm_flags_t vm_flags = VM_LOCKED;
 
+	start = untagged_addr(start);
+
 	if (flags & ~MLOCK_ONFAULT)
 		return -EINVAL;
 
@@ -735,6 +738,8 @@ SYSCALL_DEFINE2(munlock, unsigned long, start, size_t, len)
 {
 	int ret;
 
+	start = untagged_addr(start);
+
 	len = PAGE_ALIGN(len + (offset_in_page(start)));
 	start &= PAGE_MASK;
 
diff --git a/mm/mmap.c b/mm/mmap.c
index f901065c4c64..fc8e908a97ba 100644
--- a/mm/mmap.c
+++ b/mm/mmap.c
@@ -199,6 +199,8 @@ SYSCALL_DEFINE1(brk, unsigned long, brk)
 	bool downgraded = false;
 	LIST_HEAD(uf);
 
+	brk = untagged_addr(brk);
+
 	if (down_write_killable(&mm->mmap_sem))
 		return -EINTR;
 
@@ -1571,6 +1573,8 @@ unsigned long ksys_mmap_pgoff(unsigned long addr, unsigned long len,
 	struct file *file = NULL;
 	unsigned long retval;
 
+	addr = untagged_addr(addr);
+
 	if (!(flags & MAP_ANONYMOUS)) {
 		audit_mmap_fd(fd, flags);
 		file = fget(fd);
@@ -2869,6 +2873,7 @@ EXPORT_SYMBOL(vm_munmap);
 
 SYSCALL_DEFINE2(munmap, unsigned long, addr, size_t, len)
 {
+	addr = untagged_addr(addr);
 	profile_munmap(addr);
 	return __vm_munmap(addr, len, true);
 }
@@ -2887,6 +2892,8 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
 	unsigned long ret = -EINVAL;
 	struct file *file;
 
+	start = untagged_addr(start);
+
 	pr_warn_once("%s (%d) uses deprecated remap_file_pages() syscall. See Documentation/vm/remap_file_pages.rst.\n",
 		     current->comm, current->pid);
 
diff --git a/mm/mprotect.c b/mm/mprotect.c
index 36cb358db170..9d79594dabee 100644
--- a/mm/mprotect.c
+++ b/mm/mprotect.c
@@ -578,6 +578,7 @@ static int do_mprotect_pkey(unsigned long start, size_t len,
 SYSCALL_DEFINE3(mprotect, unsigned long, start, size_t, len,
 		unsigned long, prot)
 {
+	start = untagged_addr(start);
 	return do_mprotect_pkey(start, len, prot, -1);
 }
 
@@ -586,6 +587,7 @@ SYSCALL_DEFINE3(mprotect, unsigned long, start, size_t, len,
 SYSCALL_DEFINE4(pkey_mprotect, unsigned long, start, size_t, len,
 		unsigned long, prot, int, pkey)
 {
+	start = untagged_addr(start);
 	return do_mprotect_pkey(start, len, prot, pkey);
 }
 
diff --git a/mm/mremap.c b/mm/mremap.c
index 3320616ed93f..cd0e79c6ce63 100644
--- a/mm/mremap.c
+++ b/mm/mremap.c
@@ -588,6 +588,8 @@ SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned long, old_len,
 	LIST_HEAD(uf_unmap_early);
 	LIST_HEAD(uf_unmap);
 
+	addr = untagged_addr(addr);
+
 	if (flags & ~(MREMAP_FIXED | MREMAP_MAYMOVE))
 		return ret;
 
diff --git a/mm/msync.c b/mm/msync.c
index ef30a429623a..c3bd3e75f687 100644
--- a/mm/msync.c
+++ b/mm/msync.c
@@ -37,6 +37,8 @@ SYSCALL_DEFINE3(msync, unsigned long, start, size_t, len, int, flags)
 	int unmapped_error = 0;
 	int error = -EINVAL;
 
+	start = untagged_addr(start);
+
 	if (flags & ~(MS_ASYNC | MS_INVALIDATE | MS_SYNC))
 		goto out;
 	if (offset_in_page(start))
-- 
2.21.0.rc0.258.g878e2cd30e-goog


^ permalink raw reply related	[flat|nested] 30+ messages in thread

* [PATCH v10 05/12] mm, arm64: untag user pointers in mm/gup.c
  2019-02-22 12:53 [PATCH v10 00/12] arm64: untag user pointers passed to the kernel Andrey Konovalov
                   ` (3 preceding siblings ...)
  2019-02-22 12:53 ` [PATCH v10 04/12] mm, arm64: untag user pointers passed to memory syscalls Andrey Konovalov
@ 2019-02-22 12:53 ` Andrey Konovalov
  2019-02-22 12:53 ` [PATCH v10 06/12] fs, arm64: untag user pointers in copy_mount_options Andrey Konovalov
                   ` (8 subsequent siblings)
  13 siblings, 0 replies; 30+ messages in thread
From: Andrey Konovalov @ 2019-02-22 12:53 UTC (permalink / raw)
  To: Catalin Marinas, Will Deacon, Mark Rutland, Robin Murphy,
	Kees Cook, Kate Stewart, Greg Kroah-Hartman, Andrew Morton,
	Ingo Molnar, Kirill A . Shutemov, Shuah Khan, Vincenzo Frascino,
	linux-arm-kernel, linux-doc, linux-mm, linux-arch,
	linux-kselftest, linux-kernel
  Cc: Dmitry Vyukov, Kostya Serebryany, Evgeniy Stepanov, Lee Smith,
	Ramana Radhakrishnan, Jacob Bramley, Ruben Ayrapetyan,
	Chintan Pandya, Luc Van Oostenryck, Dave Martin, Kevin Brodsky,
	Szabolcs Nagy, Andrey Konovalov

mm/gup.c provides a kernel interface that accepts user addresses and
manipulates user pages directly (for example get_user_pages, that is used
by the futex syscall). Since a user can provided tagged addresses, we need
to handle such case.

Add untagging to gup.c functions that use user addresses for vma lookup.

Signed-off-by: Andrey Konovalov <andreyknvl@google.com>
---
 mm/gup.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/mm/gup.c b/mm/gup.c
index 75029649baca..b6eda1608bea 100644
--- a/mm/gup.c
+++ b/mm/gup.c
@@ -683,6 +683,8 @@ static long __get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
 	if (!nr_pages)
 		return 0;
 
+	start = untagged_addr(start);
+
 	VM_BUG_ON(!!pages != !!(gup_flags & FOLL_GET));
 
 	/*
@@ -845,6 +847,8 @@ int fixup_user_fault(struct task_struct *tsk, struct mm_struct *mm,
 	struct vm_area_struct *vma;
 	vm_fault_t ret, major = 0;
 
+	address = untagged_addr(address);
+
 	if (unlocked)
 		fault_flags |= FAULT_FLAG_ALLOW_RETRY;
 
-- 
2.21.0.rc0.258.g878e2cd30e-goog


^ permalink raw reply related	[flat|nested] 30+ messages in thread

* [PATCH v10 06/12] fs, arm64: untag user pointers in copy_mount_options
  2019-02-22 12:53 [PATCH v10 00/12] arm64: untag user pointers passed to the kernel Andrey Konovalov
                   ` (4 preceding siblings ...)
  2019-02-22 12:53 ` [PATCH v10 05/12] mm, arm64: untag user pointers in mm/gup.c Andrey Konovalov
@ 2019-02-22 12:53 ` Andrey Konovalov
  2019-02-22 23:03   ` Dave Hansen
  2019-02-22 12:53 ` [PATCH v10 07/12] fs, arm64: untag user pointers in fs/userfaultfd.c Andrey Konovalov
                   ` (7 subsequent siblings)
  13 siblings, 1 reply; 30+ messages in thread
From: Andrey Konovalov @ 2019-02-22 12:53 UTC (permalink / raw)
  To: Catalin Marinas, Will Deacon, Mark Rutland, Robin Murphy,
	Kees Cook, Kate Stewart, Greg Kroah-Hartman, Andrew Morton,
	Ingo Molnar, Kirill A . Shutemov, Shuah Khan, Vincenzo Frascino,
	linux-arm-kernel, linux-doc, linux-mm, linux-arch,
	linux-kselftest, linux-kernel
  Cc: Dmitry Vyukov, Kostya Serebryany, Evgeniy Stepanov, Lee Smith,
	Ramana Radhakrishnan, Jacob Bramley, Ruben Ayrapetyan,
	Chintan Pandya, Luc Van Oostenryck, Dave Martin, Kevin Brodsky,
	Szabolcs Nagy, Andrey Konovalov

In copy_mount_options a user address is being subtracted from TASK_SIZE.
If the address is lower than TASK_SIZE, the size is calculated to not
allow the exact_copy_from_user() call to cross TASK_SIZE boundary.
However if the address is tagged, then the size will be calculated
incorrectly.

Untag the address before subtracting.

Signed-off-by: Andrey Konovalov <andreyknvl@google.com>
---
 fs/namespace.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/fs/namespace.c b/fs/namespace.c
index a677b59efd74..d4b7adef9204 100644
--- a/fs/namespace.c
+++ b/fs/namespace.c
@@ -2730,7 +2730,7 @@ void *copy_mount_options(const void __user * data)
 	 * the remainder of the page.
 	 */
 	/* copy_from_user cannot cross TASK_SIZE ! */
-	size = TASK_SIZE - (unsigned long)data;
+	size = TASK_SIZE - (unsigned long)untagged_addr(data);
 	if (size > PAGE_SIZE)
 		size = PAGE_SIZE;
 
-- 
2.21.0.rc0.258.g878e2cd30e-goog


^ permalink raw reply related	[flat|nested] 30+ messages in thread

* [PATCH v10 07/12] fs, arm64: untag user pointers in fs/userfaultfd.c
  2019-02-22 12:53 [PATCH v10 00/12] arm64: untag user pointers passed to the kernel Andrey Konovalov
                   ` (5 preceding siblings ...)
  2019-02-22 12:53 ` [PATCH v10 06/12] fs, arm64: untag user pointers in copy_mount_options Andrey Konovalov
@ 2019-02-22 12:53 ` Andrey Konovalov
  2019-02-22 23:05   ` Dave Hansen
  2019-02-22 12:53 ` [PATCH v10 08/12] net, arm64: untag user pointers in tcp_zerocopy_receive Andrey Konovalov
                   ` (6 subsequent siblings)
  13 siblings, 1 reply; 30+ messages in thread
From: Andrey Konovalov @ 2019-02-22 12:53 UTC (permalink / raw)
  To: Catalin Marinas, Will Deacon, Mark Rutland, Robin Murphy,
	Kees Cook, Kate Stewart, Greg Kroah-Hartman, Andrew Morton,
	Ingo Molnar, Kirill A . Shutemov, Shuah Khan, Vincenzo Frascino,
	linux-arm-kernel, linux-doc, linux-mm, linux-arch,
	linux-kselftest, linux-kernel
  Cc: Dmitry Vyukov, Kostya Serebryany, Evgeniy Stepanov, Lee Smith,
	Ramana Radhakrishnan, Jacob Bramley, Ruben Ayrapetyan,
	Chintan Pandya, Luc Van Oostenryck, Dave Martin, Kevin Brodsky,
	Szabolcs Nagy, Andrey Konovalov

userfaultfd_register() and userfaultfd_unregister() use provided user
pointers for vma lookups, which can only by done with untagged pointers.

Untag user pointers in these functions.

Signed-off-by: Andrey Konovalov <andreyknvl@google.com>
---
 fs/userfaultfd.c | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/fs/userfaultfd.c b/fs/userfaultfd.c
index 89800fc7dc9d..a3b70e0d9756 100644
--- a/fs/userfaultfd.c
+++ b/fs/userfaultfd.c
@@ -1320,6 +1320,9 @@ static int userfaultfd_register(struct userfaultfd_ctx *ctx,
 		goto out;
 	}
 
+	uffdio_register.range.start =
+		untagged_addr(uffdio_register.range.start);
+
 	ret = validate_range(mm, uffdio_register.range.start,
 			     uffdio_register.range.len);
 	if (ret)
@@ -1507,6 +1510,8 @@ static int userfaultfd_unregister(struct userfaultfd_ctx *ctx,
 	if (copy_from_user(&uffdio_unregister, buf, sizeof(uffdio_unregister)))
 		goto out;
 
+	uffdio_unregister.start = untagged_addr(uffdio_unregister.start);
+
 	ret = validate_range(mm, uffdio_unregister.start,
 			     uffdio_unregister.len);
 	if (ret)
-- 
2.21.0.rc0.258.g878e2cd30e-goog


^ permalink raw reply related	[flat|nested] 30+ messages in thread

* [PATCH v10 08/12] net, arm64: untag user pointers in tcp_zerocopy_receive
  2019-02-22 12:53 [PATCH v10 00/12] arm64: untag user pointers passed to the kernel Andrey Konovalov
                   ` (6 preceding siblings ...)
  2019-02-22 12:53 ` [PATCH v10 07/12] fs, arm64: untag user pointers in fs/userfaultfd.c Andrey Konovalov
@ 2019-02-22 12:53 ` Andrey Konovalov
  2019-02-22 12:53 ` [PATCH v10 09/12] kernel, arm64: untag user pointers in prctl_set_mm* Andrey Konovalov
                   ` (5 subsequent siblings)
  13 siblings, 0 replies; 30+ messages in thread
From: Andrey Konovalov @ 2019-02-22 12:53 UTC (permalink / raw)
  To: Catalin Marinas, Will Deacon, Mark Rutland, Robin Murphy,
	Kees Cook, Kate Stewart, Greg Kroah-Hartman, Andrew Morton,
	Ingo Molnar, Kirill A . Shutemov, Shuah Khan, Vincenzo Frascino,
	linux-arm-kernel, linux-doc, linux-mm, linux-arch,
	linux-kselftest, linux-kernel
  Cc: Dmitry Vyukov, Kostya Serebryany, Evgeniy Stepanov, Lee Smith,
	Ramana Radhakrishnan, Jacob Bramley, Ruben Ayrapetyan,
	Chintan Pandya, Luc Van Oostenryck, Dave Martin, Kevin Brodsky,
	Szabolcs Nagy, Andrey Konovalov

tcp_zerocopy_receive() uses provided user pointers for vma lookups, which
can only by done with untagged pointers.

Untag user pointers in this function.

Signed-off-by: Andrey Konovalov <andreyknvl@google.com>
---
 net/ipv4/tcp.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c
index cf3c5095c10e..80f3c1fb9809 100644
--- a/net/ipv4/tcp.c
+++ b/net/ipv4/tcp.c
@@ -1756,6 +1756,8 @@ static int tcp_zerocopy_receive(struct sock *sk,
 	int inq;
 	int ret;
 
+	address = untagged_addr(address);
+
 	if (address & (PAGE_SIZE - 1) || address != zc->address)
 		return -EINVAL;
 
-- 
2.21.0.rc0.258.g878e2cd30e-goog


^ permalink raw reply related	[flat|nested] 30+ messages in thread

* [PATCH v10 09/12] kernel, arm64: untag user pointers in prctl_set_mm*
  2019-02-22 12:53 [PATCH v10 00/12] arm64: untag user pointers passed to the kernel Andrey Konovalov
                   ` (7 preceding siblings ...)
  2019-02-22 12:53 ` [PATCH v10 08/12] net, arm64: untag user pointers in tcp_zerocopy_receive Andrey Konovalov
@ 2019-02-22 12:53 ` Andrey Konovalov
  2019-02-22 12:53 ` [PATCH v10 10/12] tracing, arm64: untag user pointers in seq_print_user_ip Andrey Konovalov
                   ` (4 subsequent siblings)
  13 siblings, 0 replies; 30+ messages in thread
From: Andrey Konovalov @ 2019-02-22 12:53 UTC (permalink / raw)
  To: Catalin Marinas, Will Deacon, Mark Rutland, Robin Murphy,
	Kees Cook, Kate Stewart, Greg Kroah-Hartman, Andrew Morton,
	Ingo Molnar, Kirill A . Shutemov, Shuah Khan, Vincenzo Frascino,
	linux-arm-kernel, linux-doc, linux-mm, linux-arch,
	linux-kselftest, linux-kernel
  Cc: Dmitry Vyukov, Kostya Serebryany, Evgeniy Stepanov, Lee Smith,
	Ramana Radhakrishnan, Jacob Bramley, Ruben Ayrapetyan,
	Chintan Pandya, Luc Van Oostenryck, Dave Martin, Kevin Brodsky,
	Szabolcs Nagy, Andrey Konovalov

prctl_set_mm() and prctl_set_mm_map() use provided user pointers for vma
lookups, which can only by done with untagged pointers.

Untag user pointers in these functions.

Signed-off-by: Andrey Konovalov <andreyknvl@google.com>
---
 kernel/sys.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

diff --git a/kernel/sys.c b/kernel/sys.c
index f7eb62eceb24..12910be94b7f 100644
--- a/kernel/sys.c
+++ b/kernel/sys.c
@@ -1992,6 +1992,18 @@ static int prctl_set_mm_map(int opt, const void __user *addr, unsigned long data
 	if (copy_from_user(&prctl_map, addr, sizeof(prctl_map)))
 		return -EFAULT;
 
+	prctl_map->start_code	= untagged_addr(prctl_map.start_code);
+	prctl_map->end_code	= untagged_addr(prctl_map.end_code);
+	prctl_map->start_data	= untagged_addr(prctl_map.start_data);
+	prctl_map->end_data	= untagged_addr(prctl_map.end_data);
+	prctl_map->start_brk	= untagged_addr(prctl_map.start_brk);
+	prctl_map->brk		= untagged_addr(prctl_map.brk);
+	prctl_map->start_stack	= untagged_addr(prctl_map.start_stack);
+	prctl_map->arg_start	= untagged_addr(prctl_map.arg_start);
+	prctl_map->arg_end	= untagged_addr(prctl_map.arg_end);
+	prctl_map->env_start	= untagged_addr(prctl_map.env_start);
+	prctl_map->env_end	= untagged_addr(prctl_map.env_end);
+
 	error = validate_prctl_map(&prctl_map);
 	if (error)
 		return error;
@@ -2105,6 +2117,8 @@ static int prctl_set_mm(int opt, unsigned long addr,
 			      opt != PR_SET_MM_MAP_SIZE)))
 		return -EINVAL;
 
+	addr = untagged_addr(addr);
+
 #ifdef CONFIG_CHECKPOINT_RESTORE
 	if (opt == PR_SET_MM_MAP || opt == PR_SET_MM_MAP_SIZE)
 		return prctl_set_mm_map(opt, (const void __user *)addr, arg4);
-- 
2.21.0.rc0.258.g878e2cd30e-goog


^ permalink raw reply related	[flat|nested] 30+ messages in thread

* [PATCH v10 10/12] tracing, arm64: untag user pointers in seq_print_user_ip
  2019-02-22 12:53 [PATCH v10 00/12] arm64: untag user pointers passed to the kernel Andrey Konovalov
                   ` (8 preceding siblings ...)
  2019-02-22 12:53 ` [PATCH v10 09/12] kernel, arm64: untag user pointers in prctl_set_mm* Andrey Konovalov
@ 2019-02-22 12:53 ` Andrey Konovalov
  2019-02-22 12:53 ` [PATCH v10 11/12] arm64: update Documentation/arm64/tagged-pointers.txt Andrey Konovalov
                   ` (3 subsequent siblings)
  13 siblings, 0 replies; 30+ messages in thread
From: Andrey Konovalov @ 2019-02-22 12:53 UTC (permalink / raw)
  To: Catalin Marinas, Will Deacon, Mark Rutland, Robin Murphy,
	Kees Cook, Kate Stewart, Greg Kroah-Hartman, Andrew Morton,
	Ingo Molnar, Kirill A . Shutemov, Shuah Khan, Vincenzo Frascino,
	linux-arm-kernel, linux-doc, linux-mm, linux-arch,
	linux-kselftest, linux-kernel
  Cc: Dmitry Vyukov, Kostya Serebryany, Evgeniy Stepanov, Lee Smith,
	Ramana Radhakrishnan, Jacob Bramley, Ruben Ayrapetyan,
	Chintan Pandya, Luc Van Oostenryck, Dave Martin, Kevin Brodsky,
	Szabolcs Nagy, Andrey Konovalov

seq_print_user_ip() uses provided user pointers for vma lookups, which
can only by done with untagged pointers.

Untag user pointers in this function.

Signed-off-by: Andrey Konovalov <andreyknvl@google.com>
---
 kernel/trace/trace_output.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/kernel/trace/trace_output.c b/kernel/trace/trace_output.c
index 54373d93e251..7c893328f97b 100644
--- a/kernel/trace/trace_output.c
+++ b/kernel/trace/trace_output.c
@@ -379,7 +379,7 @@ static int seq_print_user_ip(struct trace_seq *s, struct mm_struct *mm,
 		const struct vm_area_struct *vma;
 
 		down_read(&mm->mmap_sem);
-		vma = find_vma(mm, ip);
+		vma = find_vma(mm, untagged_addr(ip));
 		if (vma) {
 			file = vma->vm_file;
 			vmstart = vma->vm_start;
-- 
2.21.0.rc0.258.g878e2cd30e-goog


^ permalink raw reply related	[flat|nested] 30+ messages in thread

* [PATCH v10 11/12] arm64: update Documentation/arm64/tagged-pointers.txt
  2019-02-22 12:53 [PATCH v10 00/12] arm64: untag user pointers passed to the kernel Andrey Konovalov
                   ` (9 preceding siblings ...)
  2019-02-22 12:53 ` [PATCH v10 10/12] tracing, arm64: untag user pointers in seq_print_user_ip Andrey Konovalov
@ 2019-02-22 12:53 ` Andrey Konovalov
  2019-02-22 12:53 ` [PATCH v10 12/12] selftests, arm64: add a selftest for passing tagged pointers to kernel Andrey Konovalov
                   ` (2 subsequent siblings)
  13 siblings, 0 replies; 30+ messages in thread
From: Andrey Konovalov @ 2019-02-22 12:53 UTC (permalink / raw)
  To: Catalin Marinas, Will Deacon, Mark Rutland, Robin Murphy,
	Kees Cook, Kate Stewart, Greg Kroah-Hartman, Andrew Morton,
	Ingo Molnar, Kirill A . Shutemov, Shuah Khan, Vincenzo Frascino,
	linux-arm-kernel, linux-doc, linux-mm, linux-arch,
	linux-kselftest, linux-kernel
  Cc: Dmitry Vyukov, Kostya Serebryany, Evgeniy Stepanov, Lee Smith,
	Ramana Radhakrishnan, Jacob Bramley, Ruben Ayrapetyan,
	Chintan Pandya, Luc Van Oostenryck, Dave Martin, Kevin Brodsky,
	Szabolcs Nagy, Andrey Konovalov

Document the changes in Documentation/arm64/tagged-pointers.txt.

Signed-off-by: Andrey Konovalov <andreyknvl@google.com>
---
 Documentation/arm64/tagged-pointers.txt | 25 +++++++++++++++----------
 1 file changed, 15 insertions(+), 10 deletions(-)

diff --git a/Documentation/arm64/tagged-pointers.txt b/Documentation/arm64/tagged-pointers.txt
index a25a99e82bb1..f4cf1f5cf362 100644
--- a/Documentation/arm64/tagged-pointers.txt
+++ b/Documentation/arm64/tagged-pointers.txt
@@ -17,13 +17,22 @@ this byte for application use.
 Passing tagged addresses to the kernel
 --------------------------------------
 
-All interpretation of userspace memory addresses by the kernel assumes
-an address tag of 0x00.
+The kernel supports tags in pointer arguments (including pointers in
+structures) for a limited set of syscalls, the exceptions are:
 
-This includes, but is not limited to, addresses found in:
+ - memory syscalls: brk, madvise, mbind, mincore, mlock, mlock2, move_pages,
+   mprotect, mremap, msync, munlock, munmap, pkey_mprotect, process_vm_readv,
+   process_vm_writev, remap_file_pages;
 
- - pointer arguments to system calls, including pointers in structures
-   passed to system calls,
+ - ioctls that accept user pointers that describe virtual memory ranges;
+
+ - TCP_ZEROCOPY_RECEIVE setsockopt.
+
+The kernel supports tags in user fault addresses. However the fault_address
+field in the sigcontext struct will contain an untagged address.
+
+All other interpretations of userspace memory addresses by the kernel
+assume an address tag of 0x00, in particular:
 
  - the stack pointer (sp), e.g. when interpreting it to deliver a
    signal,
@@ -33,11 +42,7 @@ This includes, but is not limited to, addresses found in:
 
 Using non-zero address tags in any of these locations may result in an
 error code being returned, a (fatal) signal being raised, or other modes
-of failure.
-
-For these reasons, passing non-zero address tags to the kernel via
-system calls is forbidden, and using a non-zero address tag for sp is
-strongly discouraged.
+of failure. Using a non-zero address tag for sp is strongly discouraged.
 
 Programs maintaining a frame pointer and frame records that use non-zero
 address tags may suffer impaired or inaccurate debug and profiling
-- 
2.21.0.rc0.258.g878e2cd30e-goog


^ permalink raw reply related	[flat|nested] 30+ messages in thread

* [PATCH v10 12/12] selftests, arm64: add a selftest for passing tagged pointers to kernel
  2019-02-22 12:53 [PATCH v10 00/12] arm64: untag user pointers passed to the kernel Andrey Konovalov
                   ` (10 preceding siblings ...)
  2019-02-22 12:53 ` [PATCH v10 11/12] arm64: update Documentation/arm64/tagged-pointers.txt Andrey Konovalov
@ 2019-02-22 12:53 ` Andrey Konovalov
  2019-02-22 15:35 ` [PATCH v10 00/12] arm64: untag user pointers passed to the kernel Szabolcs Nagy
  2019-02-22 22:54 ` Dave Hansen
  13 siblings, 0 replies; 30+ messages in thread
From: Andrey Konovalov @ 2019-02-22 12:53 UTC (permalink / raw)
  To: Catalin Marinas, Will Deacon, Mark Rutland, Robin Murphy,
	Kees Cook, Kate Stewart, Greg Kroah-Hartman, Andrew Morton,
	Ingo Molnar, Kirill A . Shutemov, Shuah Khan, Vincenzo Frascino,
	linux-arm-kernel, linux-doc, linux-mm, linux-arch,
	linux-kselftest, linux-kernel
  Cc: Dmitry Vyukov, Kostya Serebryany, Evgeniy Stepanov, Lee Smith,
	Ramana Radhakrishnan, Jacob Bramley, Ruben Ayrapetyan,
	Chintan Pandya, Luc Van Oostenryck, Dave Martin, Kevin Brodsky,
	Szabolcs Nagy, Andrey Konovalov

This patch adds a simple test, that calls the uname syscall with a
tagged user pointer as an argument. Without the kernel accepting tagged
user pointers the test fails with EFAULT.

Signed-off-by: Andrey Konovalov <andreyknvl@google.com>
---
 tools/testing/selftests/arm64/.gitignore      |  1 +
 tools/testing/selftests/arm64/Makefile        | 11 +++++++++++
 .../testing/selftests/arm64/run_tags_test.sh  | 12 ++++++++++++
 tools/testing/selftests/arm64/tags_test.c     | 19 +++++++++++++++++++
 4 files changed, 43 insertions(+)
 create mode 100644 tools/testing/selftests/arm64/.gitignore
 create mode 100644 tools/testing/selftests/arm64/Makefile
 create mode 100755 tools/testing/selftests/arm64/run_tags_test.sh
 create mode 100644 tools/testing/selftests/arm64/tags_test.c

diff --git a/tools/testing/selftests/arm64/.gitignore b/tools/testing/selftests/arm64/.gitignore
new file mode 100644
index 000000000000..e8fae8d61ed6
--- /dev/null
+++ b/tools/testing/selftests/arm64/.gitignore
@@ -0,0 +1 @@
+tags_test
diff --git a/tools/testing/selftests/arm64/Makefile b/tools/testing/selftests/arm64/Makefile
new file mode 100644
index 000000000000..a61b2e743e99
--- /dev/null
+++ b/tools/testing/selftests/arm64/Makefile
@@ -0,0 +1,11 @@
+# SPDX-License-Identifier: GPL-2.0
+
+# ARCH can be overridden by the user for cross compiling
+ARCH ?= $(shell uname -m 2>/dev/null || echo not)
+
+ifneq (,$(filter $(ARCH),aarch64 arm64))
+TEST_GEN_PROGS := tags_test
+TEST_PROGS := run_tags_test.sh
+endif
+
+include ../lib.mk
diff --git a/tools/testing/selftests/arm64/run_tags_test.sh b/tools/testing/selftests/arm64/run_tags_test.sh
new file mode 100755
index 000000000000..745f11379930
--- /dev/null
+++ b/tools/testing/selftests/arm64/run_tags_test.sh
@@ -0,0 +1,12 @@
+#!/bin/sh
+# SPDX-License-Identifier: GPL-2.0
+
+echo "--------------------"
+echo "running tags test"
+echo "--------------------"
+./tags_test
+if [ $? -ne 0 ]; then
+	echo "[FAIL]"
+else
+	echo "[PASS]"
+fi
diff --git a/tools/testing/selftests/arm64/tags_test.c b/tools/testing/selftests/arm64/tags_test.c
new file mode 100644
index 000000000000..1452ed7d33f9
--- /dev/null
+++ b/tools/testing/selftests/arm64/tags_test.c
@@ -0,0 +1,19 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <stdio.h>
+#include <unistd.h>
+#include <stdint.h>
+#include <sys/utsname.h>
+
+#define SHIFT_TAG(tag)		((uint64_t)(tag) << 56)
+#define SET_TAG(ptr, tag)	(((uint64_t)(ptr) & ~SHIFT_TAG(0xff)) | \
+					SHIFT_TAG(tag))
+
+int main(void)
+{
+	struct utsname utsname;
+	void *ptr = &utsname;
+	void *tagged_ptr = (void *)SET_TAG(ptr, 0x42);
+	int err = uname(tagged_ptr);
+	return err;
+}
-- 
2.21.0.rc0.258.g878e2cd30e-goog


^ permalink raw reply related	[flat|nested] 30+ messages in thread

* Re: [PATCH v10 00/12] arm64: untag user pointers passed to the kernel
  2019-02-22 12:53 [PATCH v10 00/12] arm64: untag user pointers passed to the kernel Andrey Konovalov
                   ` (11 preceding siblings ...)
  2019-02-22 12:53 ` [PATCH v10 12/12] selftests, arm64: add a selftest for passing tagged pointers to kernel Andrey Konovalov
@ 2019-02-22 15:35 ` Szabolcs Nagy
  2019-02-22 15:40   ` Andrey Konovalov
  2019-02-22 22:54 ` Dave Hansen
  13 siblings, 1 reply; 30+ messages in thread
From: Szabolcs Nagy @ 2019-02-22 15:35 UTC (permalink / raw)
  To: Andrey Konovalov, Catalin Marinas, Will Deacon, Mark Rutland,
	Robin Murphy, Kees Cook, Kate Stewart, Greg Kroah-Hartman,
	Andrew Morton, Ingo Molnar, Kirill A . Shutemov, Shuah Khan,
	Vincenzo Frascino, linux-arm-kernel, linux-doc, linux-mm,
	linux-arch, linux-kselftest, linux-kernel
  Cc: nd, Dmitry Vyukov, Kostya Serebryany, Evgeniy Stepanov,
	Lee Smith, Ramana Radhakrishnan, Jacob Bramley, Ruben Ayrapetyan,
	Chintan Pandya, Luc Van Oostenryck, Dave P Martin, Kevin Brodsky

On 22/02/2019 12:53, Andrey Konovalov wrote:
> This patchset is meant to be merged together with "arm64 relaxed ABI" [1].
> 
> arm64 has a feature called Top Byte Ignore, which allows to embed pointer
> tags into the top byte of each pointer. Userspace programs (such as
> HWASan, a memory debugging tool [2]) might use this feature and pass
> tagged user pointers to the kernel through syscalls or other interfaces.
> 
> Right now the kernel is already able to handle user faults with tagged
> pointers, due to these patches:
> 
> 1. 81cddd65 ("arm64: traps: fix userspace cache maintenance emulation on a
>              tagged pointer")
> 2. 7dcd9dd8 ("arm64: hw_breakpoint: fix watchpoint matching for tagged
> 	      pointers")
> 3. 276e9327 ("arm64: entry: improve data abort handling of tagged
> 	      pointers")
> 
> This patchset extends tagged pointer support to syscall arguments.
> 
> For non-memory syscalls this is done by untaging user pointers when the
> kernel performs pointer checking to find out whether the pointer comes
> from userspace (most notably in access_ok). The untagging is done only
> when the pointer is being checked, the tag is preserved as the pointer
> makes its way through the kernel.
> 
> Since memory syscalls (mmap, mprotect, etc.) don't do memory accesses but
> rather deal with memory ranges, untagged pointers are better suited to
> describe memory ranges internally. Thus for memory syscalls we untag
> pointers completely when they enter the kernel.

i think the same is true when user pointers are compared.

e.g. i suspect there may be issues with tagged robust mutex
list pointers because the kernel does

futex.c:3541:	while (entry != &head->list) {

where entry is a user pointer that may be tagged, and
&head->list is probably not tagged.

> One of the alternative approaches to untagging that was considered is to
> completely strip the pointer tag as the pointer enters the kernel with
> some kind of a syscall wrapper, but that won't work with the countless
> number of different ioctl calls. With this approach we would need a custom
> wrapper for each ioctl variation, which doesn't seem practical.
> 
> The following testing approaches has been taken to find potential issues
> with user pointer untagging:
> 
> 1. Static testing (with sparse [3] and separately with a custom static
>    analyzer based on Clang) to track casts of __user pointers to integer
>    types to find places where untagging needs to be done.
> 
> 2. Static testing with grep to find parts of the kernel that call
>    find_vma() (and other similar functions) or directly compare against
>    vm_start/vm_end fields of vma.
> 
> 3. Static testing with grep to find parts of the kernel that compare
>    user pointers with TASK_SIZE or other similar consts and macros.
> 
> 4. Dynamic testing: adding BUG_ON(has_tag(addr)) to find_vma() and running
>    a modified syzkaller version that passes tagged pointers to the kernel.
> 
> Based on the results of the testing the requried patches have been added
> to the patchset.
> 
> This patchset has been merged into the Pixel 2 kernel tree and is now
> being used to enable testing of Pixel 2 phones with HWASan.
> 
> This patchset is a prerequisite for ARM's memory tagging hardware feature
> support [4].
> 
> Thanks!
> 
> [1] https://lkml.org/lkml/2018/12/10/402
> 
> [2] http://clang.llvm.org/docs/HardwareAssistedAddressSanitizerDesign.html
> 
> [3] https://github.com/lucvoo/sparse-dev/commit/5f960cb10f56ec2017c128ef9d16060e0145f292
> 
> [4] https://community.arm.com/processors/b/blog/posts/arm-a-profile-architecture-2018-developments-armv85a
> 
> Changes in v10:
> - Added "mm, arm64: untag user pointers passed to memory syscalls" back.
> - New patch "fs, arm64: untag user pointers in fs/userfaultfd.c".
> - New patch "net, arm64: untag user pointers in tcp_zerocopy_receive".
> - New patch "kernel, arm64: untag user pointers in prctl_set_mm*".
> - New patch "tracing, arm64: untag user pointers in seq_print_user_ip".
> 
> Changes in v9:
> - Rebased onto 4.20-rc6.
> - Used u64 instead of __u64 in type casts in the untagged_addr macro for
>   arm64.
> - Added braces around (addr) in the untagged_addr macro for other arches.
> 
> Changes in v8:
> - Rebased onto 65102238 (4.20-rc1).
> - Added a note to the cover letter on why syscall wrappers/shims that untag
>   user pointers won't work.
> - Added a note to the cover letter that this patchset has been merged into
>   the Pixel 2 kernel tree.
> - Documentation fixes, in particular added a list of syscalls that don't
>   support tagged user pointers.
> 
> Changes in v7:
> - Rebased onto 17b57b18 (4.19-rc6).
> - Dropped the "arm64: untag user address in __do_user_fault" patch, since
>   the existing patches already handle user faults properly.
> - Dropped the "usb, arm64: untag user addresses in devio" patch, since the
>   passed pointer must come from a vma and therefore be untagged.
> - Dropped the "arm64: annotate user pointers casts detected by sparse"
>   patch (see the discussion to the replies of the v6 of this patchset).
> - Added more context to the cover letter.
> - Updated Documentation/arm64/tagged-pointers.txt.
> 
> Changes in v6:
> - Added annotations for user pointer casts found by sparse.
> - Rebased onto 050cdc6c (4.19-rc1+).
> 
> Changes in v5:
> - Added 3 new patches that add untagging to places found with static
>   analysis.
> - Rebased onto 44c929e1 (4.18-rc8).
> 
> Changes in v4:
> - Added a selftest for checking that passing tagged pointers to the
>   kernel succeeds.
> - Rebased onto 81e97f013 (4.18-rc1+).
> 
> Changes in v3:
> - Rebased onto e5c51f30 (4.17-rc6+).
> - Added linux-arch@ to the list of recipients.
> 
> Changes in v2:
> - Rebased onto 2d618bdf (4.17-rc3+).
> - Removed excessive untagging in gup.c.
> - Removed untagging pointers returned from __uaccess_mask_ptr.
> 
> Changes in v1:
> - Rebased onto 4.17-rc1.
> 
> Changes in RFC v2:
> - Added "#ifndef untagged_addr..." fallback in linux/uaccess.h instead of
>   defining it for each arch individually.
> - Updated Documentation/arm64/tagged-pointers.txt.
> - Dropped "mm, arm64: untag user addresses in memory syscalls".
> - Rebased onto 3eb2ce82 (4.16-rc7).
> 
> Reviewed-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
> Signed-off-by: Andrey Konovalov <andreyknvl@google.com>
> 
> Andrey Konovalov (12):
>   uaccess: add untagged_addr definition for other arches
>   arm64: untag user pointers in access_ok and __uaccess_mask_ptr
>   lib, arm64: untag user pointers in strn*_user
>   mm, arm64: untag user pointers passed to memory syscalls
>   mm, arm64: untag user pointers in mm/gup.c
>   fs, arm64: untag user pointers in copy_mount_options
>   fs, arm64: untag user pointers in fs/userfaultfd.c
>   net, arm64: untag user pointers in tcp_zerocopy_receive
>   kernel, arm64: untag user pointers in prctl_set_mm*
>   tracing, arm64: untag user pointers in seq_print_user_ip
>   arm64: update Documentation/arm64/tagged-pointers.txt
>   selftests, arm64: add a selftest for passing tagged pointers to kernel
> 
>  Documentation/arm64/tagged-pointers.txt       | 25 +++++++++++--------
>  arch/arm64/include/asm/uaccess.h              | 10 +++++---
>  fs/namespace.c                                |  2 +-
>  fs/userfaultfd.c                              |  5 ++++
>  include/linux/memory.h                        |  4 +++
>  ipc/shm.c                                     |  2 ++
>  kernel/sys.c                                  | 14 +++++++++++
>  kernel/trace/trace_output.c                   |  2 +-
>  lib/strncpy_from_user.c                       |  2 ++
>  lib/strnlen_user.c                            |  2 ++
>  mm/gup.c                                      |  4 +++
>  mm/madvise.c                                  |  2 ++
>  mm/mempolicy.c                                |  5 ++++
>  mm/migrate.c                                  |  1 +
>  mm/mincore.c                                  |  2 ++
>  mm/mlock.c                                    |  5 ++++
>  mm/mmap.c                                     |  7 ++++++
>  mm/mprotect.c                                 |  2 ++
>  mm/mremap.c                                   |  2 ++
>  mm/msync.c                                    |  2 ++
>  net/ipv4/tcp.c                                |  2 ++
>  tools/testing/selftests/arm64/.gitignore      |  1 +
>  tools/testing/selftests/arm64/Makefile        | 11 ++++++++
>  .../testing/selftests/arm64/run_tags_test.sh  | 12 +++++++++
>  tools/testing/selftests/arm64/tags_test.c     | 19 ++++++++++++++
>  25 files changed, 129 insertions(+), 16 deletions(-)
>  create mode 100644 tools/testing/selftests/arm64/.gitignore
>  create mode 100644 tools/testing/selftests/arm64/Makefile
>  create mode 100755 tools/testing/selftests/arm64/run_tags_test.sh
>  create mode 100644 tools/testing/selftests/arm64/tags_test.c
> 


^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [PATCH v10 00/12] arm64: untag user pointers passed to the kernel
  2019-02-22 15:35 ` [PATCH v10 00/12] arm64: untag user pointers passed to the kernel Szabolcs Nagy
@ 2019-02-22 15:40   ` Andrey Konovalov
  2019-02-22 16:10     ` Szabolcs Nagy
  0 siblings, 1 reply; 30+ messages in thread
From: Andrey Konovalov @ 2019-02-22 15:40 UTC (permalink / raw)
  To: Szabolcs Nagy
  Cc: Catalin Marinas, Will Deacon, Mark Rutland, Robin Murphy,
	Kees Cook, Kate Stewart, Greg Kroah-Hartman, Andrew Morton,
	Ingo Molnar, Kirill A . Shutemov, Shuah Khan, Vincenzo Frascino,
	linux-arm-kernel, linux-doc, linux-mm, linux-arch,
	linux-kselftest, linux-kernel, nd, Dmitry Vyukov,
	Kostya Serebryany, Evgeniy Stepanov, Lee Smith,
	Ramana Radhakrishnan, Jacob Bramley, Ruben Ayrapetyan,
	Chintan Pandya, Luc Van Oostenryck, Dave P Martin, Kevin Brodsky

On Fri, Feb 22, 2019 at 4:35 PM Szabolcs Nagy <Szabolcs.Nagy@arm.com> wrote:
>
> On 22/02/2019 12:53, Andrey Konovalov wrote:
> > This patchset is meant to be merged together with "arm64 relaxed ABI" [1].
> >
> > arm64 has a feature called Top Byte Ignore, which allows to embed pointer
> > tags into the top byte of each pointer. Userspace programs (such as
> > HWASan, a memory debugging tool [2]) might use this feature and pass
> > tagged user pointers to the kernel through syscalls or other interfaces.
> >
> > Right now the kernel is already able to handle user faults with tagged
> > pointers, due to these patches:
> >
> > 1. 81cddd65 ("arm64: traps: fix userspace cache maintenance emulation on a
> >              tagged pointer")
> > 2. 7dcd9dd8 ("arm64: hw_breakpoint: fix watchpoint matching for tagged
> >             pointers")
> > 3. 276e9327 ("arm64: entry: improve data abort handling of tagged
> >             pointers")
> >
> > This patchset extends tagged pointer support to syscall arguments.
> >
> > For non-memory syscalls this is done by untaging user pointers when the
> > kernel performs pointer checking to find out whether the pointer comes
> > from userspace (most notably in access_ok). The untagging is done only
> > when the pointer is being checked, the tag is preserved as the pointer
> > makes its way through the kernel.
> >
> > Since memory syscalls (mmap, mprotect, etc.) don't do memory accesses but
> > rather deal with memory ranges, untagged pointers are better suited to
> > describe memory ranges internally. Thus for memory syscalls we untag
> > pointers completely when they enter the kernel.
>
> i think the same is true when user pointers are compared.
>
> e.g. i suspect there may be issues with tagged robust mutex
> list pointers because the kernel does
>
> futex.c:3541:   while (entry != &head->list) {
>
> where entry is a user pointer that may be tagged, and
> &head->list is probably not tagged.

You're right. I'll expand the cover letter in the next version to
describe this more accurately. The patchset however contains "mm,
arm64: untag user pointers in mm/gup.c" that should take care of futex
pointers.

Thanks!

>
> > One of the alternative approaches to untagging that was considered is to
> > completely strip the pointer tag as the pointer enters the kernel with
> > some kind of a syscall wrapper, but that won't work with the countless
> > number of different ioctl calls. With this approach we would need a custom
> > wrapper for each ioctl variation, which doesn't seem practical.
> >
> > The following testing approaches has been taken to find potential issues
> > with user pointer untagging:
> >
> > 1. Static testing (with sparse [3] and separately with a custom static
> >    analyzer based on Clang) to track casts of __user pointers to integer
> >    types to find places where untagging needs to be done.
> >
> > 2. Static testing with grep to find parts of the kernel that call
> >    find_vma() (and other similar functions) or directly compare against
> >    vm_start/vm_end fields of vma.
> >
> > 3. Static testing with grep to find parts of the kernel that compare
> >    user pointers with TASK_SIZE or other similar consts and macros.
> >
> > 4. Dynamic testing: adding BUG_ON(has_tag(addr)) to find_vma() and running
> >    a modified syzkaller version that passes tagged pointers to the kernel.
> >
> > Based on the results of the testing the requried patches have been added
> > to the patchset.
> >
> > This patchset has been merged into the Pixel 2 kernel tree and is now
> > being used to enable testing of Pixel 2 phones with HWASan.
> >
> > This patchset is a prerequisite for ARM's memory tagging hardware feature
> > support [4].
> >
> > Thanks!
> >
> > [1] https://lkml.org/lkml/2018/12/10/402
> >
> > [2] http://clang.llvm.org/docs/HardwareAssistedAddressSanitizerDesign.html
> >
> > [3] https://github.com/lucvoo/sparse-dev/commit/5f960cb10f56ec2017c128ef9d16060e0145f292
> >
> > [4] https://community.arm.com/processors/b/blog/posts/arm-a-profile-architecture-2018-developments-armv85a
> >
> > Changes in v10:
> > - Added "mm, arm64: untag user pointers passed to memory syscalls" back.
> > - New patch "fs, arm64: untag user pointers in fs/userfaultfd.c".
> > - New patch "net, arm64: untag user pointers in tcp_zerocopy_receive".
> > - New patch "kernel, arm64: untag user pointers in prctl_set_mm*".
> > - New patch "tracing, arm64: untag user pointers in seq_print_user_ip".
> >
> > Changes in v9:
> > - Rebased onto 4.20-rc6.
> > - Used u64 instead of __u64 in type casts in the untagged_addr macro for
> >   arm64.
> > - Added braces around (addr) in the untagged_addr macro for other arches.
> >
> > Changes in v8:
> > - Rebased onto 65102238 (4.20-rc1).
> > - Added a note to the cover letter on why syscall wrappers/shims that untag
> >   user pointers won't work.
> > - Added a note to the cover letter that this patchset has been merged into
> >   the Pixel 2 kernel tree.
> > - Documentation fixes, in particular added a list of syscalls that don't
> >   support tagged user pointers.
> >
> > Changes in v7:
> > - Rebased onto 17b57b18 (4.19-rc6).
> > - Dropped the "arm64: untag user address in __do_user_fault" patch, since
> >   the existing patches already handle user faults properly.
> > - Dropped the "usb, arm64: untag user addresses in devio" patch, since the
> >   passed pointer must come from a vma and therefore be untagged.
> > - Dropped the "arm64: annotate user pointers casts detected by sparse"
> >   patch (see the discussion to the replies of the v6 of this patchset).
> > - Added more context to the cover letter.
> > - Updated Documentation/arm64/tagged-pointers.txt.
> >
> > Changes in v6:
> > - Added annotations for user pointer casts found by sparse.
> > - Rebased onto 050cdc6c (4.19-rc1+).
> >
> > Changes in v5:
> > - Added 3 new patches that add untagging to places found with static
> >   analysis.
> > - Rebased onto 44c929e1 (4.18-rc8).
> >
> > Changes in v4:
> > - Added a selftest for checking that passing tagged pointers to the
> >   kernel succeeds.
> > - Rebased onto 81e97f013 (4.18-rc1+).
> >
> > Changes in v3:
> > - Rebased onto e5c51f30 (4.17-rc6+).
> > - Added linux-arch@ to the list of recipients.
> >
> > Changes in v2:
> > - Rebased onto 2d618bdf (4.17-rc3+).
> > - Removed excessive untagging in gup.c.
> > - Removed untagging pointers returned from __uaccess_mask_ptr.
> >
> > Changes in v1:
> > - Rebased onto 4.17-rc1.
> >
> > Changes in RFC v2:
> > - Added "#ifndef untagged_addr..." fallback in linux/uaccess.h instead of
> >   defining it for each arch individually.
> > - Updated Documentation/arm64/tagged-pointers.txt.
> > - Dropped "mm, arm64: untag user addresses in memory syscalls".
> > - Rebased onto 3eb2ce82 (4.16-rc7).
> >
> > Reviewed-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
> > Signed-off-by: Andrey Konovalov <andreyknvl@google.com>
> >
> > Andrey Konovalov (12):
> >   uaccess: add untagged_addr definition for other arches
> >   arm64: untag user pointers in access_ok and __uaccess_mask_ptr
> >   lib, arm64: untag user pointers in strn*_user
> >   mm, arm64: untag user pointers passed to memory syscalls
> >   mm, arm64: untag user pointers in mm/gup.c
> >   fs, arm64: untag user pointers in copy_mount_options
> >   fs, arm64: untag user pointers in fs/userfaultfd.c
> >   net, arm64: untag user pointers in tcp_zerocopy_receive
> >   kernel, arm64: untag user pointers in prctl_set_mm*
> >   tracing, arm64: untag user pointers in seq_print_user_ip
> >   arm64: update Documentation/arm64/tagged-pointers.txt
> >   selftests, arm64: add a selftest for passing tagged pointers to kernel
> >
> >  Documentation/arm64/tagged-pointers.txt       | 25 +++++++++++--------
> >  arch/arm64/include/asm/uaccess.h              | 10 +++++---
> >  fs/namespace.c                                |  2 +-
> >  fs/userfaultfd.c                              |  5 ++++
> >  include/linux/memory.h                        |  4 +++
> >  ipc/shm.c                                     |  2 ++
> >  kernel/sys.c                                  | 14 +++++++++++
> >  kernel/trace/trace_output.c                   |  2 +-
> >  lib/strncpy_from_user.c                       |  2 ++
> >  lib/strnlen_user.c                            |  2 ++
> >  mm/gup.c                                      |  4 +++
> >  mm/madvise.c                                  |  2 ++
> >  mm/mempolicy.c                                |  5 ++++
> >  mm/migrate.c                                  |  1 +
> >  mm/mincore.c                                  |  2 ++
> >  mm/mlock.c                                    |  5 ++++
> >  mm/mmap.c                                     |  7 ++++++
> >  mm/mprotect.c                                 |  2 ++
> >  mm/mremap.c                                   |  2 ++
> >  mm/msync.c                                    |  2 ++
> >  net/ipv4/tcp.c                                |  2 ++
> >  tools/testing/selftests/arm64/.gitignore      |  1 +
> >  tools/testing/selftests/arm64/Makefile        | 11 ++++++++
> >  .../testing/selftests/arm64/run_tags_test.sh  | 12 +++++++++
> >  tools/testing/selftests/arm64/tags_test.c     | 19 ++++++++++++++
> >  25 files changed, 129 insertions(+), 16 deletions(-)
> >  create mode 100644 tools/testing/selftests/arm64/.gitignore
> >  create mode 100644 tools/testing/selftests/arm64/Makefile
> >  create mode 100755 tools/testing/selftests/arm64/run_tags_test.sh
> >  create mode 100644 tools/testing/selftests/arm64/tags_test.c
> >
>

^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [PATCH v10 00/12] arm64: untag user pointers passed to the kernel
  2019-02-22 15:40   ` Andrey Konovalov
@ 2019-02-22 16:10     ` Szabolcs Nagy
  2019-02-26 17:00       ` Andrey Konovalov
  0 siblings, 1 reply; 30+ messages in thread
From: Szabolcs Nagy @ 2019-02-22 16:10 UTC (permalink / raw)
  To: Andrey Konovalov
  Cc: nd, Catalin Marinas, Will Deacon, Mark Rutland, Robin Murphy,
	Kees Cook, Kate Stewart, Greg Kroah-Hartman, Andrew Morton,
	Ingo Molnar, Kirill A . Shutemov, Shuah Khan, Vincenzo Frascino,
	linux-arm-kernel, linux-doc, linux-mm, linux-arch,
	linux-kselftest, linux-kernel, Dmitry Vyukov, Kostya Serebryany,
	Evgeniy Stepanov, Lee Smith, Ramana Radhakrishnan, Jacob Bramley,
	Ruben Ayrapetyan, Chintan Pandya, Luc Van Oostenryck,
	Dave P Martin, Kevin Brodsky

On 22/02/2019 15:40, Andrey Konovalov wrote:
> On Fri, Feb 22, 2019 at 4:35 PM Szabolcs Nagy <Szabolcs.Nagy@arm.com> wrote:
>>
>> On 22/02/2019 12:53, Andrey Konovalov wrote:
>>> This patchset is meant to be merged together with "arm64 relaxed ABI" [1].
>>>
>>> arm64 has a feature called Top Byte Ignore, which allows to embed pointer
>>> tags into the top byte of each pointer. Userspace programs (such as
>>> HWASan, a memory debugging tool [2]) might use this feature and pass
>>> tagged user pointers to the kernel through syscalls or other interfaces.
>>>
>>> Right now the kernel is already able to handle user faults with tagged
>>> pointers, due to these patches:
>>>
>>> 1. 81cddd65 ("arm64: traps: fix userspace cache maintenance emulation on a
>>>              tagged pointer")
>>> 2. 7dcd9dd8 ("arm64: hw_breakpoint: fix watchpoint matching for tagged
>>>             pointers")
>>> 3. 276e9327 ("arm64: entry: improve data abort handling of tagged
>>>             pointers")
>>>
>>> This patchset extends tagged pointer support to syscall arguments.
>>>
>>> For non-memory syscalls this is done by untaging user pointers when the
>>> kernel performs pointer checking to find out whether the pointer comes
>>> from userspace (most notably in access_ok). The untagging is done only
>>> when the pointer is being checked, the tag is preserved as the pointer
>>> makes its way through the kernel.
>>>
>>> Since memory syscalls (mmap, mprotect, etc.) don't do memory accesses but
>>> rather deal with memory ranges, untagged pointers are better suited to
>>> describe memory ranges internally. Thus for memory syscalls we untag
>>> pointers completely when they enter the kernel.
>>
>> i think the same is true when user pointers are compared.
>>
>> e.g. i suspect there may be issues with tagged robust mutex
>> list pointers because the kernel does
>>
>> futex.c:3541:   while (entry != &head->list) {
>>
>> where entry is a user pointer that may be tagged, and
>> &head->list is probably not tagged.
> 
> You're right. I'll expand the cover letter in the next version to
> describe this more accurately. The patchset however contains "mm,
> arm64: untag user pointers in mm/gup.c" that should take care of futex
> pointers.

the robust mutex list pointer is not a futex pointer,
i'm not sure how the mm/gup.c patch helps.

>>
>>> One of the alternative approaches to untagging that was considered is to
>>> completely strip the pointer tag as the pointer enters the kernel with
>>> some kind of a syscall wrapper, but that won't work with the countless
>>> number of different ioctl calls. With this approach we would need a custom
>>> wrapper for each ioctl variation, which doesn't seem practical.
>>>
>>> The following testing approaches has been taken to find potential issues
>>> with user pointer untagging:
>>>
>>> 1. Static testing (with sparse [3] and separately with a custom static
>>>    analyzer based on Clang) to track casts of __user pointers to integer
>>>    types to find places where untagging needs to be done.
>>>
>>> 2. Static testing with grep to find parts of the kernel that call
>>>    find_vma() (and other similar functions) or directly compare against
>>>    vm_start/vm_end fields of vma.
>>>
>>> 3. Static testing with grep to find parts of the kernel that compare
>>>    user pointers with TASK_SIZE or other similar consts and macros.
>>>
>>> 4. Dynamic testing: adding BUG_ON(has_tag(addr)) to find_vma() and running
>>>    a modified syzkaller version that passes tagged pointers to the kernel.
>>>
>>> Based on the results of the testing the requried patches have been added
>>> to the patchset.
>>>
>>> This patchset has been merged into the Pixel 2 kernel tree and is now
>>> being used to enable testing of Pixel 2 phones with HWASan.
>>>
>>> This patchset is a prerequisite for ARM's memory tagging hardware feature
>>> support [4].
>>>
>>> Thanks!
>>>
>>> [1] https://lkml.org/lkml/2018/12/10/402
>>>
>>> [2] http://clang.llvm.org/docs/HardwareAssistedAddressSanitizerDesign.html
>>>
>>> [3] https://github.com/lucvoo/sparse-dev/commit/5f960cb10f56ec2017c128ef9d16060e0145f292
>>>
>>> [4] https://community.arm.com/processors/b/blog/posts/arm-a-profile-architecture-2018-developments-armv85a
>>>
>>> Changes in v10:
>>> - Added "mm, arm64: untag user pointers passed to memory syscalls" back.
>>> - New patch "fs, arm64: untag user pointers in fs/userfaultfd.c".
>>> - New patch "net, arm64: untag user pointers in tcp_zerocopy_receive".
>>> - New patch "kernel, arm64: untag user pointers in prctl_set_mm*".
>>> - New patch "tracing, arm64: untag user pointers in seq_print_user_ip".
>>>
>>> Changes in v9:
>>> - Rebased onto 4.20-rc6.
>>> - Used u64 instead of __u64 in type casts in the untagged_addr macro for
>>>   arm64.
>>> - Added braces around (addr) in the untagged_addr macro for other arches.
>>>
>>> Changes in v8:
>>> - Rebased onto 65102238 (4.20-rc1).
>>> - Added a note to the cover letter on why syscall wrappers/shims that untag
>>>   user pointers won't work.
>>> - Added a note to the cover letter that this patchset has been merged into
>>>   the Pixel 2 kernel tree.
>>> - Documentation fixes, in particular added a list of syscalls that don't
>>>   support tagged user pointers.
>>>
>>> Changes in v7:
>>> - Rebased onto 17b57b18 (4.19-rc6).
>>> - Dropped the "arm64: untag user address in __do_user_fault" patch, since
>>>   the existing patches already handle user faults properly.
>>> - Dropped the "usb, arm64: untag user addresses in devio" patch, since the
>>>   passed pointer must come from a vma and therefore be untagged.
>>> - Dropped the "arm64: annotate user pointers casts detected by sparse"
>>>   patch (see the discussion to the replies of the v6 of this patchset).
>>> - Added more context to the cover letter.
>>> - Updated Documentation/arm64/tagged-pointers.txt.
>>>
>>> Changes in v6:
>>> - Added annotations for user pointer casts found by sparse.
>>> - Rebased onto 050cdc6c (4.19-rc1+).
>>>
>>> Changes in v5:
>>> - Added 3 new patches that add untagging to places found with static
>>>   analysis.
>>> - Rebased onto 44c929e1 (4.18-rc8).
>>>
>>> Changes in v4:
>>> - Added a selftest for checking that passing tagged pointers to the
>>>   kernel succeeds.
>>> - Rebased onto 81e97f013 (4.18-rc1+).
>>>
>>> Changes in v3:
>>> - Rebased onto e5c51f30 (4.17-rc6+).
>>> - Added linux-arch@ to the list of recipients.
>>>
>>> Changes in v2:
>>> - Rebased onto 2d618bdf (4.17-rc3+).
>>> - Removed excessive untagging in gup.c.
>>> - Removed untagging pointers returned from __uaccess_mask_ptr.
>>>
>>> Changes in v1:
>>> - Rebased onto 4.17-rc1.
>>>
>>> Changes in RFC v2:
>>> - Added "#ifndef untagged_addr..." fallback in linux/uaccess.h instead of
>>>   defining it for each arch individually.
>>> - Updated Documentation/arm64/tagged-pointers.txt.
>>> - Dropped "mm, arm64: untag user addresses in memory syscalls".
>>> - Rebased onto 3eb2ce82 (4.16-rc7).
>>>
>>> Reviewed-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
>>> Signed-off-by: Andrey Konovalov <andreyknvl@google.com>
>>>
>>> Andrey Konovalov (12):
>>>   uaccess: add untagged_addr definition for other arches
>>>   arm64: untag user pointers in access_ok and __uaccess_mask_ptr
>>>   lib, arm64: untag user pointers in strn*_user
>>>   mm, arm64: untag user pointers passed to memory syscalls
>>>   mm, arm64: untag user pointers in mm/gup.c
>>>   fs, arm64: untag user pointers in copy_mount_options
>>>   fs, arm64: untag user pointers in fs/userfaultfd.c
>>>   net, arm64: untag user pointers in tcp_zerocopy_receive
>>>   kernel, arm64: untag user pointers in prctl_set_mm*
>>>   tracing, arm64: untag user pointers in seq_print_user_ip
>>>   arm64: update Documentation/arm64/tagged-pointers.txt
>>>   selftests, arm64: add a selftest for passing tagged pointers to kernel
>>>
>>>  Documentation/arm64/tagged-pointers.txt       | 25 +++++++++++--------
>>>  arch/arm64/include/asm/uaccess.h              | 10 +++++---
>>>  fs/namespace.c                                |  2 +-
>>>  fs/userfaultfd.c                              |  5 ++++
>>>  include/linux/memory.h                        |  4 +++
>>>  ipc/shm.c                                     |  2 ++
>>>  kernel/sys.c                                  | 14 +++++++++++
>>>  kernel/trace/trace_output.c                   |  2 +-
>>>  lib/strncpy_from_user.c                       |  2 ++
>>>  lib/strnlen_user.c                            |  2 ++
>>>  mm/gup.c                                      |  4 +++
>>>  mm/madvise.c                                  |  2 ++
>>>  mm/mempolicy.c                                |  5 ++++
>>>  mm/migrate.c                                  |  1 +
>>>  mm/mincore.c                                  |  2 ++
>>>  mm/mlock.c                                    |  5 ++++
>>>  mm/mmap.c                                     |  7 ++++++
>>>  mm/mprotect.c                                 |  2 ++
>>>  mm/mremap.c                                   |  2 ++
>>>  mm/msync.c                                    |  2 ++
>>>  net/ipv4/tcp.c                                |  2 ++
>>>  tools/testing/selftests/arm64/.gitignore      |  1 +
>>>  tools/testing/selftests/arm64/Makefile        | 11 ++++++++
>>>  .../testing/selftests/arm64/run_tags_test.sh  | 12 +++++++++
>>>  tools/testing/selftests/arm64/tags_test.c     | 19 ++++++++++++++
>>>  25 files changed, 129 insertions(+), 16 deletions(-)
>>>  create mode 100644 tools/testing/selftests/arm64/.gitignore
>>>  create mode 100644 tools/testing/selftests/arm64/Makefile
>>>  create mode 100755 tools/testing/selftests/arm64/run_tags_test.sh
>>>  create mode 100644 tools/testing/selftests/arm64/tags_test.c
>>>
>>


^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [PATCH v10 00/12] arm64: untag user pointers passed to the kernel
  2019-02-22 12:53 [PATCH v10 00/12] arm64: untag user pointers passed to the kernel Andrey Konovalov
                   ` (12 preceding siblings ...)
  2019-02-22 15:35 ` [PATCH v10 00/12] arm64: untag user pointers passed to the kernel Szabolcs Nagy
@ 2019-02-22 22:54 ` Dave Hansen
  2019-02-26 17:18   ` Andrey Konovalov
  13 siblings, 1 reply; 30+ messages in thread
From: Dave Hansen @ 2019-02-22 22:54 UTC (permalink / raw)
  To: Andrey Konovalov, Catalin Marinas, Will Deacon, Mark Rutland,
	Robin Murphy, Kees Cook, Kate Stewart, Greg Kroah-Hartman,
	Andrew Morton, Ingo Molnar, Kirill A . Shutemov, Shuah Khan,
	Vincenzo Frascino, linux-arm-kernel, linux-doc, linux-mm,
	linux-arch, linux-kselftest, linux-kernel
  Cc: Dmitry Vyukov, Kostya Serebryany, Evgeniy Stepanov, Lee Smith,
	Ramana Radhakrishnan, Jacob Bramley, Ruben Ayrapetyan,
	Chintan Pandya, Luc Van Oostenryck, Dave Martin, Kevin Brodsky,
	Szabolcs Nagy

On 2/22/19 4:53 AM, Andrey Konovalov wrote:
> The following testing approaches has been taken to find potential issues
> with user pointer untagging:
> 
> 1. Static testing (with sparse [3] and separately with a custom static
>    analyzer based on Clang) to track casts of __user pointers to integer
>    types to find places where untagging needs to be done.

First of all, it's really cool that you took this approach.  Sounds like
there was a lot of systematic work to fix up the sites in the existing
codebase.

But, isn't this a _bit_ fragile going forward?  Folks can't just "make
sparse" to find issues with missing untags.  This seems like something
where we would ideally add an __tagged annotation (or something) to the
source tree and then have sparse rules that can look for missed untags.

^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [PATCH v10 06/12] fs, arm64: untag user pointers in copy_mount_options
  2019-02-22 12:53 ` [PATCH v10 06/12] fs, arm64: untag user pointers in copy_mount_options Andrey Konovalov
@ 2019-02-22 23:03   ` Dave Hansen
  2019-02-26 14:35     ` Andrey Konovalov
  0 siblings, 1 reply; 30+ messages in thread
From: Dave Hansen @ 2019-02-22 23:03 UTC (permalink / raw)
  To: Andrey Konovalov, Catalin Marinas, Will Deacon, Mark Rutland,
	Robin Murphy, Kees Cook, Kate Stewart, Greg Kroah-Hartman,
	Andrew Morton, Ingo Molnar, Kirill A . Shutemov, Shuah Khan,
	Vincenzo Frascino, linux-arm-kernel, linux-doc, linux-mm,
	linux-arch, linux-kselftest, linux-kernel
  Cc: Dmitry Vyukov, Kostya Serebryany, Evgeniy Stepanov, Lee Smith,
	Ramana Radhakrishnan, Jacob Bramley, Ruben Ayrapetyan,
	Chintan Pandya, Luc Van Oostenryck, Dave Martin, Kevin Brodsky,
	Szabolcs Nagy

On 2/22/19 4:53 AM, Andrey Konovalov wrote:
> --- a/fs/namespace.c
> +++ b/fs/namespace.c
> @@ -2730,7 +2730,7 @@ void *copy_mount_options(const void __user * data)
>  	 * the remainder of the page.
>  	 */
>  	/* copy_from_user cannot cross TASK_SIZE ! */
> -	size = TASK_SIZE - (unsigned long)data;
> +	size = TASK_SIZE - (unsigned long)untagged_addr(data);
>  	if (size > PAGE_SIZE)
>  		size = PAGE_SIZE;

I would have thought that copy_from_user() *is* entirely capable of
detecting and returning an error in the case that its arguments cross
TASK_SIZE.  It will fail and return an error, but that's what it's
supposed to do.

I'd question why this code needs to be doing its own checking in the
first place.  Is there something subtle going on?

^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [PATCH v10 07/12] fs, arm64: untag user pointers in fs/userfaultfd.c
  2019-02-22 12:53 ` [PATCH v10 07/12] fs, arm64: untag user pointers in fs/userfaultfd.c Andrey Konovalov
@ 2019-02-22 23:05   ` Dave Hansen
  2019-02-26 14:39     ` Andrey Konovalov
  0 siblings, 1 reply; 30+ messages in thread
From: Dave Hansen @ 2019-02-22 23:05 UTC (permalink / raw)
  To: Andrey Konovalov, Catalin Marinas, Will Deacon, Mark Rutland,
	Robin Murphy, Kees Cook, Kate Stewart, Greg Kroah-Hartman,
	Andrew Morton, Ingo Molnar, Kirill A . Shutemov, Shuah Khan,
	Vincenzo Frascino, linux-arm-kernel, linux-doc, linux-mm,
	linux-arch, linux-kselftest, linux-kernel
  Cc: Dmitry Vyukov, Kostya Serebryany, Evgeniy Stepanov, Lee Smith,
	Ramana Radhakrishnan, Jacob Bramley, Ruben Ayrapetyan,
	Chintan Pandya, Luc Van Oostenryck, Dave Martin, Kevin Brodsky,
	Szabolcs Nagy

On 2/22/19 4:53 AM, Andrey Konovalov wrote:
> userfaultfd_register() and userfaultfd_unregister() use provided user
> pointers for vma lookups, which can only by done with untagged pointers.

So, we have to patch all these sites before the tagged values get to the
point of hitting the vma lookup functions.  Dumb question: Why don't we
just patch the vma lookup functions themselves instead of all of these
callers?

^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [PATCH v10 04/12] mm, arm64: untag user pointers passed to memory syscalls
  2019-02-22 12:53 ` [PATCH v10 04/12] mm, arm64: untag user pointers passed to memory syscalls Andrey Konovalov
@ 2019-02-22 23:07   ` Dave Hansen
  2019-02-26 14:41     ` Andrey Konovalov
  0 siblings, 1 reply; 30+ messages in thread
From: Dave Hansen @ 2019-02-22 23:07 UTC (permalink / raw)
  To: Andrey Konovalov, Catalin Marinas, Will Deacon, Mark Rutland,
	Robin Murphy, Kees Cook, Kate Stewart, Greg Kroah-Hartman,
	Andrew Morton, Ingo Molnar, Kirill A . Shutemov, Shuah Khan,
	Vincenzo Frascino, linux-arm-kernel, linux-doc, linux-mm,
	linux-arch, linux-kselftest, linux-kernel
  Cc: Dmitry Vyukov, Kostya Serebryany, Evgeniy Stepanov, Lee Smith,
	Ramana Radhakrishnan, Jacob Bramley, Ruben Ayrapetyan,
	Chintan Pandya, Luc Van Oostenryck, Dave Martin, Kevin Brodsky,
	Szabolcs Nagy

On 2/22/19 4:53 AM, Andrey Konovalov wrote:
> --- a/mm/mprotect.c
> +++ b/mm/mprotect.c
> @@ -578,6 +578,7 @@ static int do_mprotect_pkey(unsigned long start, size_t len,
>  SYSCALL_DEFINE3(mprotect, unsigned long, start, size_t, len,
>  		unsigned long, prot)
>  {
> +	start = untagged_addr(start);
>  	return do_mprotect_pkey(start, len, prot, -1);
>  }
>  
> @@ -586,6 +587,7 @@ SYSCALL_DEFINE3(mprotect, unsigned long, start, size_t, len,
>  SYSCALL_DEFINE4(pkey_mprotect, unsigned long, start, size_t, len,
>  		unsigned long, prot, int, pkey)
>  {
> +	start = untagged_addr(start);
>  	return do_mprotect_pkey(start, len, prot, pkey);
>  }

This seems to have taken the approach of going as close as possible to
the syscall boundary and untagging the pointer there.  I guess that's
OK, but it does lead to more churn than necessary.  For instance, why
not just do the untagging in do_mprotect_pkey()?

I think that's an overall design question.  I kinda asked the same thing
about patching call sites vs. VMA lookup functions.

^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [PATCH v10 06/12] fs, arm64: untag user pointers in copy_mount_options
  2019-02-22 23:03   ` Dave Hansen
@ 2019-02-26 14:35     ` Andrey Konovalov
  0 siblings, 0 replies; 30+ messages in thread
From: Andrey Konovalov @ 2019-02-26 14:35 UTC (permalink / raw)
  To: Dave Hansen
  Cc: Catalin Marinas, Will Deacon, Mark Rutland, Robin Murphy,
	Kees Cook, Kate Stewart, Greg Kroah-Hartman, Andrew Morton,
	Ingo Molnar, Kirill A . Shutemov, Shuah Khan, Vincenzo Frascino,
	Linux ARM, open list:DOCUMENTATION, Linux Memory Management List,
	linux-arch, open list:KERNEL SELFTEST FRAMEWORK, LKML,
	Dmitry Vyukov, Kostya Serebryany, Evgeniy Stepanov, Lee Smith,
	Ramana Radhakrishnan, Jacob Bramley, Ruben Ayrapetyan,
	Chintan Pandya, Luc Van Oostenryck, Dave Martin, Kevin Brodsky,
	Szabolcs Nagy

On Sat, Feb 23, 2019 at 12:03 AM Dave Hansen <dave.hansen@intel.com> wrote:
>
> On 2/22/19 4:53 AM, Andrey Konovalov wrote:
> > --- a/fs/namespace.c
> > +++ b/fs/namespace.c
> > @@ -2730,7 +2730,7 @@ void *copy_mount_options(const void __user * data)
> >        * the remainder of the page.
> >        */
> >       /* copy_from_user cannot cross TASK_SIZE ! */
> > -     size = TASK_SIZE - (unsigned long)data;
> > +     size = TASK_SIZE - (unsigned long)untagged_addr(data);
> >       if (size > PAGE_SIZE)
> >               size = PAGE_SIZE;
>
> I would have thought that copy_from_user() *is* entirely capable of
> detecting and returning an error in the case that its arguments cross
> TASK_SIZE.  It will fail and return an error, but that's what it's
> supposed to do.
>
> I'd question why this code needs to be doing its own checking in the
> first place.  Is there something subtle going on?

The comment above exact_copy_from_user() states:

Some copy_from_user() implementations do not return the exact number of
bytes remaining to copy on a fault.  But copy_mount_options() requires that.
Note that this function differs from copy_from_user() in that it will oops
on bad values of `to', rather than returning a short copy.

^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [PATCH v10 07/12] fs, arm64: untag user pointers in fs/userfaultfd.c
  2019-02-22 23:05   ` Dave Hansen
@ 2019-02-26 14:39     ` Andrey Konovalov
  2019-03-01 16:59       ` Catalin Marinas
  0 siblings, 1 reply; 30+ messages in thread
From: Andrey Konovalov @ 2019-02-26 14:39 UTC (permalink / raw)
  To: Dave Hansen
  Cc: Catalin Marinas, Will Deacon, Mark Rutland, Robin Murphy,
	Kees Cook, Kate Stewart, Greg Kroah-Hartman, Andrew Morton,
	Ingo Molnar, Kirill A . Shutemov, Shuah Khan, Vincenzo Frascino,
	Linux ARM, open list:DOCUMENTATION, Linux Memory Management List,
	linux-arch, open list:KERNEL SELFTEST FRAMEWORK, LKML,
	Dmitry Vyukov, Kostya Serebryany, Evgeniy Stepanov, Lee Smith,
	Ramana Radhakrishnan, Jacob Bramley, Ruben Ayrapetyan,
	Chintan Pandya, Luc Van Oostenryck, Dave Martin, Kevin Brodsky,
	Szabolcs Nagy

On Sat, Feb 23, 2019 at 12:06 AM Dave Hansen <dave.hansen@intel.com> wrote:
>
> On 2/22/19 4:53 AM, Andrey Konovalov wrote:
> > userfaultfd_register() and userfaultfd_unregister() use provided user
> > pointers for vma lookups, which can only by done with untagged pointers.
>
> So, we have to patch all these sites before the tagged values get to the
> point of hitting the vma lookup functions.  Dumb question: Why don't we
> just patch the vma lookup functions themselves instead of all of these
> callers?

That might be a working approach as well. We'll still need to fix up
places where the vma fields are accessed directly. Catalin, what do
you think?

^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [PATCH v10 04/12] mm, arm64: untag user pointers passed to memory syscalls
  2019-02-22 23:07   ` Dave Hansen
@ 2019-02-26 14:41     ` Andrey Konovalov
  0 siblings, 0 replies; 30+ messages in thread
From: Andrey Konovalov @ 2019-02-26 14:41 UTC (permalink / raw)
  To: Dave Hansen
  Cc: Catalin Marinas, Will Deacon, Mark Rutland, Robin Murphy,
	Kees Cook, Kate Stewart, Greg Kroah-Hartman, Andrew Morton,
	Ingo Molnar, Kirill A . Shutemov, Shuah Khan, Vincenzo Frascino,
	Linux ARM, open list:DOCUMENTATION, Linux Memory Management List,
	linux-arch, open list:KERNEL SELFTEST FRAMEWORK, LKML,
	Dmitry Vyukov, Kostya Serebryany, Evgeniy Stepanov, Lee Smith,
	Ramana Radhakrishnan, Jacob Bramley, Ruben Ayrapetyan,
	Chintan Pandya, Luc Van Oostenryck, Dave Martin, Kevin Brodsky,
	Szabolcs Nagy

On Sat, Feb 23, 2019 at 12:07 AM Dave Hansen <dave.hansen@intel.com> wrote:
>
> On 2/22/19 4:53 AM, Andrey Konovalov wrote:
> > --- a/mm/mprotect.c
> > +++ b/mm/mprotect.c
> > @@ -578,6 +578,7 @@ static int do_mprotect_pkey(unsigned long start, size_t len,
> >  SYSCALL_DEFINE3(mprotect, unsigned long, start, size_t, len,
> >               unsigned long, prot)
> >  {
> > +     start = untagged_addr(start);
> >       return do_mprotect_pkey(start, len, prot, -1);
> >  }
> >
> > @@ -586,6 +587,7 @@ SYSCALL_DEFINE3(mprotect, unsigned long, start, size_t, len,
> >  SYSCALL_DEFINE4(pkey_mprotect, unsigned long, start, size_t, len,
> >               unsigned long, prot, int, pkey)
> >  {
> > +     start = untagged_addr(start);
> >       return do_mprotect_pkey(start, len, prot, pkey);
> >  }
>
> This seems to have taken the approach of going as close as possible to
> the syscall boundary and untagging the pointer there.  I guess that's
> OK, but it does lead to more churn than necessary.  For instance, why
> not just do the untagging in do_mprotect_pkey()?

I think that makes more sense, will do in the next version, thanks!

>
> I think that's an overall design question.  I kinda asked the same thing
> about patching call sites vs. VMA lookup functions.

Replied in the other thread.

^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [PATCH v10 00/12] arm64: untag user pointers passed to the kernel
  2019-02-22 16:10     ` Szabolcs Nagy
@ 2019-02-26 17:00       ` Andrey Konovalov
  0 siblings, 0 replies; 30+ messages in thread
From: Andrey Konovalov @ 2019-02-26 17:00 UTC (permalink / raw)
  To: Szabolcs Nagy
  Cc: nd, Catalin Marinas, Will Deacon, Mark Rutland, Robin Murphy,
	Kees Cook, Kate Stewart, Greg Kroah-Hartman, Andrew Morton,
	Ingo Molnar, Kirill A . Shutemov, Shuah Khan, Vincenzo Frascino,
	linux-arm-kernel, linux-doc, linux-mm, linux-arch,
	linux-kselftest, linux-kernel, Dmitry Vyukov, Kostya Serebryany,
	Evgeniy Stepanov, Lee Smith, Ramana Radhakrishnan, Jacob Bramley,
	Ruben Ayrapetyan, Chintan Pandya, Luc Van Oostenryck,
	Dave P Martin, Kevin Brodsky

On Fri, Feb 22, 2019 at 5:10 PM Szabolcs Nagy <Szabolcs.Nagy@arm.com> wrote:
>
> On 22/02/2019 15:40, Andrey Konovalov wrote:
> > On Fri, Feb 22, 2019 at 4:35 PM Szabolcs Nagy <Szabolcs.Nagy@arm.com> wrote:
> >>
> >> On 22/02/2019 12:53, Andrey Konovalov wrote:
> >>> This patchset is meant to be merged together with "arm64 relaxed ABI" [1].
> >>>
> >>> arm64 has a feature called Top Byte Ignore, which allows to embed pointer
> >>> tags into the top byte of each pointer. Userspace programs (such as
> >>> HWASan, a memory debugging tool [2]) might use this feature and pass
> >>> tagged user pointers to the kernel through syscalls or other interfaces.
> >>>
> >>> Right now the kernel is already able to handle user faults with tagged
> >>> pointers, due to these patches:
> >>>
> >>> 1. 81cddd65 ("arm64: traps: fix userspace cache maintenance emulation on a
> >>>              tagged pointer")
> >>> 2. 7dcd9dd8 ("arm64: hw_breakpoint: fix watchpoint matching for tagged
> >>>             pointers")
> >>> 3. 276e9327 ("arm64: entry: improve data abort handling of tagged
> >>>             pointers")
> >>>
> >>> This patchset extends tagged pointer support to syscall arguments.
> >>>
> >>> For non-memory syscalls this is done by untaging user pointers when the
> >>> kernel performs pointer checking to find out whether the pointer comes
> >>> from userspace (most notably in access_ok). The untagging is done only
> >>> when the pointer is being checked, the tag is preserved as the pointer
> >>> makes its way through the kernel.
> >>>
> >>> Since memory syscalls (mmap, mprotect, etc.) don't do memory accesses but
> >>> rather deal with memory ranges, untagged pointers are better suited to
> >>> describe memory ranges internally. Thus for memory syscalls we untag
> >>> pointers completely when they enter the kernel.
> >>
> >> i think the same is true when user pointers are compared.
> >>
> >> e.g. i suspect there may be issues with tagged robust mutex
> >> list pointers because the kernel does
> >>
> >> futex.c:3541:   while (entry != &head->list) {
> >>
> >> where entry is a user pointer that may be tagged, and
> >> &head->list is probably not tagged.
> >
> > You're right. I'll expand the cover letter in the next version to
> > describe this more accurately. The patchset however contains "mm,
> > arm64: untag user pointers in mm/gup.c" that should take care of futex
> > pointers.
>
> the robust mutex list pointer is not a futex pointer,
> i'm not sure how the mm/gup.c patch helps.

Oh, I've misinterpreted what you said, sorry.

I've looked at the robust futex list implementation, and I'm not sure
if we need to add untagging here.

> >> futex.c:3541:   while (entry != &head->list) {

Here head has whatever value user has set via the set_robust_list
syscall and it might be tagged. AFAIU this loop iterates over the
robust list stored in userspace, until it encounters the head pointer
again, at which point the kernel decides that it has iterated over the
whole list and stops. The question is whether we want the user to use
the same tag for the pointer that is passed to the set_robust_list
syscall and the pointer that is used to mark the end of the robust
list.

Catalin, what do you think?

>
> >>
> >>> One of the alternative approaches to untagging that was considered is to
> >>> completely strip the pointer tag as the pointer enters the kernel with
> >>> some kind of a syscall wrapper, but that won't work with the countless
> >>> number of different ioctl calls. With this approach we would need a custom
> >>> wrapper for each ioctl variation, which doesn't seem practical.
> >>>
> >>> The following testing approaches has been taken to find potential issues
> >>> with user pointer untagging:
> >>>
> >>> 1. Static testing (with sparse [3] and separately with a custom static
> >>>    analyzer based on Clang) to track casts of __user pointers to integer
> >>>    types to find places where untagging needs to be done.
> >>>
> >>> 2. Static testing with grep to find parts of the kernel that call
> >>>    find_vma() (and other similar functions) or directly compare against
> >>>    vm_start/vm_end fields of vma.
> >>>
> >>> 3. Static testing with grep to find parts of the kernel that compare
> >>>    user pointers with TASK_SIZE or other similar consts and macros.
> >>>
> >>> 4. Dynamic testing: adding BUG_ON(has_tag(addr)) to find_vma() and running
> >>>    a modified syzkaller version that passes tagged pointers to the kernel.
> >>>
> >>> Based on the results of the testing the requried patches have been added
> >>> to the patchset.
> >>>
> >>> This patchset has been merged into the Pixel 2 kernel tree and is now
> >>> being used to enable testing of Pixel 2 phones with HWASan.
> >>>
> >>> This patchset is a prerequisite for ARM's memory tagging hardware feature
> >>> support [4].
> >>>
> >>> Thanks!
> >>>
> >>> [1] https://lkml.org/lkml/2018/12/10/402
> >>>
> >>> [2] http://clang.llvm.org/docs/HardwareAssistedAddressSanitizerDesign.html
> >>>
> >>> [3] https://github.com/lucvoo/sparse-dev/commit/5f960cb10f56ec2017c128ef9d16060e0145f292
> >>>
> >>> [4] https://community.arm.com/processors/b/blog/posts/arm-a-profile-architecture-2018-developments-armv85a
> >>>
> >>> Changes in v10:
> >>> - Added "mm, arm64: untag user pointers passed to memory syscalls" back.
> >>> - New patch "fs, arm64: untag user pointers in fs/userfaultfd.c".
> >>> - New patch "net, arm64: untag user pointers in tcp_zerocopy_receive".
> >>> - New patch "kernel, arm64: untag user pointers in prctl_set_mm*".
> >>> - New patch "tracing, arm64: untag user pointers in seq_print_user_ip".
> >>>
> >>> Changes in v9:
> >>> - Rebased onto 4.20-rc6.
> >>> - Used u64 instead of __u64 in type casts in the untagged_addr macro for
> >>>   arm64.
> >>> - Added braces around (addr) in the untagged_addr macro for other arches.
> >>>
> >>> Changes in v8:
> >>> - Rebased onto 65102238 (4.20-rc1).
> >>> - Added a note to the cover letter on why syscall wrappers/shims that untag
> >>>   user pointers won't work.
> >>> - Added a note to the cover letter that this patchset has been merged into
> >>>   the Pixel 2 kernel tree.
> >>> - Documentation fixes, in particular added a list of syscalls that don't
> >>>   support tagged user pointers.
> >>>
> >>> Changes in v7:
> >>> - Rebased onto 17b57b18 (4.19-rc6).
> >>> - Dropped the "arm64: untag user address in __do_user_fault" patch, since
> >>>   the existing patches already handle user faults properly.
> >>> - Dropped the "usb, arm64: untag user addresses in devio" patch, since the
> >>>   passed pointer must come from a vma and therefore be untagged.
> >>> - Dropped the "arm64: annotate user pointers casts detected by sparse"
> >>>   patch (see the discussion to the replies of the v6 of this patchset).
> >>> - Added more context to the cover letter.
> >>> - Updated Documentation/arm64/tagged-pointers.txt.
> >>>
> >>> Changes in v6:
> >>> - Added annotations for user pointer casts found by sparse.
> >>> - Rebased onto 050cdc6c (4.19-rc1+).
> >>>
> >>> Changes in v5:
> >>> - Added 3 new patches that add untagging to places found with static
> >>>   analysis.
> >>> - Rebased onto 44c929e1 (4.18-rc8).
> >>>
> >>> Changes in v4:
> >>> - Added a selftest for checking that passing tagged pointers to the
> >>>   kernel succeeds.
> >>> - Rebased onto 81e97f013 (4.18-rc1+).
> >>>
> >>> Changes in v3:
> >>> - Rebased onto e5c51f30 (4.17-rc6+).
> >>> - Added linux-arch@ to the list of recipients.
> >>>
> >>> Changes in v2:
> >>> - Rebased onto 2d618bdf (4.17-rc3+).
> >>> - Removed excessive untagging in gup.c.
> >>> - Removed untagging pointers returned from __uaccess_mask_ptr.
> >>>
> >>> Changes in v1:
> >>> - Rebased onto 4.17-rc1.
> >>>
> >>> Changes in RFC v2:
> >>> - Added "#ifndef untagged_addr..." fallback in linux/uaccess.h instead of
> >>>   defining it for each arch individually.
> >>> - Updated Documentation/arm64/tagged-pointers.txt.
> >>> - Dropped "mm, arm64: untag user addresses in memory syscalls".
> >>> - Rebased onto 3eb2ce82 (4.16-rc7).
> >>>
> >>> Reviewed-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
> >>> Signed-off-by: Andrey Konovalov <andreyknvl@google.com>
> >>>
> >>> Andrey Konovalov (12):
> >>>   uaccess: add untagged_addr definition for other arches
> >>>   arm64: untag user pointers in access_ok and __uaccess_mask_ptr
> >>>   lib, arm64: untag user pointers in strn*_user
> >>>   mm, arm64: untag user pointers passed to memory syscalls
> >>>   mm, arm64: untag user pointers in mm/gup.c
> >>>   fs, arm64: untag user pointers in copy_mount_options
> >>>   fs, arm64: untag user pointers in fs/userfaultfd.c
> >>>   net, arm64: untag user pointers in tcp_zerocopy_receive
> >>>   kernel, arm64: untag user pointers in prctl_set_mm*
> >>>   tracing, arm64: untag user pointers in seq_print_user_ip
> >>>   arm64: update Documentation/arm64/tagged-pointers.txt
> >>>   selftests, arm64: add a selftest for passing tagged pointers to kernel
> >>>
> >>>  Documentation/arm64/tagged-pointers.txt       | 25 +++++++++++--------
> >>>  arch/arm64/include/asm/uaccess.h              | 10 +++++---
> >>>  fs/namespace.c                                |  2 +-
> >>>  fs/userfaultfd.c                              |  5 ++++
> >>>  include/linux/memory.h                        |  4 +++
> >>>  ipc/shm.c                                     |  2 ++
> >>>  kernel/sys.c                                  | 14 +++++++++++
> >>>  kernel/trace/trace_output.c                   |  2 +-
> >>>  lib/strncpy_from_user.c                       |  2 ++
> >>>  lib/strnlen_user.c                            |  2 ++
> >>>  mm/gup.c                                      |  4 +++
> >>>  mm/madvise.c                                  |  2 ++
> >>>  mm/mempolicy.c                                |  5 ++++
> >>>  mm/migrate.c                                  |  1 +
> >>>  mm/mincore.c                                  |  2 ++
> >>>  mm/mlock.c                                    |  5 ++++
> >>>  mm/mmap.c                                     |  7 ++++++
> >>>  mm/mprotect.c                                 |  2 ++
> >>>  mm/mremap.c                                   |  2 ++
> >>>  mm/msync.c                                    |  2 ++
> >>>  net/ipv4/tcp.c                                |  2 ++
> >>>  tools/testing/selftests/arm64/.gitignore      |  1 +
> >>>  tools/testing/selftests/arm64/Makefile        | 11 ++++++++
> >>>  .../testing/selftests/arm64/run_tags_test.sh  | 12 +++++++++
> >>>  tools/testing/selftests/arm64/tags_test.c     | 19 ++++++++++++++
> >>>  25 files changed, 129 insertions(+), 16 deletions(-)
> >>>  create mode 100644 tools/testing/selftests/arm64/.gitignore
> >>>  create mode 100644 tools/testing/selftests/arm64/Makefile
> >>>  create mode 100755 tools/testing/selftests/arm64/run_tags_test.sh
> >>>  create mode 100644 tools/testing/selftests/arm64/tags_test.c
> >>>
> >>
>

^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [PATCH v10 00/12] arm64: untag user pointers passed to the kernel
  2019-02-22 22:54 ` Dave Hansen
@ 2019-02-26 17:18   ` Andrey Konovalov
  2019-02-26 17:35     ` Dave Hansen
  2019-02-26 23:17     ` Luc Van Oostenryck
  0 siblings, 2 replies; 30+ messages in thread
From: Andrey Konovalov @ 2019-02-26 17:18 UTC (permalink / raw)
  To: Dave Hansen
  Cc: Catalin Marinas, Will Deacon, Mark Rutland, Robin Murphy,
	Kees Cook, Kate Stewart, Greg Kroah-Hartman, Andrew Morton,
	Ingo Molnar, Kirill A . Shutemov, Shuah Khan, Vincenzo Frascino,
	Linux ARM, open list:DOCUMENTATION, Linux Memory Management List,
	linux-arch, open list:KERNEL SELFTEST FRAMEWORK, LKML,
	Dmitry Vyukov, Kostya Serebryany, Evgeniy Stepanov, Lee Smith,
	Ramana Radhakrishnan, Jacob Bramley, Ruben Ayrapetyan,
	Chintan Pandya, Luc Van Oostenryck, Dave Martin, Kevin Brodsky,
	Szabolcs Nagy

On Fri, Feb 22, 2019 at 11:55 PM Dave Hansen <dave.hansen@intel.com> wrote:
>
> On 2/22/19 4:53 AM, Andrey Konovalov wrote:
> > The following testing approaches has been taken to find potential issues
> > with user pointer untagging:
> >
> > 1. Static testing (with sparse [3] and separately with a custom static
> >    analyzer based on Clang) to track casts of __user pointers to integer
> >    types to find places where untagging needs to be done.
>
> First of all, it's really cool that you took this approach.  Sounds like
> there was a lot of systematic work to fix up the sites in the existing
> codebase.
>
> But, isn't this a _bit_ fragile going forward?  Folks can't just "make
> sparse" to find issues with missing untags.

Yes, this static approach can only be used as a hint to find some
places where untagging is needed, but certainly not all.

> This seems like something
> where we would ideally add an __tagged annotation (or something) to the
> source tree and then have sparse rules that can look for missed untags.

This has been suggested before, search for __untagged here [1].
However there are many places in the kernel where a __user pointer is
casted into unsigned long and passed further. I'm not sure if it's
possible apply a __tagged/__untagged kind of attribute to non-pointer
types, is it?

[1] https://patchwork.kernel.org/patch/10581535/

^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [PATCH v10 00/12] arm64: untag user pointers passed to the kernel
  2019-02-26 17:18   ` Andrey Konovalov
@ 2019-02-26 17:35     ` Dave Hansen
  2019-02-26 23:17     ` Luc Van Oostenryck
  1 sibling, 0 replies; 30+ messages in thread
From: Dave Hansen @ 2019-02-26 17:35 UTC (permalink / raw)
  To: Andrey Konovalov
  Cc: Catalin Marinas, Will Deacon, Mark Rutland, Robin Murphy,
	Kees Cook, Kate Stewart, Greg Kroah-Hartman, Andrew Morton,
	Ingo Molnar, Kirill A . Shutemov, Shuah Khan, Vincenzo Frascino,
	Linux ARM, open list:DOCUMENTATION, Linux Memory Management List,
	linux-arch, open list:KERNEL SELFTEST FRAMEWORK, LKML,
	Dmitry Vyukov, Kostya Serebryany, Evgeniy Stepanov, Lee Smith,
	Ramana Radhakrishnan, Jacob Bramley, Ruben Ayrapetyan,
	Chintan Pandya, Luc Van Oostenryck, Dave Martin, Kevin Brodsky,
	Szabolcs Nagy

On 2/26/19 9:18 AM, Andrey Konovalov wrote:
>> This seems like something
>> where we would ideally add an __tagged annotation (or something) to the
>> source tree and then have sparse rules that can look for missed untags.
> This has been suggested before, search for __untagged here [1].
> However there are many places in the kernel where a __user pointer is
> casted into unsigned long and passed further. I'm not sure if it's
> possible apply a __tagged/__untagged kind of attribute to non-pointer
> types, is it?
> 
> [1] https://patchwork.kernel.org/patch/10581535/

I believe we have sparse checking __GFP_* flags.  We also have a gfp_t
for them and I'm unsure whether the sparse support is tied to _that_ or
whether it's just by tagging the type itself as being part of a discrete
address space.

^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [PATCH v10 00/12] arm64: untag user pointers passed to the kernel
  2019-02-26 17:18   ` Andrey Konovalov
  2019-02-26 17:35     ` Dave Hansen
@ 2019-02-26 23:17     ` Luc Van Oostenryck
  1 sibling, 0 replies; 30+ messages in thread
From: Luc Van Oostenryck @ 2019-02-26 23:17 UTC (permalink / raw)
  To: Andrey Konovalov
  Cc: Dave Hansen, Catalin Marinas, Will Deacon, Mark Rutland,
	Robin Murphy, Kees Cook, Kate Stewart, Greg Kroah-Hartman,
	Andrew Morton, Ingo Molnar, Kirill A . Shutemov, Shuah Khan,
	Vincenzo Frascino, Linux ARM, open list:DOCUMENTATION,
	Linux Memory Management List, linux-arch,
	open list:KERNEL SELFTEST FRAMEWORK, LKML, Dmitry Vyukov,
	Kostya Serebryany, Evgeniy Stepanov, Lee Smith,
	Ramana Radhakrishnan, Jacob Bramley, Ruben Ayrapetyan,
	Chintan Pandya, Dave Martin, Kevin Brodsky, Szabolcs Nagy

On Tue, Feb 26, 2019 at 06:18:25PM +0100, Andrey Konovalov wrote:
> On Fri, Feb 22, 2019 at 11:55 PM Dave Hansen <dave.hansen@intel.com> wrote:
> >
> > On 2/22/19 4:53 AM, Andrey Konovalov wrote:
> > > The following testing approaches has been taken to find potential issues
> > > with user pointer untagging:
> > >
> > > 1. Static testing (with sparse [3] and separately with a custom static
> > >    analyzer based on Clang) to track casts of __user pointers to integer
> > >    types to find places where untagging needs to be done.
> >
> > First of all, it's really cool that you took this approach.  Sounds like
> > there was a lot of systematic work to fix up the sites in the existing
> > codebase.
> >
> > But, isn't this a _bit_ fragile going forward?  Folks can't just "make
> > sparse" to find issues with missing untags.
> 
> Yes, this static approach can only be used as a hint to find some
> places where untagging is needed, but certainly not all.
> 
> > This seems like something
> > where we would ideally add an __tagged annotation (or something) to the
> > source tree and then have sparse rules that can look for missed untags.
> 
> This has been suggested before, search for __untagged here [1].
> However there are many places in the kernel where a __user pointer is
> casted into unsigned long and passed further. I'm not sure if it's
> possible apply a __tagged/__untagged kind of attribute to non-pointer
> types, is it?
> 
> [1] https://patchwork.kernel.org/patch/10581535/

It's something that should need to be added to sparse since it's
different from what sparse already have (the existing __bitwise and
concept of address-space doesn't seem to do the job here).

-- Luc Van Oostenryck

^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [PATCH v10 07/12] fs, arm64: untag user pointers in fs/userfaultfd.c
  2019-02-26 14:39     ` Andrey Konovalov
@ 2019-03-01 16:59       ` Catalin Marinas
  2019-03-01 18:37         ` Dave Hansen
  0 siblings, 1 reply; 30+ messages in thread
From: Catalin Marinas @ 2019-03-01 16:59 UTC (permalink / raw)
  To: Andrey Konovalov
  Cc: Dave Hansen, Will Deacon, Mark Rutland, Robin Murphy, Kees Cook,
	Kate Stewart, Greg Kroah-Hartman, Andrew Morton, Ingo Molnar,
	Kirill A . Shutemov, Shuah Khan, Vincenzo Frascino, Linux ARM,
	open list:DOCUMENTATION, Linux Memory Management List,
	linux-arch, open list:KERNEL SELFTEST FRAMEWORK, LKML,
	Dmitry Vyukov, Kostya Serebryany, Evgeniy Stepanov, Lee Smith,
	Ramana Radhakrishnan, Jacob Bramley, Ruben Ayrapetyan,
	Chintan Pandya, Luc Van Oostenryck, Dave Martin, Kevin Brodsky,
	Szabolcs Nagy

On Tue, Feb 26, 2019 at 03:39:08PM +0100, Andrey Konovalov wrote:
> On Sat, Feb 23, 2019 at 12:06 AM Dave Hansen <dave.hansen@intel.com> wrote:
> >
> > On 2/22/19 4:53 AM, Andrey Konovalov wrote:
> > > userfaultfd_register() and userfaultfd_unregister() use provided user
> > > pointers for vma lookups, which can only by done with untagged pointers.
> >
> > So, we have to patch all these sites before the tagged values get to the
> > point of hitting the vma lookup functions.  Dumb question: Why don't we
> > just patch the vma lookup functions themselves instead of all of these
> > callers?
> 
> That might be a working approach as well. We'll still need to fix up
> places where the vma fields are accessed directly. Catalin, what do
> you think?

Most callers of find_vma*() always follow it by a check of
vma->vma_start against some tagged address ('end' in the
userfaultfd_(un)register()) case. So it's not sufficient to untag it in
find_vma().

-- 
Catalin

^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [PATCH v10 07/12] fs, arm64: untag user pointers in fs/userfaultfd.c
  2019-03-01 16:59       ` Catalin Marinas
@ 2019-03-01 18:37         ` Dave Hansen
  2019-03-05 17:47           ` Andrey Konovalov
  0 siblings, 1 reply; 30+ messages in thread
From: Dave Hansen @ 2019-03-01 18:37 UTC (permalink / raw)
  To: Catalin Marinas, Andrey Konovalov
  Cc: Will Deacon, Mark Rutland, Robin Murphy, Kees Cook, Kate Stewart,
	Greg Kroah-Hartman, Andrew Morton, Ingo Molnar,
	Kirill A . Shutemov, Shuah Khan, Vincenzo Frascino, Linux ARM,
	open list:DOCUMENTATION, Linux Memory Management List,
	linux-arch, open list:KERNEL SELFTEST FRAMEWORK, LKML,
	Dmitry Vyukov, Kostya Serebryany, Evgeniy Stepanov, Lee Smith,
	Ramana Radhakrishnan, Jacob Bramley, Ruben Ayrapetyan,
	Chintan Pandya, Luc Van Oostenryck, Dave Martin, Kevin Brodsky,
	Szabolcs Nagy

On 3/1/19 8:59 AM, Catalin Marinas wrote:
>>> So, we have to patch all these sites before the tagged values get to the
>>> point of hitting the vma lookup functions.  Dumb question: Why don't we
>>> just patch the vma lookup functions themselves instead of all of these
>>> callers?
>> That might be a working approach as well. We'll still need to fix up
>> places where the vma fields are accessed directly. Catalin, what do
>> you think?
> Most callers of find_vma*() always follow it by a check of
> vma->vma_start against some tagged address ('end' in the
> userfaultfd_(un)register()) case. So it's not sufficient to untag it in
> find_vma().

If that's truly the common case, sounds like we should have a find_vma()
that does the vma_end checking as well.  Then at least the common case
would not have to worry about tagging.

^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [PATCH v10 07/12] fs, arm64: untag user pointers in fs/userfaultfd.c
  2019-03-01 18:37         ` Dave Hansen
@ 2019-03-05 17:47           ` Andrey Konovalov
  0 siblings, 0 replies; 30+ messages in thread
From: Andrey Konovalov @ 2019-03-05 17:47 UTC (permalink / raw)
  To: Dave Hansen
  Cc: Catalin Marinas, Will Deacon, Mark Rutland, Robin Murphy,
	Kees Cook, Kate Stewart, Greg Kroah-Hartman, Andrew Morton,
	Ingo Molnar, Kirill A . Shutemov, Shuah Khan, Vincenzo Frascino,
	Linux ARM, open list:DOCUMENTATION, Linux Memory Management List,
	linux-arch, open list:KERNEL SELFTEST FRAMEWORK, LKML,
	Dmitry Vyukov, Kostya Serebryany, Evgeniy Stepanov, Lee Smith,
	Ramana Radhakrishnan, Jacob Bramley, Ruben Ayrapetyan,
	Chintan Pandya, Luc Van Oostenryck, Dave Martin, Kevin Brodsky,
	Szabolcs Nagy

On Fri, Mar 1, 2019 at 7:37 PM Dave Hansen <dave.hansen@intel.com> wrote:
>
> On 3/1/19 8:59 AM, Catalin Marinas wrote:
> >>> So, we have to patch all these sites before the tagged values get to the
> >>> point of hitting the vma lookup functions.  Dumb question: Why don't we
> >>> just patch the vma lookup functions themselves instead of all of these
> >>> callers?
> >> That might be a working approach as well. We'll still need to fix up
> >> places where the vma fields are accessed directly. Catalin, what do
> >> you think?
> > Most callers of find_vma*() always follow it by a check of
> > vma->vma_start against some tagged address ('end' in the
> > userfaultfd_(un)register()) case. So it's not sufficient to untag it in
> > find_vma().
>
> If that's truly the common case, sounds like we should have a find_vma()
> that does the vma_end checking as well.  Then at least the common case
> would not have to worry about tagging.

It seems that a lot of find_vma() callers indeed do different kinds of
checking/subtractions of vma->vma_start and a tagged address, which
look hardly unifiable. So untagging the addresses in find_vma()
callers looks like a more suitable solution.

^ permalink raw reply	[flat|nested] 30+ messages in thread

end of thread, other threads:[~2019-03-05 17:47 UTC | newest]

Thread overview: 30+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-02-22 12:53 [PATCH v10 00/12] arm64: untag user pointers passed to the kernel Andrey Konovalov
2019-02-22 12:53 ` [PATCH v10 01/12] uaccess: add untagged_addr definition for other arches Andrey Konovalov
2019-02-22 12:53 ` [PATCH v10 02/12] arm64: untag user pointers in access_ok and __uaccess_mask_ptr Andrey Konovalov
2019-02-22 12:53 ` [PATCH v10 03/12] lib, arm64: untag user pointers in strn*_user Andrey Konovalov
2019-02-22 12:53 ` [PATCH v10 04/12] mm, arm64: untag user pointers passed to memory syscalls Andrey Konovalov
2019-02-22 23:07   ` Dave Hansen
2019-02-26 14:41     ` Andrey Konovalov
2019-02-22 12:53 ` [PATCH v10 05/12] mm, arm64: untag user pointers in mm/gup.c Andrey Konovalov
2019-02-22 12:53 ` [PATCH v10 06/12] fs, arm64: untag user pointers in copy_mount_options Andrey Konovalov
2019-02-22 23:03   ` Dave Hansen
2019-02-26 14:35     ` Andrey Konovalov
2019-02-22 12:53 ` [PATCH v10 07/12] fs, arm64: untag user pointers in fs/userfaultfd.c Andrey Konovalov
2019-02-22 23:05   ` Dave Hansen
2019-02-26 14:39     ` Andrey Konovalov
2019-03-01 16:59       ` Catalin Marinas
2019-03-01 18:37         ` Dave Hansen
2019-03-05 17:47           ` Andrey Konovalov
2019-02-22 12:53 ` [PATCH v10 08/12] net, arm64: untag user pointers in tcp_zerocopy_receive Andrey Konovalov
2019-02-22 12:53 ` [PATCH v10 09/12] kernel, arm64: untag user pointers in prctl_set_mm* Andrey Konovalov
2019-02-22 12:53 ` [PATCH v10 10/12] tracing, arm64: untag user pointers in seq_print_user_ip Andrey Konovalov
2019-02-22 12:53 ` [PATCH v10 11/12] arm64: update Documentation/arm64/tagged-pointers.txt Andrey Konovalov
2019-02-22 12:53 ` [PATCH v10 12/12] selftests, arm64: add a selftest for passing tagged pointers to kernel Andrey Konovalov
2019-02-22 15:35 ` [PATCH v10 00/12] arm64: untag user pointers passed to the kernel Szabolcs Nagy
2019-02-22 15:40   ` Andrey Konovalov
2019-02-22 16:10     ` Szabolcs Nagy
2019-02-26 17:00       ` Andrey Konovalov
2019-02-22 22:54 ` Dave Hansen
2019-02-26 17:18   ` Andrey Konovalov
2019-02-26 17:35     ` Dave Hansen
2019-02-26 23:17     ` Luc Van Oostenryck

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).