Skip to contentSkip to conversation

arm64: untag user pointers passed to the kernel

PATCH Superseded Andrey Konovalov 9 participants 72 messages Jun 3, 2019 – Jun 12, 2019

Subsystems (20)

v16 of 19 Superseded Latest is v19 →

lore
Share & tools
lore
#0 lore raw

v16 cover arm64: untag user pointers passed to the kernel

=== Overview

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 [1]) 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.

As per the proposed ABI change [3], tagged pointers are only allowed to be passed to syscalls when they point to memory ranges obtained by anonymous mmap() or sbrk() (see the patchset [3] for more details).

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 and stays tagged when the kernel dereferences the pointer when perfoming user memory accesses.

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

=== Other approaches

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.

An alternative approach to untagging pointers in memory syscalls prologues is to inspead allow tagged pointers to be passed to find_vma() (and other vma related functions) and untag them there. Unfortunately, a lot of find_vma() callers then compare or subtract the returned vma start and end fields against the pointer that was being searched. Thus this approach would still require changing all find_vma() callers.

=== Testing

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

1. Static testing (with sparse [2] 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.

=== Notes

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

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

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

Thanks!

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

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

[3] https://lkml.org/lkml/2019/3/18/819

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

Changes in v16:
- Moved untagging for memory syscalls from arm64 wrappers back to generic code.
- Dropped untagging for the following memory syscalls: brk, mmap, munmap; mremap (only dropped for new_address); mmap_pgoff (not used on arm64); remap_file_pages (deprecated); shmat, shmdt (work on shared memory).
- Changed kselftest to LD_PRELOAD a shared library that overrides malloc to return tagged pointers.
- Rebased onto 5.2-rc3.

Changes in v15:
- Removed unnecessary untagging from radeon_ttm_tt_set_userptr().
- Removed unnecessary untagging from amdgpu_ttm_tt_set_userptr().
- Moved untagging to validate_range() in userfaultfd code.
- Moved untagging to ib_uverbs_(re)reg_mr() from mlx4_get_umem_mr().
- Rebased onto 5.1.

Changes in v14:
- Moved untagging for most memory syscalls to an arm64 specific implementation, instead of doing that in the common code.
- Dropped "net, arm64: untag user pointers in tcp_zerocopy_receive", since the provided user pointers don't come from an anonymous map and thus are not covered by this ABI relaxation.
- Dropped "kernel, arm64: untag user pointers in prctl_set_mm*".
- Moved untagging from __check_mem_type() to tee_shm_register().
- Updated untagging for the amdgpu and radeon drivers to cover the MMU notifier, as suggested by Felix.
- Since this ABI relaxation doesn't actually allow tagged instruction pointers, dropped the following patches:
- Dropped "tracing, arm64: untag user pointers in seq_print_user_ip".
- Dropped "uprobes, arm64: untag user pointers in find_active_uprobe".
- Dropped "bpf, arm64: untag user pointers in stack_map_get_build_id_offset".
- Rebased onto 5.1-rc7 (37624b58).

Changes in v13:
- Simplified untagging in tcp_zerocopy_receive().
- Looked at find_vma() callers in drivers/, which allowed to identify a few other places where untagging is needed.
- Added patch "mm, arm64: untag user pointers in get_vaddr_frames".
- Added patch "drm/amdgpu, arm64: untag user pointers in amdgpu_ttm_tt_get_user_pages".
- Added patch "drm/radeon, arm64: untag user pointers in radeon_ttm_tt_pin_userptr".
- Added patch "IB/mlx4, arm64: untag user pointers in mlx4_get_umem_mr".
- Added patch "media/v4l2-core, arm64: untag user pointers in videobuf_dma_contig_user_get".
- Added patch "tee/optee, arm64: untag user pointers in check_mem_type".
- Added patch "vfio/type1, arm64: untag user pointers".

Changes in v12:
- Changed untagging in tcp_zerocopy_receive() to also untag zc->address.
- Fixed untagging in prctl_set_mm* to only untag pointers for vma lookups and validity checks, but leave them as is for actual user space accesses.
- Updated the link to the v2 of the "arm64 relaxed ABI" patchset [3].
- Dropped the documentation patch, as the "arm64 relaxed ABI" patchset [3] handles that.

Changes in v11:
- Added "uprobes, arm64: untag user pointers in find_active_uprobe" patch.
- Added "bpf, arm64: untag user pointers in stack_map_get_build_id_offset" patch.
- Fixed "tracing, arm64: untag user pointers in seq_print_user_ip" to correctly perform subtration with a tagged addr.
- Moved untagged_addr() from SYSCALL_DEFINE3(mprotect) and SYSCALL_DEFINE4(pkey_mprotect) to do_mprotect_pkey().
- Moved untagged_addr() definition for other arches from include/linux/memory.h to include/linux/mm.h.
- Changed untagging in strn*_user() to perform userspace accesses through tagged pointers.
- Updated the documentation to mention that passing tagged pointers to memory syscalls is allowed.
- Updated the test to use malloc'ed memory instead of stack memory.

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).

Andrey Konovalov (16):
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: untag user pointers in do_pages_move
arm64: untag user pointers passed to memory syscalls
mm, arm64: untag user pointers in mm/gup.c
mm, arm64: untag user pointers in get_vaddr_frames
fs, arm64: untag user pointers in copy_mount_options
fs, arm64: untag user pointers in fs/userfaultfd.c drm/amdgpu, arm64: untag user pointers
drm/radeon, arm64: untag user pointers in radeon_gem_userptr_ioctl IB, arm64: untag user pointers in ib_uverbs_(re)reg_mr() media/v4l2-core, arm64: untag user pointers in
videobuf_dma_contig_user_get
tee, arm64: untag user pointers in tee_shm_register vfio/type1, arm64: untag user pointers in vaddr_get_pfn selftests, arm64: add a selftest for passing tagged pointers to kernel

 arch/arm64/include/asm/uaccess.h              | 10 +++--
 .../gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c  |  2 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c       |  2 +
 drivers/gpu/drm/radeon/radeon_gem.c           |  2 +
 drivers/infiniband/core/uverbs_cmd.c          |  4 ++
 drivers/media/v4l2-core/videobuf-dma-contig.c |  9 ++--
 drivers/tee/tee_shm.c                         |  1 +
 drivers/vfio/vfio_iommu_type1.c               |  2 +
 fs/namespace.c                                |  2 +-
 fs/userfaultfd.c                              | 22 +++++-----
 include/linux/mm.h                            |  4 ++
 lib/strncpy_from_user.c                       |  3 +-
 lib/strnlen_user.c                            |  3 +-
 mm/frame_vector.c                             |  2 +
 mm/gup.c                                      |  4 ++
 mm/madvise.c                                  |  2 +
 mm/mempolicy.c                                |  3 ++
 mm/migrate.c                                  |  1 +
 mm/mincore.c                                  |  2 +
 mm/mlock.c                                    |  4 ++
 mm/mprotect.c                                 |  2 +
 mm/mremap.c                                   |  2 +
 mm/msync.c                                    |  2 +
 tools/testing/selftests/arm64/.gitignore      |  1 +
 tools/testing/selftests/arm64/Makefile        | 22 ++++++++++
 .../testing/selftests/arm64/run_tags_test.sh  | 12 ++++++
 tools/testing/selftests/arm64/tags_lib.c      | 42 +++++++++++++++++++
 tools/testing/selftests/arm64/tags_test.c     | 18 ++++++++
 28 files changed, 163 insertions(+), 22 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_lib.c
 create mode 100644 tools/testing/selftests/arm64/tags_test.c
Signature · 1 line
2.22.0.rc1.311.g5d7573a151-goog
#1 lore raw

PATCH v16 1/16 uaccess: add untagged_addr definition for other arches

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.

---
 include/linux/mm.h | 4 ++++
 1 file changed, 4 insertions(+)
include/linux/mm.h +4 -0
index 0e8834ac32b7..949d43e9c0b6 100644
@@ -99,6 +99,10 @@ extern int mmap_rnd_compat_bits __read_mostly;
 #include <asm/pgtable.h>
 #include <asm/processor.h>
 
+#ifndef untagged_addr
+#define untagged_addr(addr) (addr)
+#endif
+
 #ifndef __pa_symbol
 #define __pa_symbol(x)  __pa(RELOC_HIDE((unsigned long)(x), 0))
 #endif
Signature · 1 line
2.22.0.rc1.311.g5d7573a151-goog
#2 lore raw

Re: [PATCH v16 01/16] uaccess: add untagged_addr definition for other arches

Quoted 20 lines · 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.

---
 include/linux/mm.h | 4 ++++
 1 file changed, 4 insertions(+)
include/linux/mm.h +0 -0
index 0e8834ac32b7..949d43e9c0b6 100644

include/linux/mm.h extern int mmap_rnd_compat_bits __read_mostly; · line 108

Quoted 10 lines
include/linux/mm.h +4 -0
@@ -99,6 +99,10 @@ extern int mmap_rnd_compat_bits __read_mostly;
 #include <asm/pgtable.h>
 #include <asm/processor.h>
 
+#ifndef untagged_addr
+#define untagged_addr(addr) (addr)
+#endif
+
 #ifndef __pa_symbol
 #define __pa_symbol(x)  __pa(RELOC_HIDE((unsigned long)(x), 0))
 #endif

Andrey,

This patch has now become part of the other patch series Chris Hellwig has sent out -
<https://lore.kernel.org/lkml/[email protected]/>. Can you coordinate with that patch series?

--
Khalid

#3 lore raw

Re: [PATCH v16 01/16] uaccess: add untagged_addr definition for other arches

Quoted 35 lines · Khalid Aziz
Quoted 20 lines · 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.

---
 include/linux/mm.h | 4 ++++
 1 file changed, 4 insertions(+)
include/linux/mm.h +0 -0
index 0e8834ac32b7..949d43e9c0b6 100644

include/linux/mm.h extern int mmap_rnd_compat_bits __read_mostly; · line 108

Quoted 10 lines
include/linux/mm.h +4 -0
@@ -99,6 +99,10 @@ extern int mmap_rnd_compat_bits __read_mostly;
 #include <asm/pgtable.h>
 #include <asm/processor.h>
 
+#ifndef untagged_addr
+#define untagged_addr(addr) (addr)
+#endif
+
 #ifndef __pa_symbol
 #define __pa_symbol(x)  __pa(RELOC_HIDE((unsigned long)(x), 0))
 #endif

Andrey,

Quoted 4 lines

This patch has now become part of the other patch series Chris Hellwig has sent out -
<https://lore.kernel.org/lkml/[email protected]/>. Can you coordinate with that patch series?

Hi!

Yes, I've seen it. How should I coordinate? Rebase this series on top of that one?

Thanks!

Quoted 4 lines

--
Khalid

#4 lore raw

Re: [PATCH v16 01/16] uaccess: add untagged_addr definition for other arches

Quoted 8 lines · Andrey Konovalov
Quoted 6 lines · Khalid Aziz

Andrey,

This patch has now become part of the other patch series Chris Hellwig has sent out -
<https://lore.kernel.org/lkml/[email protected]/>. Can you coordinate with that patch series?

Quoted 4 lines

Hi!

Yes, I've seen it. How should I coordinate? Rebase this series on top of that one?

That would be one way to do it. Better yet, separate this patch from both patch series, make it standalone and then rebase the two patch series on top of it.

--
Khalid

#5 lore raw

Re: [PATCH v16 01/16] uaccess: add untagged_addr definition for other arches

Quoted 9 lines · Khalid Aziz
Quoted 8 lines · Andrey Konovalov
Quoted 6 lines · Khalid Aziz

Andrey,

This patch has now become part of the other patch series Chris Hellwig has sent out -
<https://lore.kernel.org/lkml/[email protected]/>. Can you coordinate with that patch series?

Quoted 8 lines
Quoted 4 lines

Hi!

Yes, I've seen it. How should I coordinate? Rebase this series on top of that one?

That would be one way to do it. Better yet, separate this patch from both patch series, make it standalone and then rebase the two patch series on top of it.

I think easiest would be to just ask Linus if he could make an exception and include this trivial prep patch in 5.2-rc.

#6 lore raw

Re: [PATCH v16 01/16] uaccess: add untagged_addr definition for other arches

Quoted 15 lines · Christoph Hellwig
Quoted 14 lines · Khalid Aziz
Quoted 12 lines · Andrey Konovalov
Quoted 6 lines · Khalid Aziz

Andrey,

This patch has now become part of the other patch series Chris Hellwig has sent out -
<https://lore.kernel.org/lkml/[email protected]/>. Can you coordinate with that patch series?

Hi!

Yes, I've seen it. How should I coordinate? Rebase this series on top of that one?

Quoted 7 lines
Quoted 3 lines

That would be one way to do it. Better yet, separate this patch from both patch series, make it standalone and then rebase the two patch series on top of it.

I think easiest would be to just ask Linus if he could make an exception and include this trivial prep patch in 5.2-rc.

Andrey,

Would you mind updating the commit log to make it not arm64 specific and sending this patch out by itself. We can then ask Linus if he can include just this patch in the next rc.

Thanks,
Khalid

#7 lore raw

Re: [PATCH v16 01/16] uaccess: add untagged_addr definition for other arches

Quoted 25 lines · Khalid Aziz
Quoted 15 lines · Christoph Hellwig
Quoted 14 lines · Khalid Aziz
Quoted 12 lines · Andrey Konovalov
Quoted 6 lines · Khalid Aziz

Andrey,

This patch has now become part of the other patch series Chris Hellwig has sent out -
<https://lore.kernel.org/lkml/[email protected]/>. Can you coordinate with that patch series?

Hi!

Yes, I've seen it. How should I coordinate? Rebase this series on top of that one?

Quoted 7 lines
Quoted 3 lines

That would be one way to do it. Better yet, separate this patch from both patch series, make it standalone and then rebase the two patch series on top of it.

I think easiest would be to just ask Linus if he could make an exception and include this trivial prep patch in 5.2-rc.

Quoted 5 lines

Andrey,

Would you mind updating the commit log to make it not arm64 specific and sending this patch out by itself. We can then ask Linus if he can include just this patch in the next rc.

Sure! Just sent it out.

Quoted 4 lines

Thanks,
Khalid

#8 lore raw

PATCH v16 2/16 arm64: untag user pointers in access_ok and __uaccess_mask_ptr

In reply to Andrey Konovalov

This patch is a part of a series that extends arm64 kernel ABI to allow to pass tagged user pointers (with the top byte set to something else other than 0x00) as syscall arguments.

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.

---
 arch/arm64/include/asm/uaccess.h | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
arch/arm64/include/asm/uaccess.h +6 -4
index e5d5f31c6d36..9164ecb5feca 100644
@@ -94,7 +94,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)						\
@@ -226,7 +226,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)
@@ -234,10 +235,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();
Signature · 1 line
2.22.0.rc1.311.g5d7573a151-goog
Reviewed-by on v16 2/16
#9 lore raw

Re: [PATCH v16 02/16] arm64: untag user pointers in access_ok and __uaccess_mask_ptr

Quoted 9 lines · Andrey Konovalov

This patch is a part of a series that extends arm64 kernel ABI to allow to pass tagged user pointers (with the top byte set to something else other than 0x00) as syscall arguments.

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.

Quoted 8 lines

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.

-Kees

Quoted 44 lines
---
 arch/arm64/include/asm/uaccess.h | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
arch/arm64/include/asm/uaccess.h +6 -4
index e5d5f31c6d36..9164ecb5feca 100644
@@ -94,7 +94,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)						\
@@ -226,7 +226,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)
@@ -234,10 +235,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();
Signature · 1 line
2.22.0.rc1.311.g5d7573a151-goog
Signature · 1 line
Kees Cook
maintainer
#10 lore raw

Re: [PATCH v16 02/16] arm64: untag user pointers in access_ok and __uaccess_mask_ptr

In reply to Andrey Konovalov

arch/arm64/include/asm/uaccess.h static inline unsigned long __range_ok(const void __user *addr, unsigned long si · line 97

Quoted 10 lines · Andrey Konovalov
arch/arm64/include/asm/uaccess.h +1 -1
index e5d5f31c6d36..9164ecb5feca 100644
@@ -94,7 +94,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)

I'm going to propose an opt-in method here (RFC for now). We can't have a check in untagged_addr() since this is already used throughout the kernel for both user and kernel addresses (khwasan) but we can add one in __range_ok(). The same prctl() option will be used for controlling the precise/imprecise mode of MTE later on. We can use a TIF_ flag here assuming that this will be called early on and any cloned thread will inherit this.

Anyway, it's easier to paste some diff than explain but Vincenzo can fold them into his ABI patches that should really go together with these. I added a couple of MTE definitions for prctl() as an example, not used currently:

------------------8<---------------------------------------------

arch/arm64/include/asm/processor.h +4 -0
index fcd0e691b1ea..2d4cb7e4edab 100644
@@ -307,6 +307,10 @@ extern void __init minsigstksz_setup(void);
 /* PR_PAC_RESET_KEYS prctl */
 #define PAC_RESET_KEYS(tsk, arg)	ptrauth_prctl_reset_keys(tsk, arg)
 
+/* PR_UNTAGGED_UADDR prctl */
+int untagged_uaddr_set_mode(unsigned long arg);
+#define SET_UNTAGGED_UADDR_MODE(arg)	untagged_uaddr_set_mode(arg)
+
 /*
  * For CONFIG_GCC_PLUGIN_STACKLEAK
  *
arch/arm64/include/asm/thread_info.h +2 -0
index c285d1ce7186..89ce77773c49 100644
@@ -101,6 +101,7 @@ void arch_release_task_struct(struct task_struct *tsk);
 #define TIF_SVE			23	/* Scalable Vector Extension in use */
 #define TIF_SVE_VL_INHERIT	24	/* Inherit sve_vl_onexec across exec */
 #define TIF_SSBD		25	/* Wants SSB mitigation */
+#define TIF_UNTAGGED_UADDR	26
 
 #define _TIF_SIGPENDING		(1 << TIF_SIGPENDING)
 #define _TIF_NEED_RESCHED	(1 << TIF_NEED_RESCHED)
@@ -116,6 +117,7 @@ void arch_release_task_struct(struct task_struct *tsk);
 #define _TIF_FSCHECK		(1 << TIF_FSCHECK)
 #define _TIF_32BIT		(1 << TIF_32BIT)
 #define _TIF_SVE		(1 << TIF_SVE)
+#define _TIF_UNTAGGED_UADDR	(1 << TIF_UNTAGGED_UADDR)
 
 #define _TIF_WORK_MASK		(_TIF_NEED_RESCHED | _TIF_SIGPENDING | \
 				 _TIF_NOTIFY_RESUME | _TIF_FOREIGN_FPSTATE | \
arch/arm64/include/asm/uaccess.h +4 -1
index 9164ecb5feca..54f5bbaebbc4 100644
@@ -73,6 +73,9 @@ static inline unsigned long __range_ok(const void __user *addr, unsigned long si
 {
 	unsigned long ret, limit = current_thread_info()->addr_limit;
 
+	if (test_thread_flag(TIF_UNTAGGED_UADDR))
+		addr = untagged_addr(addr);
+
 	__chk_user_ptr(addr);
 	asm volatile(
 	// A + B <= C + 1 for all A,B,C, in four easy steps:
@@ -94,7 +97,7 @@ static inline unsigned long __range_ok(const void __user *addr, unsigned long si
 	return ret;
 }
 
-#define access_ok(addr, size)	__range_ok(untagged_addr(addr), size)
+#define access_ok(addr, size)	__range_ok(addr, size)
 #define user_addr_max			get_fs
 
 #define _ASM_EXTABLE(from, to)						\
arch/arm64/kernel/process.c +15 -0
index 3767fb21a5b8..fd191c5b92aa 100644
@@ -552,3 +552,18 @@ void arch_setup_new_exec(void)
 
 	ptrauth_thread_init_user(current);
 }
+
+/*
+ * Enable the relaxed ABI allowing tagged user addresses into the kernel.
+ */
+int untagged_uaddr_set_mode(unsigned long arg)
+{
+	if (is_compat_task())
+		return -ENOTSUPP;
+	if (arg)
+		return -EINVAL;
+
+	set_thread_flag(TIF_UNTAGGED_UADDR);
+
+	return 0;
+}
include/uapi/linux/prctl.h +5 -0
index 094bb03b9cc2..4afd5e2980ee 100644
@@ -229,4 +229,9 @@ struct prctl_mm_map {
 # define PR_PAC_APDBKEY			(1UL << 3)
 # define PR_PAC_APGAKEY			(1UL << 4)
 
+/* Untagged user addresses for arm64 */
+#define PR_UNTAGGED_UADDR		55
+# define PR_MTE_IMPRECISE_CHECK		0
+# define PR_MTE_PRECISE_CHECK		1
+
 #endif /* _LINUX_PRCTL_H */
kernel/sys.c +8 -0
index 2969304c29fe..b1f67a8cffc4 100644
@@ -124,6 +124,9 @@
 #ifndef PAC_RESET_KEYS
 # define PAC_RESET_KEYS(a, b)	(-EINVAL)
 #endif
+#ifndef SET_UNTAGGED_UADDR_MODE
+# define SET_UNTAGGED_UADDR_MODE	(-EINVAL)
+#endif
 
 /*
  * this is where the system-wide overflow UID and GID are defined, for
@@ -2492,6 +2495,11 @@ SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3,
 			return -EINVAL;
 		error = PAC_RESET_KEYS(me, arg2);
 		break;
+	case PR_UNTAGGED_UADDR:
+		if (arg3 || arg4 || arg5)
+			return -EINVAL;
+		error = SET_UNTAGGED_UADDR_MODE(arg2);
+		break;
 	default:
 		error = -EINVAL;
 		break;

------------------8<---------------------------------------------

The tag_ptr() function in the test library would become:

static void *tag_ptr(void *ptr)

{
	static int tbi_enabled = 0;
	unsigned long tag = 0;

	if (!tbi_enabled) {
		if (prctl(PR_UNTAGGED_UADDR, 0, 0, 0, 0) == 0)
			tbi_enabled = 1;
	}

	if (!ptr)
		return ptr;
	if (tbi_enabled)
		tag = rand() & 0xff;

	return (void *)((unsigned long)ptr | (tag << TAG_SHIFT));
}
Signature · 1 line
Catalin
#11 lore raw

Re: [PATCH v16 02/16] arm64: untag user pointers in access_ok and __uaccess_mask_ptr

Quoted 97 lines · Catalin Marinas

arch/arm64/include/asm/uaccess.h static inline unsigned long __range_ok(const void __user *addr, unsigned long si · line 97

Quoted 10 lines · Andrey Konovalov
arch/arm64/include/asm/uaccess.h +1 -1
index e5d5f31c6d36..9164ecb5feca 100644
@@ -94,7 +94,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)

I'm going to propose an opt-in method here (RFC for now). We can't have a check in untagged_addr() since this is already used throughout the kernel for both user and kernel addresses (khwasan) but we can add one in __range_ok(). The same prctl() option will be used for controlling the precise/imprecise mode of MTE later on. We can use a TIF_ flag here assuming that this will be called early on and any cloned thread will inherit this.

Anyway, it's easier to paste some diff than explain but Vincenzo can fold them into his ABI patches that should really go together with these. I added a couple of MTE definitions for prctl() as an example, not used currently:

------------------8<---------------------------------------------

arch/arm64/include/asm/processor.h +4 -0
index fcd0e691b1ea..2d4cb7e4edab 100644
@@ -307,6 +307,10 @@ extern void __init minsigstksz_setup(void);
 /* PR_PAC_RESET_KEYS prctl */
 #define PAC_RESET_KEYS(tsk, arg)	ptrauth_prctl_reset_keys(tsk, arg)
 
+/* PR_UNTAGGED_UADDR prctl */
+int untagged_uaddr_set_mode(unsigned long arg);
+#define SET_UNTAGGED_UADDR_MODE(arg)	untagged_uaddr_set_mode(arg)
+
 /*
  * For CONFIG_GCC_PLUGIN_STACKLEAK
  *
arch/arm64/include/asm/thread_info.h +2 -0
index c285d1ce7186..89ce77773c49 100644
@@ -101,6 +101,7 @@ void arch_release_task_struct(struct task_struct *tsk);
 #define TIF_SVE			23	/* Scalable Vector Extension in use */
 #define TIF_SVE_VL_INHERIT	24	/* Inherit sve_vl_onexec across exec */
 #define TIF_SSBD		25	/* Wants SSB mitigation */
+#define TIF_UNTAGGED_UADDR	26
 
 #define _TIF_SIGPENDING		(1 << TIF_SIGPENDING)
 #define _TIF_NEED_RESCHED	(1 << TIF_NEED_RESCHED)
@@ -116,6 +117,7 @@ void arch_release_task_struct(struct task_struct *tsk);
 #define _TIF_FSCHECK		(1 << TIF_FSCHECK)
 #define _TIF_32BIT		(1 << TIF_32BIT)
 #define _TIF_SVE		(1 << TIF_SVE)
+#define _TIF_UNTAGGED_UADDR	(1 << TIF_UNTAGGED_UADDR)
 
 #define _TIF_WORK_MASK		(_TIF_NEED_RESCHED | _TIF_SIGPENDING | \
 				 _TIF_NOTIFY_RESUME | _TIF_FOREIGN_FPSTATE | \
arch/arm64/include/asm/uaccess.h +4 -1
index 9164ecb5feca..54f5bbaebbc4 100644
@@ -73,6 +73,9 @@ static inline unsigned long __range_ok(const void __user *addr, unsigned long si
 {
 	unsigned long ret, limit = current_thread_info()->addr_limit;
 
+	if (test_thread_flag(TIF_UNTAGGED_UADDR))
+		addr = untagged_addr(addr);
+
 	__chk_user_ptr(addr);
 	asm volatile(
 	// A + B <= C + 1 for all A,B,C, in four easy steps:
@@ -94,7 +97,7 @@ static inline unsigned long __range_ok(const void __user *addr, unsigned long si
 	return ret;
 }
 
-#define access_ok(addr, size)	__range_ok(untagged_addr(addr), size)
+#define access_ok(addr, size)	__range_ok(addr, size)
 #define user_addr_max			get_fs
 
 #define _ASM_EXTABLE(from, to)						\
arch/arm64/kernel/process.c +5 -0
index 3767fb21a5b8..fd191c5b92aa 100644
@@ -552,3 +552,18 @@ void arch_setup_new_exec(void)
 
 	ptrauth_thread_init_user(current);
 }
+
+/*
+ * Enable the relaxed ABI allowing tagged user addresses into the kernel.
+ */
+int untagged_uaddr_set_mode(unsigned long arg)

arch/arm64/kernel/process.c void arch_setup_new_exec(void) · line 569

Quoted 10 lines
arch/arm64/kernel/process.c +10 -0
+{
+	if (is_compat_task())
+		return -ENOTSUPP;
+	if (arg)
+		return -EINVAL;
+
+	set_thread_flag(TIF_UNTAGGED_UADDR);
+
+	return 0;
+}

I think this should be paired with a flag clearing in copy_thread(), yes? (i.e. each binary needs to opt in)

Signature · 1 line
Kees Cook
maintainer
#12 lore raw

Re: [PATCH v16 02/16] arm64: untag user pointers in access_ok and __uaccess_mask_ptr

Quoted 12 lines · Kees Cook
Quoted 11 lines · Catalin Marinas
Quoted 10 lines · Andrey Konovalov
arch/arm64/include/asm/uaccess.h +1 -1
index e5d5f31c6d36..9164ecb5feca 100644
@@ -94,7 +94,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)

[...]

Quoted 18 lines
Quoted 18 lines
arch/arm64/kernel/process.c +10 -0
index 3767fb21a5b8..fd191c5b92aa 100644
@@ -552,3 +552,18 @@ void arch_setup_new_exec(void)
 
 	ptrauth_thread_init_user(current);
 }
+
+/*
+ * Enable the relaxed ABI allowing tagged user addresses into the kernel.
+ */
+int untagged_uaddr_set_mode(unsigned long arg)
+{
+	if (is_compat_task())
+		return -ENOTSUPP;
+	if (arg)
+		return -EINVAL;
Quoted 8 lines

arch/arm64/kernel/process.c void arch_setup_new_exec(void) · line 569

Quoted 5 lines
arch/arm64/kernel/process.c +5 -0
+
+	set_thread_flag(TIF_UNTAGGED_UADDR);
+
+	return 0;
+}

I think this should be paired with a flag clearing in copy_thread(), yes? (i.e. each binary needs to opt in)

It indeed needs clearing though not in copy_thread() as that's used on clone/fork but rather in flush_thread(), called on the execve() path.

And a note to myself: I think PR_UNTAGGED_ADDR (not UADDR) looks better in a uapi header, the user doesn't differentiate between uaddr and kaddr.

Signature · 1 line
Catalin
#13 lore raw

Re: [PATCH v16 02/16] arm64: untag user pointers in access_ok and __uaccess_mask_ptr

Quoted 29 lines · Catalin Marinas
Quoted 19 lines · Kees Cook
Quoted 18 lines · Catalin Marinas
arch/arm64/kernel/process.c +10 -0
index 3767fb21a5b8..fd191c5b92aa 100644
@@ -552,3 +552,18 @@ void arch_setup_new_exec(void)
 
 	ptrauth_thread_init_user(current);
 }
+
+/*
+ * Enable the relaxed ABI allowing tagged user addresses into the kernel.
+ */
+int untagged_uaddr_set_mode(unsigned long arg)
+{
+	if (is_compat_task())
+		return -ENOTSUPP;
+	if (arg)
+		return -EINVAL;
Quoted 8 lines

arch/arm64/kernel/process.c void arch_setup_new_exec(void) · line 569

Quoted 5 lines
arch/arm64/kernel/process.c +5 -0
+
+	set_thread_flag(TIF_UNTAGGED_UADDR);
+
+	return 0;
+}

I think this should be paired with a flag clearing in copy_thread(), yes? (i.e. each binary needs to opt in)

Quoted 2 lines

It indeed needs clearing though not in copy_thread() as that's used on clone/fork but rather in flush_thread(), called on the execve() path.

Ah! Yes, thanks.

Quoted 3 lines

And a note to myself: I think PR_UNTAGGED_ADDR (not UADDR) looks better in a uapi header, the user doesn't differentiate between uaddr and kaddr.

Good point. I would agree. :)

Signature · 1 line
Kees Cook
maintainer
#14 lore raw

Re: [PATCH v16 02/16] arm64: untag user pointers in access_ok and __uaccess_mask_ptr

In reply to Catalin Marinas
Quoted 9 lines · Catalin Marinas
Quoted 8 lines · Andrey Konovalov
arch/arm64/include/asm/uaccess.h +0 -0
index e5d5f31c6d36..9164ecb5feca 100644
@@ -94,7 +94,7 @@ static inline unsigned long __range_ok(const void __user *addr, unsigned long si
 	return ret;
 }
 
Quoted 10 lines

arch/arm64/include/asm/uaccess.h static inline unsigned long __range_ok(const void __user *addr, unsigned long si · line 97

Quoted 2 lines
arch/arm64/include/asm/uaccess.h +1 -1
-#define access_ok(addr, size)	__range_ok(addr, size)
+#define access_ok(addr, size)	__range_ok(untagged_addr(addr), size)

I'm going to propose an opt-in method here (RFC for now). We can't have a check in untagged_addr() since this is already used throughout the kernel for both user and kernel addresses (khwasan) but we can add one in __range_ok(). The same prctl() option will be used for controlling the precise/imprecise mode of MTE later on. We can use a TIF_ flag here assuming that this will be called early on and any cloned thread will inherit this.

Updated patch, inlining it below. Once we agreed on the approach, I think Andrey can insert in in this series, probably after patch 2. The differences from the one I posted yesterday:

- renamed PR_* macros together with get/set variants and the possibility to disable the relaxed ABI

- sysctl option - /proc/sys/abi/tagged_addr to disable the ABI globally (just the prctl() opt-in, tasks already using it won't be affected)

And, of course, it needs more testing.

---------8<----------------

Quoted 1 line

From 7c624777a4e545522dec1b34e60f0229cb2bd59f Mon Sep 17 00:00:00 2001

From: Catalin Marinas <[email protected]>
Date: Tue, 11 Jun 2019 13:03:38 +0100
Subject: [PATCH] arm64: Introduce prctl() options to control the tagged user
addresses ABI

It is not desirable to relax the ABI to allow tagged user addresses into the kernel indiscriminately. This patch introduces a prctl() interface for enabling or disabling the tagged ABI with a global sysctl control for preventing applications from enabling the relaxed ABI (meant for testing user-space prctl() return error checking without reconfiguring the kernel). The ABI properties are inherited by threads of the same application and fork()'ed children but cleared on execve().

The PR_SET_TAGGED_ADDR_CTRL will be expanded in the future to handle MTE-specific settings like imprecise vs precise exceptions.

---
 arch/arm64/include/asm/processor.h   |  6 +++
 arch/arm64/include/asm/thread_info.h |  1 +
 arch/arm64/include/asm/uaccess.h     |  5 ++-
 arch/arm64/kernel/process.c          | 67 ++++++++++++++++++++++++++++
 include/uapi/linux/prctl.h           |  5 +++
 kernel/sys.c                         | 16 +++++++
 6 files changed, 99 insertions(+), 1 deletion(-)
arch/arm64/include/asm/processor.h +6 -0
index fcd0e691b1ea..fee457456aa8 100644
@@ -307,6 +307,12 @@ extern void __init minsigstksz_setup(void);
 /* PR_PAC_RESET_KEYS prctl */
 #define PAC_RESET_KEYS(tsk, arg)	ptrauth_prctl_reset_keys(tsk, arg)
 
+/* PR_TAGGED_ADDR prctl */
+long set_tagged_addr_ctrl(unsigned long arg);
+long get_tagged_addr_ctrl(void);
+#define SET_TAGGED_ADDR_CTRL(arg)	set_tagged_addr_ctrl(arg)
+#define GET_TAGGED_ADDR_CTRL()		get_tagged_addr_ctrl()
+
 /*
  * For CONFIG_GCC_PLUGIN_STACKLEAK
  *
arch/arm64/include/asm/thread_info.h +1 -0
index c285d1ce7186..7263d4c973ce 100644
@@ -101,6 +101,7 @@ void arch_release_task_struct(struct task_struct *tsk);
 #define TIF_SVE			23	/* Scalable Vector Extension in use */
 #define TIF_SVE_VL_INHERIT	24	/* Inherit sve_vl_onexec across exec */
 #define TIF_SSBD		25	/* Wants SSB mitigation */
+#define TIF_TAGGED_ADDR		26
 
 #define _TIF_SIGPENDING		(1 << TIF_SIGPENDING)
 #define _TIF_NEED_RESCHED	(1 << TIF_NEED_RESCHED)
arch/arm64/include/asm/uaccess.h +4 -1
index 9164ecb5feca..995b9ea11a89 100644
@@ -73,6 +73,9 @@ static inline unsigned long __range_ok(const void __user *addr, unsigned long si
 {
 	unsigned long ret, limit = current_thread_info()->addr_limit;
 
+	if (test_thread_flag(TIF_TAGGED_ADDR))
+		addr = untagged_addr(addr);
+
 	__chk_user_ptr(addr);
 	asm volatile(
 	// A + B <= C + 1 for all A,B,C, in four easy steps:
@@ -94,7 +97,7 @@ static inline unsigned long __range_ok(const void __user *addr, unsigned long si
 	return ret;
 }
 
-#define access_ok(addr, size)	__range_ok(untagged_addr(addr), size)
+#define access_ok(addr, size)	__range_ok(addr, size)
 #define user_addr_max			get_fs
 
 #define _ASM_EXTABLE(from, to)						\
arch/arm64/kernel/process.c +67 -0

82 lines not on this page · raw message

include/uapi/linux/prctl.h +5 -0
index 094bb03b9cc2..2e927b3e9d6c 100644
@@ -229,4 +229,9 @@ struct prctl_mm_map {
 # define PR_PAC_APDBKEY			(1UL << 3)
 # define PR_PAC_APGAKEY			(1UL << 4)
 
+/* Tagged user address controls for arm64 */
+#define PR_SET_TAGGED_ADDR_CTRL		55
+#define PR_GET_TAGGED_ADDR_CTRL		56
+# define PR_TAGGED_ADDR_ENABLE		(1UL << 0)
+
 #endif /* _LINUX_PRCTL_H */
kernel/sys.c +16 -0
index 2969304c29fe..ec48396b4943 100644
@@ -124,6 +124,12 @@
 #ifndef PAC_RESET_KEYS
 # define PAC_RESET_KEYS(a, b)	(-EINVAL)
 #endif
+#ifndef SET_TAGGED_ADDR_CTRL
+# define SET_TAGGED_ADDR_CTRL(a)	(-EINVAL)
+#endif
+#ifndef GET_TAGGED_ADDR_CTRL
+# define GET_TAGGED_ADDR_CTRL()		(-EINVAL)
+#endif
 
 /*
  * this is where the system-wide overflow UID and GID are defined, for
@@ -2492,6 +2498,16 @@ SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3,
 			return -EINVAL;
 		error = PAC_RESET_KEYS(me, arg2);
 		break;
+	case PR_SET_TAGGED_ADDR_CTRL:
+		if (arg3 || arg4 || arg5)
+			return -EINVAL;
+		error = SET_TAGGED_ADDR_CTRL(arg2);
+		break;
+	case PR_GET_TAGGED_ADDR_CTRL:
+		if (arg2 || arg3 || arg4 || arg5)
+			return -EINVAL;
+		error = GET_TAGGED_ADDR_CTRL();
+		break;
 	default:
 		error = -EINVAL;
 		break;
#15 lore raw

Re: [PATCH v16 02/16] arm64: untag user pointers in access_ok and __uaccess_mask_ptr

Hi Catalin,

...

Quoted 50 lines

---------8<----------------
From 7c624777a4e545522dec1b34e60f0229cb2bd59f Mon Sep 17 00:00:00 2001
From: Catalin Marinas <[email protected]>
Date: Tue, 11 Jun 2019 13:03:38 +0100
Subject: [PATCH] arm64: Introduce prctl() options to control the tagged user
addresses ABI

It is not desirable to relax the ABI to allow tagged user addresses into the kernel indiscriminately. This patch introduces a prctl() interface for enabling or disabling the tagged ABI with a global sysctl control for preventing applications from enabling the relaxed ABI (meant for testing user-space prctl() return error checking without reconfiguring the kernel). The ABI properties are inherited by threads of the same application and fork()'ed children but cleared on execve().

The PR_SET_TAGGED_ADDR_CTRL will be expanded in the future to handle MTE-specific settings like imprecise vs precise exceptions.

---
 arch/arm64/include/asm/processor.h   |  6 +++
 arch/arm64/include/asm/thread_info.h |  1 +
 arch/arm64/include/asm/uaccess.h     |  5 ++-
 arch/arm64/kernel/process.c          | 67 ++++++++++++++++++++++++++++
 include/uapi/linux/prctl.h           |  5 +++
 kernel/sys.c                         | 16 +++++++
 6 files changed, 99 insertions(+), 1 deletion(-)
arch/arm64/include/asm/processor.h +6 -0
index fcd0e691b1ea..fee457456aa8 100644
@@ -307,6 +307,12 @@ extern void __init minsigstksz_setup(void);
 /* PR_PAC_RESET_KEYS prctl */
 #define PAC_RESET_KEYS(tsk, arg)	ptrauth_prctl_reset_keys(tsk, arg)
 
+/* PR_TAGGED_ADDR prctl */
+long set_tagged_addr_ctrl(unsigned long arg);
+long get_tagged_addr_ctrl(void);
+#define SET_TAGGED_ADDR_CTRL(arg)	set_tagged_addr_ctrl(arg)
+#define GET_TAGGED_ADDR_CTRL()		get_tagged_addr_ctrl()
+
 /*
  * For CONFIG_GCC_PLUGIN_STACKLEAK
  *
arch/arm64/include/asm/thread_info.h +0 -0
index c285d1ce7186..7263d4c973ce 100644

arch/arm64/include/asm/thread_info.h void arch_release_task_struct(struct task_struct *tsk); · line 105

Quoted 5 lines
arch/arm64/include/asm/thread_info.h +1 -0
@@ -101,6 +101,7 @@ void arch_release_task_struct(struct task_struct *tsk);
 #define TIF_SVE			23	/* Scalable Vector Extension in use */
 #define TIF_SVE_VL_INHERIT	24	/* Inherit sve_vl_onexec across exec */
 #define TIF_SSBD		25	/* Wants SSB mitigation */
+#define TIF_TAGGED_ADDR		26
 

Can you please put a comment here?

Quoted 37 lines
arch/arm64/include/asm/thread_info.h +0 -0
 #define _TIF_SIGPENDING		(1 << TIF_SIGPENDING)
 #define _TIF_NEED_RESCHED	(1 << TIF_NEED_RESCHED)
arch/arm64/include/asm/uaccess.h +4 -1
index 9164ecb5feca..995b9ea11a89 100644
@@ -73,6 +73,9 @@ static inline unsigned long __range_ok(const void __user *addr, unsigned long si
 {
 	unsigned long ret, limit = current_thread_info()->addr_limit;
 
+	if (test_thread_flag(TIF_TAGGED_ADDR))
+		addr = untagged_addr(addr);
+
 	__chk_user_ptr(addr);
 	asm volatile(
 	// A + B <= C + 1 for all A,B,C, in four easy steps:
@@ -94,7 +97,7 @@ static inline unsigned long __range_ok(const void __user *addr, unsigned long si
 	return ret;
 }
 
-#define access_ok(addr, size)	__range_ok(untagged_addr(addr), size)
+#define access_ok(addr, size)	__range_ok(addr, size)
 #define user_addr_max			get_fs
 >  #define _ASM_EXTABLE(from, to)						\
arch/arm64/kernel/process.c +1 -0
index 3767fb21a5b8..69d0be1fc708 100644
@@ -30,6 +30,7 @@
 #include <linux/kernel.h>
 #include <linux/mm.h>
 #include <linux/stddef.h>
+#include <linux/sysctl.h>
 #include <linux/unistd.h>
 #include <linux/user.h>
 #include <linux/delay.h>

arch/arm64/kernel/process.c void flush_thread(void) · line 327

Quoted 4 lines
arch/arm64/kernel/process.c +1 -0
@@ -323,6 +324,7 @@ void flush_thread(void)
 	fpsimd_flush_thread();
 	tls_thread_flush();
 	flush_ptrace_hw_breakpoint(current);
+	clear_thread_flag(TIF_TAGGED_ADDR);

Nit: in line we the other functions in thread_flush we could have something like "tagged_addr_thread_flush", maybe inlined.

Quoted 62 lines
arch/arm64/kernel/process.c +55 -0
 }
 
 void release_thread(struct task_struct *dead_task)
@@ -552,3 +554,68 @@ void arch_setup_new_exec(void)
 
 	ptrauth_thread_init_user(current);
 }
+
+/*
+ * Control the relaxed ABI allowing tagged user addresses into the kernel.
+ */
+static unsigned int tagged_addr_prctl_allowed = 1;
+
+long set_tagged_addr_ctrl(unsigned long arg)
+{
+	if (!tagged_addr_prctl_allowed)
+		return -EINVAL;
+	if (is_compat_task())
+		return -EINVAL;
+	if (arg & ~PR_TAGGED_ADDR_ENABLE)
+		return -EINVAL;
+
+	if (arg & PR_TAGGED_ADDR_ENABLE)
+		set_thread_flag(TIF_TAGGED_ADDR);
+	else
+		clear_thread_flag(TIF_TAGGED_ADDR);
+
+	return 0;
+}
+
+long get_tagged_addr_ctrl(void)
+{
+	if (!tagged_addr_prctl_allowed)
+		return -EINVAL;
+	if (is_compat_task())
+		return -EINVAL;
+
+	if (test_thread_flag(TIF_TAGGED_ADDR))
+		return PR_TAGGED_ADDR_ENABLE;
+
+	return 0;
+}
+
+/*
+ * Global sysctl to disable the tagged user addresses support. This control
+ * only prevents the tagged address ABI enabling via prctl() and does not
+ * disable it for tasks that already opted in to the relaxed ABI.
+ */
+static int zero;
+static int one = 1;
+
+static struct ctl_table tagged_addr_sysctl_table[] = {
+	{
+		.procname	= "tagged_addr",
+		.mode		= 0644,
+		.data		= &tagged_addr_prctl_allowed,
+		.maxlen		= sizeof(int),
+		.proc_handler	= proc_dointvec_minmax,
+		.extra1		= &zero,
+		.extra2		= &one,
+	},
+	{ }

arch/arm64/kernel/process.c void arch_setup_new_exec(void) · line 621

Quoted 10 lines
arch/arm64/kernel/process.c +10 -0
+};
+
+static int __init tagged_addr_init(void)
+{
+	if (!register_sysctl("abi", tagged_addr_sysctl_table))
+		return -EINVAL;
+	return 0;
+}
+
+core_initcall(tagged_addr_init);

process.c seems already a bit "overcrowded". Probably we could move all the tagged_addr features in a separate file. What do you think? It would make easier the implementation of mte as well going forward.

Quoted 35 lines
include/uapi/linux/prctl.h +5 -0
index 094bb03b9cc2..2e927b3e9d6c 100644
@@ -229,4 +229,9 @@ struct prctl_mm_map {
 # define PR_PAC_APDBKEY			(1UL << 3)
 # define PR_PAC_APGAKEY			(1UL << 4)
 
+/* Tagged user address controls for arm64 */
+#define PR_SET_TAGGED_ADDR_CTRL		55
+#define PR_GET_TAGGED_ADDR_CTRL		56
+# define PR_TAGGED_ADDR_ENABLE		(1UL << 0)
+
 #endif /* _LINUX_PRCTL_H */
kernel/sys.c +6 -0
index 2969304c29fe..ec48396b4943 100644
@@ -124,6 +124,12 @@
 #ifndef PAC_RESET_KEYS
 # define PAC_RESET_KEYS(a, b)	(-EINVAL)
 #endif
+#ifndef SET_TAGGED_ADDR_CTRL
+# define SET_TAGGED_ADDR_CTRL(a)	(-EINVAL)
+#endif
+#ifndef GET_TAGGED_ADDR_CTRL
+# define GET_TAGGED_ADDR_CTRL()		(-EINVAL)
+#endif
 
 /*
  * this is where the system-wide overflow UID and GID are defined, for
@@ -2492,6 +2498,16 @@ SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3,
 			return -EINVAL;
 		error = PAC_RESET_KEYS(me, arg2);
 		break;

kernel/sys.c SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3, · line 2510

Quoted 10 lines
kernel/sys.c +10 -0
+	case PR_SET_TAGGED_ADDR_CTRL:
+		if (arg3 || arg4 || arg5)
+			return -EINVAL;
+		error = SET_TAGGED_ADDR_CTRL(arg2);
+		break;
+	case PR_GET_TAGGED_ADDR_CTRL:
+		if (arg2 || arg3 || arg4 || arg5)
+			return -EINVAL;
+		error = GET_TAGGED_ADDR_CTRL();
+		break;

Why do we need two prctl here? We could have only one and use arg2 as set/get and arg3 as a parameter. What do you think?

Quoted 4 lines
kernel/sys.c +0 -0
 	default:
 		error = -EINVAL;
 		break;
Signature · 2 lines
Regards,
Vincenzo
maintainer
#16 lore raw

Re: [PATCH v16 02/16] arm64: untag user pointers in access_ok and __uaccess_mask_ptr

Hi Vincenzo,

Quoted 12 lines · Vincenzo Frascino
Quoted 12 lines
arch/arm64/kernel/process.c +1 -0
index 3767fb21a5b8..69d0be1fc708 100644
@@ -30,6 +30,7 @@
 #include <linux/kernel.h>
 #include <linux/mm.h>
 #include <linux/stddef.h>
+#include <linux/sysctl.h>
 #include <linux/unistd.h>
 #include <linux/user.h>
 #include <linux/delay.h>
Quoted 8 lines

arch/arm64/kernel/process.c void flush_thread(void) · line 327

Quoted 5 lines
arch/arm64/kernel/process.c +1 -0
@@ -323,6 +324,7 @@ void flush_thread(void)
 	fpsimd_flush_thread();
 	tls_thread_flush();
 	flush_ptrace_hw_breakpoint(current);
+	clear_thread_flag(TIF_TAGGED_ADDR);

Nit: in line we the other functions in thread_flush we could have something like "tagged_addr_thread_flush", maybe inlined.

The other functions do a lot more than clearing a TIF flag, so they deserved their own place. We could do this when adding MTE support. I think we also need to check what other TIF flags we may inadvertently pass on execve(), maybe have a mask clearing.

Quoted 40 lines
Quoted 40 lines
include/uapi/linux/prctl.h +5 -0
index 094bb03b9cc2..2e927b3e9d6c 100644
@@ -229,4 +229,9 @@ struct prctl_mm_map {
 # define PR_PAC_APDBKEY			(1UL << 3)
 # define PR_PAC_APGAKEY			(1UL << 4)
 
+/* Tagged user address controls for arm64 */
+#define PR_SET_TAGGED_ADDR_CTRL		55
+#define PR_GET_TAGGED_ADDR_CTRL		56
+# define PR_TAGGED_ADDR_ENABLE		(1UL << 0)
+
 #endif /* _LINUX_PRCTL_H */
kernel/sys.c +11 -0
index 2969304c29fe..ec48396b4943 100644
@@ -124,6 +124,12 @@
 #ifndef PAC_RESET_KEYS
 # define PAC_RESET_KEYS(a, b)	(-EINVAL)
 #endif
+#ifndef SET_TAGGED_ADDR_CTRL
+# define SET_TAGGED_ADDR_CTRL(a)	(-EINVAL)
+#endif
+#ifndef GET_TAGGED_ADDR_CTRL
+# define GET_TAGGED_ADDR_CTRL()		(-EINVAL)
+#endif
 
 /*
  * this is where the system-wide overflow UID and GID are defined, for
@@ -2492,6 +2498,16 @@ SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3,
 			return -EINVAL;
 		error = PAC_RESET_KEYS(me, arg2);
 		break;
+	case PR_SET_TAGGED_ADDR_CTRL:
+		if (arg3 || arg4 || arg5)
+			return -EINVAL;
+		error = SET_TAGGED_ADDR_CTRL(arg2);
+		break;
Quoted 8 lines

kernel/sys.c SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3, · line 2510

Quoted 5 lines
kernel/sys.c +5 -0
+	case PR_GET_TAGGED_ADDR_CTRL:
+		if (arg2 || arg3 || arg4 || arg5)
+			return -EINVAL;
+		error = GET_TAGGED_ADDR_CTRL();
+		break;

Why do we need two prctl here? We could have only one and use arg2 as set/get and arg3 as a parameter. What do you think?

This follows the other PR_* options, e.g. PR_SET_VL/GET_VL, PR_*_FP_MODE. We will use other bits in arg2, for example to set the precise vs imprecise MTE trapping.

Signature · 1 line
Catalin
#17 lore raw

Re: [PATCH v16 02/16] arm64: untag user pointers in access_ok and __uaccess_mask_ptr

Hi Catalin,

Quoted 24 lines · Catalin Marinas

Hi Vincenzo,

Quoted 12 lines · Vincenzo Frascino
Quoted 12 lines
arch/arm64/kernel/process.c +1 -0
index 3767fb21a5b8..69d0be1fc708 100644
@@ -30,6 +30,7 @@
 #include <linux/kernel.h>
 #include <linux/mm.h>
 #include <linux/stddef.h>
+#include <linux/sysctl.h>
 #include <linux/unistd.h>
 #include <linux/user.h>
 #include <linux/delay.h>
Quoted 8 lines

arch/arm64/kernel/process.c void flush_thread(void) · line 327

Quoted 5 lines
arch/arm64/kernel/process.c +1 -0
@@ -323,6 +324,7 @@ void flush_thread(void)
 	fpsimd_flush_thread();
 	tls_thread_flush();
 	flush_ptrace_hw_breakpoint(current);
+	clear_thread_flag(TIF_TAGGED_ADDR);

Nit: in line we the other functions in thread_flush we could have something like "tagged_addr_thread_flush", maybe inlined.

Quoted 5 lines

The other functions do a lot more than clearing a TIF flag, so they deserved their own place. We could do this when adding MTE support. I think we also need to check what other TIF flags we may inadvertently pass on execve(), maybe have a mask clearing.

Agreed. All the comments I provided are meant to simplify the addition of MTE support.

Quoted 49 lines
Quoted 40 lines
Quoted 40 lines
include/uapi/linux/prctl.h +5 -0
index 094bb03b9cc2..2e927b3e9d6c 100644
@@ -229,4 +229,9 @@ struct prctl_mm_map {
 # define PR_PAC_APDBKEY			(1UL << 3)
 # define PR_PAC_APGAKEY			(1UL << 4)
 
+/* Tagged user address controls for arm64 */
+#define PR_SET_TAGGED_ADDR_CTRL		55
+#define PR_GET_TAGGED_ADDR_CTRL		56
+# define PR_TAGGED_ADDR_ENABLE		(1UL << 0)
+
 #endif /* _LINUX_PRCTL_H */
kernel/sys.c +11 -0
index 2969304c29fe..ec48396b4943 100644
@@ -124,6 +124,12 @@
 #ifndef PAC_RESET_KEYS
 # define PAC_RESET_KEYS(a, b)	(-EINVAL)
 #endif
+#ifndef SET_TAGGED_ADDR_CTRL
+# define SET_TAGGED_ADDR_CTRL(a)	(-EINVAL)
+#endif
+#ifndef GET_TAGGED_ADDR_CTRL
+# define GET_TAGGED_ADDR_CTRL()		(-EINVAL)
+#endif
 
 /*
  * this is where the system-wide overflow UID and GID are defined, for
@@ -2492,6 +2498,16 @@ SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3,
 			return -EINVAL;
 		error = PAC_RESET_KEYS(me, arg2);
 		break;
+	case PR_SET_TAGGED_ADDR_CTRL:
+		if (arg3 || arg4 || arg5)
+			return -EINVAL;
+		error = SET_TAGGED_ADDR_CTRL(arg2);
+		break;
Quoted 8 lines

kernel/sys.c SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3, · line 2510

Quoted 5 lines
kernel/sys.c +5 -0
+	case PR_GET_TAGGED_ADDR_CTRL:
+		if (arg2 || arg3 || arg4 || arg5)
+			return -EINVAL;
+		error = GET_TAGGED_ADDR_CTRL();
+		break;

Why do we need two prctl here? We could have only one and use arg2 as set/get and arg3 as a parameter. What do you think?

Quoted 4 lines

This follows the other PR_* options, e.g. PR_SET_VL/GET_VL, PR_*_FP_MODE. We will use other bits in arg2, for example to set the precise vs imprecise MTE trapping.

Indeed. I was not questioning the pre-existing interface definition, but trying more to reduce the changes to the ABI to the minimum since:
- prctl does not mandate how to use the arg[2-5]
- prctl interface is flexible enough for the problem to be solved with only one PR_ command.

I agree on reusing the interface for MTE for the purposes you specified.

Signature · 2 lines
Regards,
Vincenzo
#18 lore raw

Re: [PATCH v16 02/16] arm64: untag user pointers in access_ok and __uaccess_mask_ptr

In reply to Catalin Marinas
Quoted 26 lines · Catalin Marinas
Quoted 9 lines · Catalin Marinas
Quoted 8 lines · Andrey Konovalov
arch/arm64/include/asm/uaccess.h +0 -0
index e5d5f31c6d36..9164ecb5feca 100644
@@ -94,7 +94,7 @@ static inline unsigned long __range_ok(const void __user *addr, unsigned long si
    return ret;
 }
 
Quoted 10 lines

arch/arm64/include/asm/uaccess.h static inline unsigned long __range_ok(const void __user *addr, unsigned long si · line 97

Quoted 2 lines
arch/arm64/include/asm/uaccess.h +1 -1
-#define access_ok(addr, size)      __range_ok(addr, size)
+#define access_ok(addr, size)      __range_ok(untagged_addr(addr), size)

I'm going to propose an opt-in method here (RFC for now). We can't have a check in untagged_addr() since this is already used throughout the kernel for both user and kernel addresses (khwasan) but we can add one in __range_ok(). The same prctl() option will be used for controlling the precise/imprecise mode of MTE later on. We can use a TIF_ flag here assuming that this will be called early on and any cloned thread will inherit this.

Updated patch, inlining it below. Once we agreed on the approach, I think Andrey can insert in in this series, probably after patch 2. The differences from the one I posted yesterday:

Quoted 7 lines

- renamed PR_* macros together with get/set variants and the possibility to disable the relaxed ABI

- sysctl option - /proc/sys/abi/tagged_addr to disable the ABI globally (just the prctl() opt-in, tasks already using it won't be affected)

And, of course, it needs more testing.

Sure, I'll add it to the series.

Should I drop access_ok() change from my patch, since yours just reverts it?

Thanks!

Quoted 218 lines

---------8<----------------
From 7c624777a4e545522dec1b34e60f0229cb2bd59f Mon Sep 17 00:00:00 2001
From: Catalin Marinas <[email protected]>
Date: Tue, 11 Jun 2019 13:03:38 +0100
Subject: [PATCH] arm64: Introduce prctl() options to control the tagged user
addresses ABI

It is not desirable to relax the ABI to allow tagged user addresses into the kernel indiscriminately. This patch introduces a prctl() interface for enabling or disabling the tagged ABI with a global sysctl control for preventing applications from enabling the relaxed ABI (meant for testing user-space prctl() return error checking without reconfiguring the kernel). The ABI properties are inherited by threads of the same application and fork()'ed children but cleared on execve().

The PR_SET_TAGGED_ADDR_CTRL will be expanded in the future to handle MTE-specific settings like imprecise vs precise exceptions.

---
 arch/arm64/include/asm/processor.h   |  6 +++
 arch/arm64/include/asm/thread_info.h |  1 +
 arch/arm64/include/asm/uaccess.h     |  5 ++-
 arch/arm64/kernel/process.c          | 67 ++++++++++++++++++++++++++++
 include/uapi/linux/prctl.h           |  5 +++
 kernel/sys.c                         | 16 +++++++
 6 files changed, 99 insertions(+), 1 deletion(-)
arch/arm64/include/asm/processor.h +6 -0
index fcd0e691b1ea..fee457456aa8 100644
@@ -307,6 +307,12 @@ extern void __init minsigstksz_setup(void);
 /* PR_PAC_RESET_KEYS prctl */
 #define PAC_RESET_KEYS(tsk, arg)       ptrauth_prctl_reset_keys(tsk, arg)
 
+/* PR_TAGGED_ADDR prctl */
+long set_tagged_addr_ctrl(unsigned long arg);
+long get_tagged_addr_ctrl(void);
+#define SET_TAGGED_ADDR_CTRL(arg)      set_tagged_addr_ctrl(arg)
+#define GET_TAGGED_ADDR_CTRL()         get_tagged_addr_ctrl()
+
 /*
  * For CONFIG_GCC_PLUGIN_STACKLEAK
  *
arch/arm64/include/asm/thread_info.h +1 -0
index c285d1ce7186..7263d4c973ce 100644
@@ -101,6 +101,7 @@ void arch_release_task_struct(struct task_struct *tsk);
 #define TIF_SVE                        23      /* Scalable Vector Extension in use */
 #define TIF_SVE_VL_INHERIT     24      /* Inherit sve_vl_onexec across exec */
 #define TIF_SSBD               25      /* Wants SSB mitigation */
+#define TIF_TAGGED_ADDR                26
 
 #define _TIF_SIGPENDING                (1 << TIF_SIGPENDING)
 #define _TIF_NEED_RESCHED      (1 << TIF_NEED_RESCHED)
arch/arm64/include/asm/uaccess.h +4 -1
index 9164ecb5feca..995b9ea11a89 100644
@@ -73,6 +73,9 @@ static inline unsigned long __range_ok(const void __user *addr, unsigned long si
 {
        unsigned long ret, limit = current_thread_info()->addr_limit;
 
+       if (test_thread_flag(TIF_TAGGED_ADDR))
+               addr = untagged_addr(addr);
+
        __chk_user_ptr(addr);
        asm volatile(
        // A + B <= C + 1 for all A,B,C, in four easy steps:
@@ -94,7 +97,7 @@ static inline unsigned long __range_ok(const void __user *addr, unsigned long si
        return ret;
 }
 
-#define access_ok(addr, size)  __range_ok(untagged_addr(addr), size)
+#define access_ok(addr, size)  __range_ok(addr, size)
 #define user_addr_max                  get_fs
 
 #define _ASM_EXTABLE(from, to)                                         \
arch/arm64/kernel/process.c +67 -0
index 3767fb21a5b8..69d0be1fc708 100644
@@ -30,6 +30,7 @@
 #include <linux/kernel.h>
 #include <linux/mm.h>
 #include <linux/stddef.h>
+#include <linux/sysctl.h>
 #include <linux/unistd.h>
 #include <linux/user.h>
 #include <linux/delay.h>
@@ -323,6 +324,7 @@ void flush_thread(void)
        fpsimd_flush_thread();
        tls_thread_flush();
        flush_ptrace_hw_breakpoint(current);
+       clear_thread_flag(TIF_TAGGED_ADDR);
 }
 
 void release_thread(struct task_struct *dead_task)
@@ -552,3 +554,68 @@ void arch_setup_new_exec(void)
 
        ptrauth_thread_init_user(current);
 }
+
+/*
+ * Control the relaxed ABI allowing tagged user addresses into the kernel.
+ */
+static unsigned int tagged_addr_prctl_allowed = 1;
+
+long set_tagged_addr_ctrl(unsigned long arg)
+{
+       if (!tagged_addr_prctl_allowed)
+               return -EINVAL;
+       if (is_compat_task())
+               return -EINVAL;
+       if (arg & ~PR_TAGGED_ADDR_ENABLE)
+               return -EINVAL;
+
+       if (arg & PR_TAGGED_ADDR_ENABLE)
+               set_thread_flag(TIF_TAGGED_ADDR);
+       else
+               clear_thread_flag(TIF_TAGGED_ADDR);
+
+       return 0;
+}
+
+long get_tagged_addr_ctrl(void)
+{
+       if (!tagged_addr_prctl_allowed)
+               return -EINVAL;
+       if (is_compat_task())
+               return -EINVAL;
+
+       if (test_thread_flag(TIF_TAGGED_ADDR))
+               return PR_TAGGED_ADDR_ENABLE;
+
+       return 0;
+}
+
+/*
+ * Global sysctl to disable the tagged user addresses support. This control
+ * only prevents the tagged address ABI enabling via prctl() and does not
+ * disable it for tasks that already opted in to the relaxed ABI.
+ */
+static int zero;
+static int one = 1;
+
+static struct ctl_table tagged_addr_sysctl_table[] = {
+       {
+               .procname       = "tagged_addr",
+               .mode           = 0644,
+               .data           = &tagged_addr_prctl_allowed,
+               .maxlen         = sizeof(int),
+               .proc_handler   = proc_dointvec_minmax,
+               .extra1         = &zero,
+               .extra2         = &one,
+       },
+       { }
+};
+
+static int __init tagged_addr_init(void)
+{
+       if (!register_sysctl("abi", tagged_addr_sysctl_table))
+               return -EINVAL;
+       return 0;
+}
+
+core_initcall(tagged_addr_init);
include/uapi/linux/prctl.h +5 -0
index 094bb03b9cc2..2e927b3e9d6c 100644
@@ -229,4 +229,9 @@ struct prctl_mm_map {
 # define PR_PAC_APDBKEY                        (1UL << 3)
 # define PR_PAC_APGAKEY                        (1UL << 4)
 
+/* Tagged user address controls for arm64 */
+#define PR_SET_TAGGED_ADDR_CTRL                55
+#define PR_GET_TAGGED_ADDR_CTRL                56
+# define PR_TAGGED_ADDR_ENABLE         (1UL << 0)
+
 #endif /* _LINUX_PRCTL_H */
kernel/sys.c +16 -0
index 2969304c29fe..ec48396b4943 100644
@@ -124,6 +124,12 @@
 #ifndef PAC_RESET_KEYS
 # define PAC_RESET_KEYS(a, b)  (-EINVAL)
 #endif
+#ifndef SET_TAGGED_ADDR_CTRL
+# define SET_TAGGED_ADDR_CTRL(a)       (-EINVAL)
+#endif
+#ifndef GET_TAGGED_ADDR_CTRL
+# define GET_TAGGED_ADDR_CTRL()                (-EINVAL)
+#endif
 
 /*
  * this is where the system-wide overflow UID and GID are defined, for
@@ -2492,6 +2498,16 @@ SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3,
                        return -EINVAL;
                error = PAC_RESET_KEYS(me, arg2);
                break;
+       case PR_SET_TAGGED_ADDR_CTRL:
+               if (arg3 || arg4 || arg5)
+                       return -EINVAL;
+               error = SET_TAGGED_ADDR_CTRL(arg2);
+               break;
+       case PR_GET_TAGGED_ADDR_CTRL:
+               if (arg2 || arg3 || arg4 || arg5)
+                       return -EINVAL;
+               error = GET_TAGGED_ADDR_CTRL();
+               break;
        default:
                error = -EINVAL;
                break;
maintainer
#19 lore raw

Re: [PATCH v16 02/16] arm64: untag user pointers in access_ok and __uaccess_mask_ptr

Quoted 30 lines · Andrey Konovalov
Quoted 29 lines · Catalin Marinas
Quoted 9 lines · Catalin Marinas
Quoted 8 lines · Andrey Konovalov
arch/arm64/include/asm/uaccess.h +0 -0
index e5d5f31c6d36..9164ecb5feca 100644
@@ -94,7 +94,7 @@ static inline unsigned long __range_ok(const void __user *addr, unsigned long si
    return ret;
 }
 
Quoted 10 lines

arch/arm64/include/asm/uaccess.h static inline unsigned long __range_ok(const void __user *addr, unsigned long si · line 97

Quoted 2 lines
arch/arm64/include/asm/uaccess.h +1 -1
-#define access_ok(addr, size)      __range_ok(addr, size)
+#define access_ok(addr, size)      __range_ok(untagged_addr(addr), size)

I'm going to propose an opt-in method here (RFC for now). We can't have a check in untagged_addr() since this is already used throughout the kernel for both user and kernel addresses (khwasan) but we can add one in __range_ok(). The same prctl() option will be used for controlling the precise/imprecise mode of MTE later on. We can use a TIF_ flag here assuming that this will be called early on and any cloned thread will inherit this.

Updated patch, inlining it below. Once we agreed on the approach, I think Andrey can insert in in this series, probably after patch 2. The differences from the one I posted yesterday:

- renamed PR_* macros together with get/set variants and the possibility to disable the relaxed ABI

Quoted 8 lines
Quoted 4 lines

- sysctl option - /proc/sys/abi/tagged_addr to disable the ABI globally (just the prctl() opt-in, tasks already using it won't be affected)

And, of course, it needs more testing.

Sure, I'll add it to the series.

Should I drop access_ok() change from my patch, since yours just reverts it?

Not necessary, your patch just relaxes the ABI for all apps, mine tightens it. You could instead move the untagging to __range_ok() and rebase my patch accordingly.

Signature · 1 line
Catalin
#20 lore raw

Re: [PATCH v16 02/16] arm64: untag user pointers in access_ok and __uaccess_mask_ptr

Quoted 37 lines · Catalin Marinas
Quoted 35 lines · Andrey Konovalov
Quoted 33 lines · Catalin Marinas
Quoted 9 lines · Catalin Marinas
Quoted 8 lines · Andrey Konovalov
arch/arm64/include/asm/uaccess.h +0 -0
index e5d5f31c6d36..9164ecb5feca 100644
@@ -94,7 +94,7 @@ static inline unsigned long __range_ok(const void __user *addr, unsigned long si
    return ret;
 }
 
Quoted 10 lines

arch/arm64/include/asm/uaccess.h static inline unsigned long __range_ok(const void __user *addr, unsigned long si · line 97

Quoted 2 lines
arch/arm64/include/asm/uaccess.h +1 -1
-#define access_ok(addr, size)      __range_ok(addr, size)
+#define access_ok(addr, size)      __range_ok(untagged_addr(addr), size)

I'm going to propose an opt-in method here (RFC for now). We can't have a check in untagged_addr() since this is already used throughout the kernel for both user and kernel addresses (khwasan) but we can add one in __range_ok(). The same prctl() option will be used for controlling the precise/imprecise mode of MTE later on. We can use a TIF_ flag here assuming that this will be called early on and any cloned thread will inherit this.

Updated patch, inlining it below. Once we agreed on the approach, I think Andrey can insert in in this series, probably after patch 2. The differences from the one I posted yesterday:

- renamed PR_* macros together with get/set variants and the possibility to disable the relaxed ABI

- sysctl option - /proc/sys/abi/tagged_addr to disable the ABI globally (just the prctl() opt-in, tasks already using it won't be affected)

And, of course, it needs more testing.

Quoted 7 lines
Quoted 3 lines

Sure, I'll add it to the series.

Should I drop access_ok() change from my patch, since yours just reverts it?

Not necessary, your patch just relaxes the ABI for all apps, mine tightens it. You could instead move the untagging to __range_ok() and rebase my patch accordingly.

OK, will do. I'll also add a comment next to TIF_TAGGED_ADDR as Vincenzo asked.

Quoted 3 lines

--
Catalin

maintainer
#21 lore raw

Re: [PATCH v16 02/16] arm64: untag user pointers in access_ok and __uaccess_mask_ptr

Quoted 9 lines · Andrey Konovalov
Quoted 6 lines · Catalin Marinas
Quoted 1 line · Andrey Konovalov

Should I drop access_ok() change from my patch, since yours just reverts it?

Not necessary, your patch just relaxes the ABI for all apps, mine tightens it. You could instead move the untagging to __range_ok() and rebase my patch accordingly.

OK, will do. I'll also add a comment next to TIF_TAGGED_ADDR as Vincenzo asked.

Thanks.

Signature · 1 line
Catalin
#22 lore raw

PATCH v16 3/16 lib, arm64: untag user pointers in strn*_user

In reply to Andrey Konovalov

This patch is a part of a series that extends arm64 kernel ABI to allow to pass tagged user pointers (with the top byte set to something else other than 0x00) as syscall arguments.

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.

Note, that this patch only temporarily untags the pointers to perform validity checks, but then uses them as is to perform user memory accesses.

---
 lib/strncpy_from_user.c | 3 ++-
 lib/strnlen_user.c      | 3 ++-
 2 files changed, 4 insertions(+), 2 deletions(-)
lib/strncpy_from_user.c +2 -1
index 023ba9f3b99f..dccb95af6003 100644
@@ -6,6 +6,7 @@
 #include <linux/uaccess.h>
 #include <linux/kernel.h>
 #include <linux/errno.h>
+#include <linux/mm.h>
 
 #include <asm/byteorder.h>
 #include <asm/word-at-a-time.h>
@@ -108,7 +109,7 @@ long strncpy_from_user(char *dst, const char __user *src, long count)
 		return 0;
 
 	max_addr = user_addr_max();
-	src_addr = (unsigned long)src;
+	src_addr = (unsigned long)untagged_addr(src);
 	if (likely(src_addr < max_addr)) {
 		unsigned long max = max_addr - src_addr;
 		long retval;
lib/strnlen_user.c +2 -1
index 7f2db3fe311f..28ff554a1be8 100644
@@ -2,6 +2,7 @@
 #include <linux/kernel.h>
 #include <linux/export.h>
 #include <linux/uaccess.h>
+#include <linux/mm.h>
 
 #include <asm/word-at-a-time.h>
 
@@ -109,7 +110,7 @@ long strnlen_user(const char __user *str, long count)
 		return 0;
 
 	max_addr = user_addr_max();
-	src_addr = (unsigned long)str;
+	src_addr = (unsigned long)untagged_addr(str);
 	if (likely(src_addr < max_addr)) {
 		unsigned long max = max_addr - src_addr;
 		long retval;
Signature · 1 line
2.22.0.rc1.311.g5d7573a151-goog
Acked-by on v16 3/16
#23 lore raw

Re: [PATCH v16 03/16] lib, arm64: untag user pointers in strn*_user

Quoted 8 lines · Andrey Konovalov

This patch is a part of a series that extends arm64 kernel ABI to allow to pass tagged user pointers (with the top byte set to something else other than 0x00) as syscall arguments.

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.

Quoted 7 lines

Untag user pointers passed to these functions.

Note, that this patch only temporarily untags the pointers to perform validity checks, but then uses them as is to perform user memory accesses.

-Kees

Quoted 50 lines
---
 lib/strncpy_from_user.c | 3 ++-
 lib/strnlen_user.c      | 3 ++-
 2 files changed, 4 insertions(+), 2 deletions(-)
lib/strncpy_from_user.c +2 -1
index 023ba9f3b99f..dccb95af6003 100644
@@ -6,6 +6,7 @@
 #include <linux/uaccess.h>
 #include <linux/kernel.h>
 #include <linux/errno.h>
+#include <linux/mm.h>
 
 #include <asm/byteorder.h>
 #include <asm/word-at-a-time.h>
@@ -108,7 +109,7 @@ long strncpy_from_user(char *dst, const char __user *src, long count)
 		return 0;
 
 	max_addr = user_addr_max();
-	src_addr = (unsigned long)src;
+	src_addr = (unsigned long)untagged_addr(src);
 	if (likely(src_addr < max_addr)) {
 		unsigned long max = max_addr - src_addr;
 		long retval;
lib/strnlen_user.c +2 -1
index 7f2db3fe311f..28ff554a1be8 100644
@@ -2,6 +2,7 @@
 #include <linux/kernel.h>
 #include <linux/export.h>
 #include <linux/uaccess.h>
+#include <linux/mm.h>
 
 #include <asm/word-at-a-time.h>
 
@@ -109,7 +110,7 @@ long strnlen_user(const char __user *str, long count)
 		return 0;
 
 	max_addr = user_addr_max();
-	src_addr = (unsigned long)str;
+	src_addr = (unsigned long)untagged_addr(str);
 	if (likely(src_addr < max_addr)) {
 		unsigned long max = max_addr - src_addr;
 		long retval;
Signature · 1 line
2.22.0.rc1.311.g5d7573a151-goog
Signature · 5 lines
Kees Cook
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
Reviewed-by on v16 3/16
#24 lore raw

Re: [PATCH v16 03/16] lib, arm64: untag user pointers in strn*_user

In reply to Andrey Konovalov
Quoted 13 lines · Andrey Konovalov

This patch is a part of a series that extends arm64 kernel ABI to allow to pass tagged user pointers (with the top byte set to something else other than 0x00) as syscall arguments.

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.

Note, that this patch only temporarily untags the pointers to perform validity checks, but then uses them as is to perform user memory accesses.

Quoted 6 lines
---
 lib/strncpy_from_user.c | 3 ++-
 lib/strnlen_user.c      | 3 ++-
 2 files changed, 4 insertions(+), 2 deletions(-)

Looks good.

Quoted 44 lines
lib/strncpy_from_user.c +2 -1
index 023ba9f3b99f..dccb95af6003 100644
@@ -6,6 +6,7 @@
 #include <linux/uaccess.h>
 #include <linux/kernel.h>
 #include <linux/errno.h>
+#include <linux/mm.h>
 
 #include <asm/byteorder.h>
 #include <asm/word-at-a-time.h>
@@ -108,7 +109,7 @@ long strncpy_from_user(char *dst, const char __user *src, long count)
 		return 0;
 
 	max_addr = user_addr_max();
-	src_addr = (unsigned long)src;
+	src_addr = (unsigned long)untagged_addr(src);
 	if (likely(src_addr < max_addr)) {
 		unsigned long max = max_addr - src_addr;
 		long retval;
lib/strnlen_user.c +2 -1
index 7f2db3fe311f..28ff554a1be8 100644
@@ -2,6 +2,7 @@
 #include <linux/kernel.h>
 #include <linux/export.h>
 #include <linux/uaccess.h>
+#include <linux/mm.h>
 
 #include <asm/word-at-a-time.h>
 
@@ -109,7 +110,7 @@ long strnlen_user(const char __user *str, long count)
 		return 0;
 
 	max_addr = user_addr_max();
-	src_addr = (unsigned long)str;
+	src_addr = (unsigned long)untagged_addr(str);
 	if (likely(src_addr < max_addr)) {
 		unsigned long max = max_addr - src_addr;
 		long retval;
#25 lore raw

PATCH v16 4/16 mm: untag user pointers in do_pages_move

In reply to Andrey Konovalov

This patch is a part of a series that extends arm64 kernel ABI to allow to pass tagged user pointers (with the top byte set to something else other than 0x00) as syscall arguments.

do_pages_move() is used in the implementation of the move_pages syscall.

Untag user pointers in this function.

---
 mm/migrate.c | 1 +
 1 file changed, 1 insertion(+)
mm/migrate.c +1 -0
index f2ecc2855a12..3930bb6fa656 100644
@@ -1617,6 +1617,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)
Signature · 1 line
2.22.0.rc1.311.g5d7573a151-goog
Reviewed-by on v16 4/16
#26 lore raw

Re: [PATCH v16 04/16] mm: untag user pointers in do_pages_move

Quoted 10 lines · Andrey Konovalov

This patch is a part of a series that extends arm64 kernel ABI to allow to pass tagged user pointers (with the top byte set to something else other than 0x00) as syscall arguments.

do_pages_move() is used in the implementation of the move_pages syscall.

Untag user pointers in this function.

-Kees

Quoted 19 lines
---
 mm/migrate.c | 1 +
 1 file changed, 1 insertion(+)
mm/migrate.c +1 -0
index f2ecc2855a12..3930bb6fa656 100644
@@ -1617,6 +1617,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)
Signature · 1 line
2.22.0.rc1.311.g5d7573a151-goog
Signature · 1 line
Kees Cook
#27 lore raw

Re: [PATCH v16 04/16] mm: untag user pointers in do_pages_move

In reply to Andrey Konovalov
Quoted 19 lines · Andrey Konovalov

This patch is a part of a series that extends arm64 kernel ABI to allow to pass tagged user pointers (with the top byte set to something else other than 0x00) as syscall arguments.

do_pages_move() is used in the implementation of the move_pages syscall.

Untag user pointers in this function.

---
 mm/migrate.c | 1 +
 1 file changed, 1 insertion(+)
mm/migrate.c +0 -0
index f2ecc2855a12..3930bb6fa656 100644

mm/migrate.c static int do_pages_move(struct mm_struct *mm, nodemask_t task_nodes, · line 1620

Quoted 4 lines
mm/migrate.c +1 -0
@@ -1617,6 +1617,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);

Why not just "addr = (unsigned long)untagged_addr(p);"

--
Khalid

#28 lore raw

Re: [PATCH v16 04/16] mm: untag user pointers in do_pages_move

Quoted 16 lines · Khalid Aziz
Quoted 14 lines · Andrey Konovalov

This patch is a part of a series that extends arm64 kernel ABI to allow to pass tagged user pointers (with the top byte set to something else other than 0x00) as syscall arguments.

do_pages_move() is used in the implementation of the move_pages syscall.

Untag user pointers in this function.

---
 mm/migrate.c | 1 +
 1 file changed, 1 insertion(+)
Quoted 11 lines

mm/migrate.c static int do_pages_move(struct mm_struct *mm, nodemask_t task_nodes, · line 1620

Quoted 9 lines
mm/migrate.c +1 -0
index f2ecc2855a12..3930bb6fa656 100644
@@ -1617,6 +1617,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);

Why not just "addr = (unsigned long)untagged_addr(p);"

Will do in the next version. I think I'll also merge this commit into the "untag user pointers passed to memory syscalls" one.

Quoted 4 lines

--
Khalid

#29 lore raw

PATCH v16 5/16 arm64: untag user pointers passed to memory syscalls

In reply to Andrey Konovalov

This patch is a part of a series that extends arm64 kernel ABI to allow to pass tagged user pointers (with the top byte set to something else other than 0x00) as syscall arguments.

This patch allows tagged pointers to be passed to the following memory
syscalls: get_mempolicy, madvise, mbind, mincore, mlock, mlock2, mprotect, mremap, msync, munlock.

---
 mm/madvise.c   | 2 ++
 mm/mempolicy.c | 3 +++
 mm/mincore.c   | 2 ++
 mm/mlock.c     | 4 ++++
 mm/mprotect.c  | 2 ++
 mm/mremap.c    | 2 ++
 mm/msync.c     | 2 ++
 7 files changed, 17 insertions(+)
mm/madvise.c +2 -0
index 628022e674a7..39b82f8a698f 100644
@@ -810,6 +810,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;
 
mm/mempolicy.c +3 -0
index 01600d80ae01..78e0a88b2680 100644
@@ -1360,6 +1360,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)
@@ -1517,6 +1518,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;
 
mm/mincore.c +2 -0
index c3f058bd0faf..64c322ed845c 100644
@@ -249,6 +249,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;
mm/mlock.c +4 -0
index 080f3b36415b..e82609eaa428 100644
@@ -674,6 +674,8 @@ static __must_check int do_mlock(unsigned long start, size_t len, vm_flags_t fla
 	unsigned long lock_limit;
 	int error = -ENOMEM;
 
+	start = untagged_addr(start);
+
 	if (!can_do_mlock())
 		return -EPERM;
 
@@ -735,6 +737,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;
 
mm/mprotect.c +2 -0
index bf38dfbbb4b4..19f981b733bc 100644
@@ -465,6 +465,8 @@ static int do_mprotect_pkey(unsigned long start, size_t len,
 	const bool rier = (current->personality & READ_IMPLIES_EXEC) &&
 				(prot & PROT_READ);
 
+	start = untagged_addr(start);
+
 	prot &= ~(PROT_GROWSDOWN|PROT_GROWSUP);
 	if (grows == (PROT_GROWSDOWN|PROT_GROWSUP)) /* can't be both */
 		return -EINVAL;
mm/mremap.c +2 -0
index fc241d23cd97..1d98281f7204 100644
@@ -606,6 +606,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;
 
mm/msync.c +2 -0
index ef30a429623a..c3bd3e75f687 100644
@@ -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))
Signature · 1 line
2.22.0.rc1.311.g5d7573a151-goog
Reviewed-by on v16 5/16
#30 lore raw

Re: [PATCH v16 05/16] arm64: untag user pointers passed to memory syscalls

Quoted 9 lines · Andrey Konovalov

This patch is a part of a series that extends arm64 kernel ABI to allow to pass tagged user pointers (with the top byte set to something else other than 0x00) as syscall arguments.

This patch allows tagged pointers to be passed to the following memory
syscalls: get_mempolicy, madvise, mbind, mincore, mlock, mlock2, mprotect, mremap, msync, munlock.

-Kees

Quoted 121 lines
---
 mm/madvise.c   | 2 ++
 mm/mempolicy.c | 3 +++
 mm/mincore.c   | 2 ++
 mm/mlock.c     | 4 ++++
 mm/mprotect.c  | 2 ++
 mm/mremap.c    | 2 ++
 mm/msync.c     | 2 ++
 7 files changed, 17 insertions(+)
mm/madvise.c +2 -0
index 628022e674a7..39b82f8a698f 100644
@@ -810,6 +810,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;
 
mm/mempolicy.c +3 -0
index 01600d80ae01..78e0a88b2680 100644
@@ -1360,6 +1360,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)
@@ -1517,6 +1518,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;
 
mm/mincore.c +2 -0
index c3f058bd0faf..64c322ed845c 100644
@@ -249,6 +249,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;
mm/mlock.c +4 -0
index 080f3b36415b..e82609eaa428 100644
@@ -674,6 +674,8 @@ static __must_check int do_mlock(unsigned long start, size_t len, vm_flags_t fla
 	unsigned long lock_limit;
 	int error = -ENOMEM;
 
+	start = untagged_addr(start);
+
 	if (!can_do_mlock())
 		return -EPERM;
 
@@ -735,6 +737,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;
 
mm/mprotect.c +2 -0
index bf38dfbbb4b4..19f981b733bc 100644
@@ -465,6 +465,8 @@ static int do_mprotect_pkey(unsigned long start, size_t len,
 	const bool rier = (current->personality & READ_IMPLIES_EXEC) &&
 				(prot & PROT_READ);
 
+	start = untagged_addr(start);
+
 	prot &= ~(PROT_GROWSDOWN|PROT_GROWSUP);
 	if (grows == (PROT_GROWSDOWN|PROT_GROWSUP)) /* can't be both */
 		return -EINVAL;
mm/mremap.c +2 -0
index fc241d23cd97..1d98281f7204 100644
@@ -606,6 +606,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;
 
mm/msync.c +2 -0
index ef30a429623a..c3bd3e75f687 100644
@@ -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))
Signature · 1 line
2.22.0.rc1.311.g5d7573a151-goog
Signature · 5 lines
Kees Cook
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
maintainer Reviewed-by on v16 5/16
#31 lore raw

Re: [PATCH v16 05/16] arm64: untag user pointers passed to memory syscalls

In reply to Andrey Konovalov
Quoted 9 lines · Andrey Konovalov

This patch is a part of a series that extends arm64 kernel ABI to allow to pass tagged user pointers (with the top byte set to something else other than 0x00) as syscall arguments.

This patch allows tagged pointers to be passed to the following memory
syscalls: get_mempolicy, madvise, mbind, mincore, mlock, mlock2, mprotect, mremap, msync, munlock.

I would add in the commit log (and possibly in the code with a comment) that mremap() and mmap() do not currently accept tagged hint addresses. Architectures may interpret the hint tag as a background colour for the corresponding vma. With this:

Signature · 1 line
Catalin
#32 lore raw

Re: [PATCH v16 05/16] arm64: untag user pointers passed to memory syscalls

Quoted 10 lines · Catalin Marinas
Quoted 8 lines · Andrey Konovalov

This patch is a part of a series that extends arm64 kernel ABI to allow to pass tagged user pointers (with the top byte set to something else other than 0x00) as syscall arguments.

This patch allows tagged pointers to be passed to the following memory
syscalls: get_mempolicy, madvise, mbind, mincore, mlock, mlock2, mprotect, mremap, msync, munlock.

Quoted 6 lines
Quoted 1 line

I would add in the commit log (and possibly in the code with a comment) that mremap() and mmap() do not currently accept tagged hint addresses. Architectures may interpret the hint tag as a background colour for the corresponding vma. With this:

I'll change the commit log. Where do you you think I should put this comment? Before mmap and mremap definitions in mm/?

Thanks!

Quoted 5 lines

--
Catalin

maintainer
#33 lore raw

Re: [PATCH v16 05/16] arm64: untag user pointers passed to memory syscalls

Quoted 12 lines · Andrey Konovalov
Quoted 11 lines · Catalin Marinas
Quoted 9 lines · Andrey Konovalov

This patch is a part of a series that extends arm64 kernel ABI to allow to pass tagged user pointers (with the top byte set to something else other than 0x00) as syscall arguments.

This patch allows tagged pointers to be passed to the following memory
syscalls: get_mempolicy, madvise, mbind, mincore, mlock, mlock2, mprotect, mremap, msync, munlock.

Quoted 7 lines
Quoted 4 lines

I would add in the commit log (and possibly in the code with a comment) that mremap() and mmap() do not currently accept tagged hint addresses. Architectures may interpret the hint tag as a background colour for the corresponding vma. With this:

I'll change the commit log. Where do you you think I should put this comment? Before mmap and mremap definitions in mm/?

On arm64 we use our own sys_mmap(). I'd say just add a comment on the generic mremap() just before the untagged_addr() along the lines that new_address is not untagged for preserving similar behaviour to mmap().

Signature · 1 line
Catalin
#34 lore raw

Re: [PATCH v16 05/16] arm64: untag user pointers passed to memory syscalls

Quoted 19 lines · Catalin Marinas
Quoted 17 lines · Andrey Konovalov
Quoted 15 lines · Catalin Marinas
Quoted 9 lines · Andrey Konovalov

This patch is a part of a series that extends arm64 kernel ABI to allow to pass tagged user pointers (with the top byte set to something else other than 0x00) as syscall arguments.

This patch allows tagged pointers to be passed to the following memory
syscalls: get_mempolicy, madvise, mbind, mincore, mlock, mlock2, mprotect, mremap, msync, munlock.

I would add in the commit log (and possibly in the code with a comment) that mremap() and mmap() do not currently accept tagged hint addresses. Architectures may interpret the hint tag as a background colour for the corresponding vma. With this:

Quoted 6 lines
Quoted 2 lines

I'll change the commit log. Where do you you think I should put this comment? Before mmap and mremap definitions in mm/?

On arm64 we use our own sys_mmap(). I'd say just add a comment on the generic mremap() just before the untagged_addr() along the lines that new_address is not untagged for preserving similar behaviour to mmap().

Will do in v17, thanks!

Quoted 3 lines

--
Catalin

#35 lore raw

PATCH v16 6/16 mm, arm64: untag user pointers in mm/gup.c

In reply to Andrey Konovalov

This patch is a part of a series that extends arm64 kernel ABI to allow to pass tagged user pointers (with the top byte set to something else other than 0x00) as syscall arguments.

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 this case.

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

---
 mm/gup.c | 4 ++++
 1 file changed, 4 insertions(+)
mm/gup.c +4 -0
index ddde097cf9e4..c37df3d455a2 100644
@@ -802,6 +802,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));
 
 	/*
@@ -964,6 +966,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;
 
Signature · 1 line
2.22.0.rc1.311.g5d7573a151-goog
Reviewed-by on v16 6/16
#36 lore raw

Re: [PATCH v16 06/16] mm, arm64: untag user pointers in mm/gup.c

Quoted 9 lines · Andrey Konovalov

This patch is a part of a series that extends arm64 kernel ABI to allow to pass tagged user pointers (with the top byte set to something else other than 0x00) as syscall arguments.

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 this case.

Quoted 4 lines

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

-Kees

Quoted 29 lines
---
 mm/gup.c | 4 ++++
 1 file changed, 4 insertions(+)
mm/gup.c +4 -0
index ddde097cf9e4..c37df3d455a2 100644
@@ -802,6 +802,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));
 
 	/*
@@ -964,6 +966,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;
 
Signature · 1 line
2.22.0.rc1.311.g5d7573a151-goog
Signature · 1 line
Kees Cook
#37 lore raw

PATCH v16 7/16 mm, arm64: untag user pointers in get_vaddr_frames

In reply to Andrey Konovalov

This patch is a part of a series that extends arm64 kernel ABI to allow to pass tagged user pointers (with the top byte set to something else other than 0x00) as syscall arguments.

get_vaddr_frames uses provided user pointers for vma lookups, which can only by done with untagged pointers. Instead of locating and changing all callers of this function, perform untagging in it.

---
 mm/frame_vector.c | 2 ++
 1 file changed, 2 insertions(+)
mm/frame_vector.c +2 -0
index c64dca6e27c2..c431ca81dad5 100644
@@ -46,6 +46,8 @@ int get_vaddr_frames(unsigned long start, unsigned int nr_frames,
 	if (WARN_ON_ONCE(nr_frames > vec->nr_allocated))
 		nr_frames = vec->nr_allocated;
 
+	start = untagged_addr(start);
+
 	down_read(&mm->mmap_sem);
 	locked = 1;
 	vma = find_vma_intersection(mm, start, start + 1);
Signature · 1 line
2.22.0.rc1.311.g5d7573a151-goog
Reviewed-by on v16 7/16
#38 lore raw

Re: [PATCH v16 07/16] mm, arm64: untag user pointers in get_vaddr_frames

Quoted 9 lines · Andrey Konovalov

This patch is a part of a series that extends arm64 kernel ABI to allow to pass tagged user pointers (with the top byte set to something else other than 0x00) as syscall arguments.

get_vaddr_frames uses provided user pointers for vma lookups, which can only by done with untagged pointers. Instead of locating and changing all callers of this function, perform untagging in it.

-Kees

Quoted 20 lines
---
 mm/frame_vector.c | 2 ++
 1 file changed, 2 insertions(+)
mm/frame_vector.c +2 -0
index c64dca6e27c2..c431ca81dad5 100644
@@ -46,6 +46,8 @@ int get_vaddr_frames(unsigned long start, unsigned int nr_frames,
 	if (WARN_ON_ONCE(nr_frames > vec->nr_allocated))
 		nr_frames = vec->nr_allocated;
 
+	start = untagged_addr(start);
+
 	down_read(&mm->mmap_sem);
 	locked = 1;
 	vma = find_vma_intersection(mm, start, start + 1);
Signature · 1 line
2.22.0.rc1.311.g5d7573a151-goog
Signature · 1 line
Kees Cook
maintainer Acked-by on v16 7/16
#39 lore raw

Re: [PATCH v16 07/16] mm, arm64: untag user pointers in get_vaddr_frames

In reply to Andrey Konovalov
Quoted 9 lines · Andrey Konovalov

This patch is a part of a series that extends arm64 kernel ABI to allow to pass tagged user pointers (with the top byte set to something else other than 0x00) as syscall arguments.

get_vaddr_frames uses provided user pointers for vma lookups, which can only by done with untagged pointers. Instead of locating and changing all callers of this function, perform untagging in it.

#40 lore raw

PATCH v16 8/16 fs, arm64: untag user pointers in copy_mount_options

In reply to Andrey Konovalov

This patch is a part of a series that extends arm64 kernel ABI to allow to pass tagged user pointers (with the top byte set to something else other than 0x00) as syscall arguments.

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.

---
 fs/namespace.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/namespace.c +1 -1
index b26778bdc236..2e85712a19ed 100644
@@ -2993,7 +2993,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;
 
Signature · 1 line
2.22.0.rc1.311.g5d7573a151-goog
Reviewed-by on v16 8/16
#41 lore raw

Re: [PATCH v16 08/16] fs, arm64: untag user pointers in copy_mount_options

Quoted 10 lines · Andrey Konovalov

This patch is a part of a series that extends arm64 kernel ABI to allow to pass tagged user pointers (with the top byte set to something else other than 0x00) as syscall arguments.

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.

Quoted 4 lines

Untag the address before subtracting.

One thing I just noticed in the commit titles... "arm64" is in the prefix, but these are arch-indep areas. Should the ", arm64" be left out?

I would expect, instead:

fs/namespace: untag user pointers in copy_mount_options

-Kees

Quoted 20 lines
---
 fs/namespace.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/namespace.c +1 -1
index b26778bdc236..2e85712a19ed 100644
@@ -2993,7 +2993,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;
 
Signature · 1 line
2.22.0.rc1.311.g5d7573a151-goog
Signature · 1 line
Kees Cook
#42 lore raw

Re: [PATCH v16 08/16] fs, arm64: untag user pointers in copy_mount_options

Quoted 17 lines · Kees Cook
Quoted 10 lines · Andrey Konovalov

This patch is a part of a series that extends arm64 kernel ABI to allow to pass tagged user pointers (with the top byte set to something else other than 0x00) as syscall arguments.

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.

Quoted 4 lines

Untag the address before subtracting.

Quoted 7 lines

One thing I just noticed in the commit titles... "arm64" is in the prefix, but these are arch-indep areas. Should the ", arm64" be left out?

I would expect, instead:

fs/namespace: untag user pointers in copy_mount_options

Hm, I've added the arm64 tag in all of the patches because they are related to changes in arm64 kernel ABI. I can remove it from all the patches that only touch common code if you think that it makes sense.

Thanks!

Quoted 28 lines

-Kees

Quoted 18 lines
---
 fs/namespace.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/namespace.c +1 -2
index b26778bdc236..2e85712a19ed 100644
@@ -2993,7 +2993,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;
 
--
Quoted 2 lines

2.22.0.rc1.311.g5d7573a151-goog

--
Kees Cook

#43 lore raw

Re: [PATCH v16 08/16] fs, arm64: untag user pointers in copy_mount_options

Quoted 23 lines · Andrey Konovalov
Quoted 21 lines · Kees Cook
Quoted 10 lines · Andrey Konovalov

This patch is a part of a series that extends arm64 kernel ABI to allow to pass tagged user pointers (with the top byte set to something else other than 0x00) as syscall arguments.

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.

Quoted 4 lines

Untag the address before subtracting.

One thing I just noticed in the commit titles... "arm64" is in the prefix, but these are arch-indep areas. Should the ", arm64" be left out?

Quoted 7 lines
Quoted 3 lines

I would expect, instead:

fs/namespace: untag user pointers in copy_mount_options

Hm, I've added the arm64 tag in all of the patches because they are related to changes in arm64 kernel ABI. I can remove it from all the patches that only touch common code if you think that it makes sense.

I'll keep the arm64 tags in commit titles for v17. Please reply explicitly if you think I should remove them. Thanks! :)

Quoted 31 lines

Thanks!

Quoted 28 lines

-Kees

Quoted 18 lines
---
 fs/namespace.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/namespace.c +1 -2
index b26778bdc236..2e85712a19ed 100644
@@ -2993,7 +2993,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;
 
--
Quoted 2 lines

2.22.0.rc1.311.g5d7573a151-goog

--
Kees Cook

#44 lore raw

PATCH v16 9/16 fs, arm64: untag user pointers in fs/userfaultfd.c

In reply to Andrey Konovalov

This patch is a part of a series that extends arm64 kernel ABI to allow to pass tagged user pointers (with the top byte set to something else other than 0x00) as syscall arguments.

userfaultfd code use provided user pointers for vma lookups, which can only by done with untagged pointers.

Untag user pointers in validate_range().

---
 fs/userfaultfd.c | 22 ++++++++++++----------
 1 file changed, 12 insertions(+), 10 deletions(-)
fs/userfaultfd.c +12 -10
index 3b30301c90ec..24d68c3b5ee2 100644
@@ -1263,21 +1263,23 @@ static __always_inline void wake_userfault(struct userfaultfd_ctx *ctx,
 }
 
 static __always_inline int validate_range(struct mm_struct *mm,
-					  __u64 start, __u64 len)
+					  __u64 *start, __u64 len)
 {
 	__u64 task_size = mm->task_size;
 
-	if (start & ~PAGE_MASK)
+	*start = untagged_addr(*start);
+
+	if (*start & ~PAGE_MASK)
 		return -EINVAL;
 	if (len & ~PAGE_MASK)
 		return -EINVAL;
 	if (!len)
 		return -EINVAL;
-	if (start < mmap_min_addr)
+	if (*start < mmap_min_addr)
 		return -EINVAL;
-	if (start >= task_size)
+	if (*start >= task_size)
 		return -EINVAL;
-	if (len > task_size - start)
+	if (len > task_size - *start)
 		return -EINVAL;
 	return 0;
 }
@@ -1327,7 +1329,7 @@ static int userfaultfd_register(struct userfaultfd_ctx *ctx,
 		goto out;
 	}
 
-	ret = validate_range(mm, uffdio_register.range.start,
+	ret = validate_range(mm, &uffdio_register.range.start,
 			     uffdio_register.range.len);
 	if (ret)
 		goto out;
@@ -1516,7 +1518,7 @@ static int userfaultfd_unregister(struct userfaultfd_ctx *ctx,
 	if (copy_from_user(&uffdio_unregister, buf, sizeof(uffdio_unregister)))
 		goto out;
 
-	ret = validate_range(mm, uffdio_unregister.start,
+	ret = validate_range(mm, &uffdio_unregister.start,
 			     uffdio_unregister.len);
 	if (ret)
 		goto out;
@@ -1667,7 +1669,7 @@ static int userfaultfd_wake(struct userfaultfd_ctx *ctx,
 	if (copy_from_user(&uffdio_wake, buf, sizeof(uffdio_wake)))
 		goto out;
 
-	ret = validate_range(ctx->mm, uffdio_wake.start, uffdio_wake.len);
+	ret = validate_range(ctx->mm, &uffdio_wake.start, uffdio_wake.len);
 	if (ret)
 		goto out;
 
@@ -1707,7 +1709,7 @@ static int userfaultfd_copy(struct userfaultfd_ctx *ctx,
 			   sizeof(uffdio_copy)-sizeof(__s64)))
 		goto out;
 
-	ret = validate_range(ctx->mm, uffdio_copy.dst, uffdio_copy.len);
+	ret = validate_range(ctx->mm, &uffdio_copy.dst, uffdio_copy.len);
 	if (ret)
 		goto out;
 	/*
@@ -1763,7 +1765,7 @@ static int userfaultfd_zeropage(struct userfaultfd_ctx *ctx,
 			   sizeof(uffdio_zeropage)-sizeof(__s64)))
 		goto out;
 
-	ret = validate_range(ctx->mm, uffdio_zeropage.range.start,
+	ret = validate_range(ctx->mm, &uffdio_zeropage.range.start,
 			     uffdio_zeropage.range.len);
 	if (ret)
 		goto out;
Signature · 1 line
2.22.0.rc1.311.g5d7573a151-goog
Reviewed-by on v16 9/16
#45 lore raw

Re: [PATCH v16 09/16] fs, arm64: untag user pointers in fs/userfaultfd.c

Quoted 10 lines · Andrey Konovalov

This patch is a part of a series that extends arm64 kernel ABI to allow to pass tagged user pointers (with the top byte set to something else other than 0x00) as syscall arguments.

userfaultfd code use provided user pointers for vma lookups, which can only by done with untagged pointers.

Untag user pointers in validate_range().

"userfaultfd: untag user pointers"

-Kees

Quoted 85 lines
---
 fs/userfaultfd.c | 22 ++++++++++++----------
 1 file changed, 12 insertions(+), 10 deletions(-)
fs/userfaultfd.c +12 -10
index 3b30301c90ec..24d68c3b5ee2 100644
@@ -1263,21 +1263,23 @@ static __always_inline void wake_userfault(struct userfaultfd_ctx *ctx,
 }
 
 static __always_inline int validate_range(struct mm_struct *mm,
-					  __u64 start, __u64 len)
+					  __u64 *start, __u64 len)
 {
 	__u64 task_size = mm->task_size;
 
-	if (start & ~PAGE_MASK)
+	*start = untagged_addr(*start);
+
+	if (*start & ~PAGE_MASK)
 		return -EINVAL;
 	if (len & ~PAGE_MASK)
 		return -EINVAL;
 	if (!len)
 		return -EINVAL;
-	if (start < mmap_min_addr)
+	if (*start < mmap_min_addr)
 		return -EINVAL;
-	if (start >= task_size)
+	if (*start >= task_size)
 		return -EINVAL;
-	if (len > task_size - start)
+	if (len > task_size - *start)
 		return -EINVAL;
 	return 0;
 }
@@ -1327,7 +1329,7 @@ static int userfaultfd_register(struct userfaultfd_ctx *ctx,
 		goto out;
 	}
 
-	ret = validate_range(mm, uffdio_register.range.start,
+	ret = validate_range(mm, &uffdio_register.range.start,
 			     uffdio_register.range.len);
 	if (ret)
 		goto out;
@@ -1516,7 +1518,7 @@ static int userfaultfd_unregister(struct userfaultfd_ctx *ctx,
 	if (copy_from_user(&uffdio_unregister, buf, sizeof(uffdio_unregister)))
 		goto out;
 
-	ret = validate_range(mm, uffdio_unregister.start,
+	ret = validate_range(mm, &uffdio_unregister.start,
 			     uffdio_unregister.len);
 	if (ret)
 		goto out;
@@ -1667,7 +1669,7 @@ static int userfaultfd_wake(struct userfaultfd_ctx *ctx,
 	if (copy_from_user(&uffdio_wake, buf, sizeof(uffdio_wake)))
 		goto out;
 
-	ret = validate_range(ctx->mm, uffdio_wake.start, uffdio_wake.len);
+	ret = validate_range(ctx->mm, &uffdio_wake.start, uffdio_wake.len);
 	if (ret)
 		goto out;
 
@@ -1707,7 +1709,7 @@ static int userfaultfd_copy(struct userfaultfd_ctx *ctx,
 			   sizeof(uffdio_copy)-sizeof(__s64)))
 		goto out;
 
-	ret = validate_range(ctx->mm, uffdio_copy.dst, uffdio_copy.len);
+	ret = validate_range(ctx->mm, &uffdio_copy.dst, uffdio_copy.len);
 	if (ret)
 		goto out;
 	/*
@@ -1763,7 +1765,7 @@ static int userfaultfd_zeropage(struct userfaultfd_ctx *ctx,
 			   sizeof(uffdio_zeropage)-sizeof(__s64)))
 		goto out;
 
-	ret = validate_range(ctx->mm, uffdio_zeropage.range.start,
+	ret = validate_range(ctx->mm, &uffdio_zeropage.range.start,
 			     uffdio_zeropage.range.len);
 	if (ret)
 		goto out;
Signature · 1 line
2.22.0.rc1.311.g5d7573a151-goog
Signature · 1 line
Kees Cook
maintainer Reviewed-by on v16 9/16
#46 lore raw

Re: [PATCH v16 09/16] fs, arm64: untag user pointers in fs/userfaultfd.c

In reply to Andrey Konovalov
Quoted 10 lines · Andrey Konovalov

This patch is a part of a series that extends arm64 kernel ABI to allow to pass tagged user pointers (with the top byte set to something else other than 0x00) as syscall arguments.

userfaultfd code use provided user pointers for vma lookups, which can only by done with untagged pointers.

Untag user pointers in validate_range().

#47 lore raw

PATCH v16 10/16 drm/amdgpu, arm64: untag user pointers

In reply to Andrey Konovalov

This patch is a part of a series that extends arm64 kernel ABI to allow to pass tagged user pointers (with the top byte set to something else other than 0x00) as syscall arguments.

In amdgpu_gem_userptr_ioctl() and amdgpu_amdkfd_gpuvm.c/init_user_pages() an MMU notifier is set up with a (tagged) userspace pointer. The untagged address should be used so that MMU notifiers for the untagged address get correctly matched up with the right BO. This patch untag user pointers in amdgpu_gem_userptr_ioctl() for the GEM case and in amdgpu_amdkfd_gpuvm_ alloc_memory_of_gpu() for the KFD case. This also makes sure that an untagged pointer is passed to amdgpu_ttm_tt_get_user_pages(), which uses it for vma lookups.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c | 2 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c          | 2 ++
 2 files changed, 3 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +1 -1
index a6e5184d436c..5d476e9bbc43 100644
@@ -1108,7 +1108,7 @@ int amdgpu_amdkfd_gpuvm_alloc_memory_of_gpu(
 		alloc_flags = 0;
 		if (!offset || !*offset)
 			return -EINVAL;
-		user_addr = *offset;
+		user_addr = untagged_addr(*offset);
 	} else if (flags & ALLOC_MEM_FLAGS_DOORBELL) {
 		domain = AMDGPU_GEM_DOMAIN_GTT;
 		alloc_domain = AMDGPU_GEM_DOMAIN_CPU;
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +2 -0
index d4fcf5475464..e91df1407618 100644
@@ -287,6 +287,8 @@ int amdgpu_gem_userptr_ioctl(struct drm_device *dev, void *data,
 	uint32_t handle;
 	int r;
 
+	args->addr = untagged_addr(args->addr);
+
 	if (offset_in_page(args->addr | args->size))
 		return -EINVAL;
 
Signature · 1 line
2.22.0.rc1.311.g5d7573a151-goog
#48 lore raw

PATCH v16 11/16 drm/radeon, arm64: untag user pointers in radeon_gem_userptr_ioctl

In reply to Andrey Konovalov

This patch is a part of a series that extends arm64 kernel ABI to allow to pass tagged user pointers (with the top byte set to something else other than 0x00) as syscall arguments.

In radeon_gem_userptr_ioctl() an MMU notifier is set up with a (tagged) userspace pointer. The untagged address should be used so that MMU notifiers for the untagged address get correctly matched up with the right BO. This funcation also calls radeon_ttm_tt_pin_userptr(), which uses provided user pointers for vma lookups, which can only by done with untagged pointers.

This patch untags user pointers in radeon_gem_userptr_ioctl().

---
 drivers/gpu/drm/radeon/radeon_gem.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/gpu/drm/radeon/radeon_gem.c +2 -0
index 44617dec8183..90eb78fb5eb2 100644
@@ -291,6 +291,8 @@ int radeon_gem_userptr_ioctl(struct drm_device *dev, void *data,
 	uint32_t handle;
 	int r;
 
+	args->addr = untagged_addr(args->addr);
+
 	if (offset_in_page(args->addr | args->size))
 		return -EINVAL;
 
Signature · 1 line
2.22.0.rc1.311.g5d7573a151-goog
#49 lore raw

PATCH v16 12/16 IB, arm64: untag user pointers in ib_uverbs_(re)reg_mr()

In reply to Andrey Konovalov

This patch is a part of a series that extends arm64 kernel ABI to allow to pass tagged user pointers (with the top byte set to something else other than 0x00) as syscall arguments.

ib_uverbs_(re)reg_mr() use provided user pointers for vma lookups (through e.g. mlx4_get_umem_mr()), which can only by done with untagged pointers.

Untag user pointers in these functions.

---
 drivers/infiniband/core/uverbs_cmd.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/infiniband/core/uverbs_cmd.c +4 -0
index 5a3a1780ceea..f88ee733e617 100644
@@ -709,6 +709,8 @@ static int ib_uverbs_reg_mr(struct uverbs_attr_bundle *attrs)
 	if (ret)
 		return ret;
 
+	cmd.start = untagged_addr(cmd.start);
+
 	if ((cmd.start & ~PAGE_MASK) != (cmd.hca_va & ~PAGE_MASK))
 		return -EINVAL;
 
@@ -791,6 +793,8 @@ static int ib_uverbs_rereg_mr(struct uverbs_attr_bundle *attrs)
 	if (ret)
 		return ret;
 
+	cmd.start = untagged_addr(cmd.start);
+
 	if (cmd.flags & ~IB_MR_REREG_SUPPORTED || !cmd.flags)
 		return -EINVAL;
 
Signature · 1 line
2.22.0.rc1.311.g5d7573a151-goog
#50 lore raw

Re: [PATCH v16 12/16] IB, arm64: untag user pointers in ib_uverbs_(re)reg_mr()

Quoted 17 lines · Andrey Konovalov

This patch is a part of a series that extends arm64 kernel ABI to allow to pass tagged user pointers (with the top byte set to something else other than 0x00) as syscall arguments.

ib_uverbs_(re)reg_mr() use provided user pointers for vma lookups (through e.g. mlx4_get_umem_mr()), which can only by done with untagged pointers.

Untag user pointers in these functions.

 drivers/infiniband/core/uverbs_cmd.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/infiniband/core/uverbs_cmd.c +1 -0
index 5a3a1780ceea..f88ee733e617 100644
+++ b/drivers/infiniband/core/uverbs_cmd.c

drivers/infiniband/core/uverbs_cmd.c static int ib_uverbs_reg_mr(struct uverbs_attr_bundle *attrs) · line 715

Quoted 7 lines
drivers/infiniband/core/uverbs_cmd.c +2 -0
@@ -709,6 +709,8 @@ static int ib_uverbs_reg_mr(struct uverbs_attr_bundle *attrs)
 	if (ret)
 		return ret;
 
+	cmd.start = untagged_addr(cmd.start);
+
 	if ((cmd.start & ~PAGE_MASK) != (cmd.hca_va & ~PAGE_MASK))
 		return -EINVAL;

I feel like we shouldn't thave to do this here, surely the cmd.start should flow unmodified to get_user_pages, and gup should untag it?

ie, this sort of direction for the IB code (this would be a giant patch, so I didn't have time to write it all, but I think it is much saner):

drivers/infiniband/core/umem.c +2 -2
index 54628ef879f0ce..7b3b736c87c253 100644
@@ -193,7 +193,7 @@ EXPORT_SYMBOL(ib_umem_find_best_pgsz);
  * @access: IB_ACCESS_xxx flags for memory being pinned
  * @dmasync: flush in-flight DMA when the memory region is written
  */
-struct ib_umem *ib_umem_get(struct ib_udata *udata, unsigned long addr,
+struct ib_umem *ib_umem_get(struct ib_udata *udata, void __user *addr,
 			    size_t size, int access, int dmasync)
 {
 	struct ib_ucontext *context;
@@ -201,7 +201,7 @@ struct ib_umem *ib_umem_get(struct ib_udata *udata, unsigned long addr,
 	struct page **page_list;
 	unsigned long lock_limit;
 	unsigned long new_pinned;
-	unsigned long cur_base;
+	void __user *cur_base;
 	struct mm_struct *mm;
 	unsigned long npages;
 	int ret;
drivers/infiniband/core/uverbs_cmd.c +2 -1
index 5a3a1780ceea4d..94389e7f12371f 100644
@@ -735,7 +735,8 @@ static int ib_uverbs_reg_mr(struct uverbs_attr_bundle *attrs)
 		}
 	}
 
-	mr = pd->device->ops.reg_user_mr(pd, cmd.start, cmd.length, cmd.hca_va,
+	mr = pd->device->ops.reg_user_mr(pd, u64_to_user_ptr(cmd.start),
+					 cmd.length, cmd.hca_va,
 					 cmd.access_flags,
 					 &attrs->driver_udata);
 	if (IS_ERR(mr)) {
drivers/infiniband/hw/mlx5/mr.c +3 -3
index 4d033796dcfcc2..bddbb952082fc5 100644
@@ -786,7 +786,7 @@ static int mr_cache_max_order(struct mlx5_ib_dev *dev)
 }
 
 static int mr_umem_get(struct mlx5_ib_dev *dev, struct ib_udata *udata,
-		       u64 start, u64 length, int access_flags,
+		       void __user *start, u64 length, int access_flags,
 		       struct ib_umem **umem, int *npages, int *page_shift,
 		       int *ncont, int *order)
 {
@@ -1262,8 +1262,8 @@ struct ib_mr *mlx5_ib_reg_dm_mr(struct ib_pd *pd, struct ib_dm *dm,
 				 attr->access_flags, mode);
 }
 
-struct ib_mr *mlx5_ib_reg_user_mr(struct ib_pd *pd, u64 start, u64 length,
-				  u64 virt_addr, int access_flags,
+struct ib_mr *mlx5_ib_reg_user_mr(struct ib_pd *pd, void __user *start,
+				  u64 length, u64 virt_addr, int access_flags,
 				  struct ib_udata *udata)
 {
 	struct mlx5_ib_dev *dev = to_mdev(pd->device);
include/rdma/ib_verbs.h +2 -2
index ec6446864b08e9..b3c8eaaa35c760 100644
@@ -2464,8 +2464,8 @@ struct ib_device_ops {
 	struct ib_mr *(*reg_user_mr)(struct ib_pd *pd, u64 start, u64 length,
 				     u64 virt_addr, int mr_access_flags,
 				     struct ib_udata *udata);
-	int (*rereg_user_mr)(struct ib_mr *mr, int flags, u64 start, u64 length,
-			     u64 virt_addr, int mr_access_flags,
+	int (*rereg_user_mr)(struct ib_mr *mr, int flags, void __user *start,
+			     u64 length, u64 virt_addr, int mr_access_flags,
 			     struct ib_pd *pd, struct ib_udata *udata);
 	int (*dereg_mr)(struct ib_mr *mr, struct ib_udata *udata);
 	struct ib_mr *(*alloc_mr)(struct ib_pd *pd, enum ib_mr_type mr_type,
#51 lore raw

Re: [PATCH v16 12/16] IB, arm64: untag user pointers in ib_uverbs_(re)reg_mr()

Quoted 30 lines · Jason Gunthorpe
Quoted 17 lines · Andrey Konovalov

This patch is a part of a series that extends arm64 kernel ABI to allow to pass tagged user pointers (with the top byte set to something else other than 0x00) as syscall arguments.

ib_uverbs_(re)reg_mr() use provided user pointers for vma lookups (through e.g. mlx4_get_umem_mr()), which can only by done with untagged pointers.

Untag user pointers in these functions.

 drivers/infiniband/core/uverbs_cmd.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/infiniband/core/uverbs_cmd.c +1 -0
index 5a3a1780ceea..f88ee733e617 100644
+++ b/drivers/infiniband/core/uverbs_cmd.c

drivers/infiniband/core/uverbs_cmd.c static int ib_uverbs_reg_mr(struct uverbs_attr_bundle *attrs) · line 715

Quoted 7 lines
drivers/infiniband/core/uverbs_cmd.c +2 -0
@@ -709,6 +709,8 @@ static int ib_uverbs_reg_mr(struct uverbs_attr_bundle *attrs)
      if (ret)
              return ret;
 
+     cmd.start = untagged_addr(cmd.start);
+
      if ((cmd.start & ~PAGE_MASK) != (cmd.hca_va & ~PAGE_MASK))
              return -EINVAL;

I feel like we shouldn't thave to do this here, surely the cmd.start should flow unmodified to get_user_pages, and gup should untag it?

Quoted 3 lines

ie, this sort of direction for the IB code (this would be a giant patch, so I didn't have time to write it all, but I think it is much saner):

Hi Jason,

ib_uverbs_reg_mr() passes cmd.start to mlx4_get_umem_mr(), which calls find_vma(), which only accepts untagged addresses. Could you explain how your patch helps?

Thanks!

Quoted 76 lines
drivers/infiniband/core/umem.c +2 -2
index 54628ef879f0ce..7b3b736c87c253 100644
@@ -193,7 +193,7 @@ EXPORT_SYMBOL(ib_umem_find_best_pgsz);
  * @access: IB_ACCESS_xxx flags for memory being pinned
  * @dmasync: flush in-flight DMA when the memory region is written
  */
-struct ib_umem *ib_umem_get(struct ib_udata *udata, unsigned long addr,
+struct ib_umem *ib_umem_get(struct ib_udata *udata, void __user *addr,
                            size_t size, int access, int dmasync)
 {
        struct ib_ucontext *context;
@@ -201,7 +201,7 @@ struct ib_umem *ib_umem_get(struct ib_udata *udata, unsigned long addr,
        struct page **page_list;
        unsigned long lock_limit;
        unsigned long new_pinned;
-       unsigned long cur_base;
+       void __user *cur_base;
        struct mm_struct *mm;
        unsigned long npages;
        int ret;
drivers/infiniband/core/uverbs_cmd.c +2 -1
index 5a3a1780ceea4d..94389e7f12371f 100644
@@ -735,7 +735,8 @@ static int ib_uverbs_reg_mr(struct uverbs_attr_bundle *attrs)
                }
        }
 
-       mr = pd->device->ops.reg_user_mr(pd, cmd.start, cmd.length, cmd.hca_va,
+       mr = pd->device->ops.reg_user_mr(pd, u64_to_user_ptr(cmd.start),
+                                        cmd.length, cmd.hca_va,
                                         cmd.access_flags,
                                         &attrs->driver_udata);
        if (IS_ERR(mr)) {
drivers/infiniband/hw/mlx5/mr.c +3 -3
index 4d033796dcfcc2..bddbb952082fc5 100644
@@ -786,7 +786,7 @@ static int mr_cache_max_order(struct mlx5_ib_dev *dev)
 }
 
 static int mr_umem_get(struct mlx5_ib_dev *dev, struct ib_udata *udata,
-                      u64 start, u64 length, int access_flags,
+                      void __user *start, u64 length, int access_flags,
                       struct ib_umem **umem, int *npages, int *page_shift,
                       int *ncont, int *order)
 {
@@ -1262,8 +1262,8 @@ struct ib_mr *mlx5_ib_reg_dm_mr(struct ib_pd *pd, struct ib_dm *dm,
                                 attr->access_flags, mode);
 }
 
-struct ib_mr *mlx5_ib_reg_user_mr(struct ib_pd *pd, u64 start, u64 length,
-                                 u64 virt_addr, int access_flags,
+struct ib_mr *mlx5_ib_reg_user_mr(struct ib_pd *pd, void __user *start,
+                                 u64 length, u64 virt_addr, int access_flags,
                                  struct ib_udata *udata)
 {
        struct mlx5_ib_dev *dev = to_mdev(pd->device);
include/rdma/ib_verbs.h +2 -2
index ec6446864b08e9..b3c8eaaa35c760 100644
@@ -2464,8 +2464,8 @@ struct ib_device_ops {
        struct ib_mr *(*reg_user_mr)(struct ib_pd *pd, u64 start, u64 length,
                                     u64 virt_addr, int mr_access_flags,
                                     struct ib_udata *udata);
-       int (*rereg_user_mr)(struct ib_mr *mr, int flags, u64 start, u64 length,
-                            u64 virt_addr, int mr_access_flags,
+       int (*rereg_user_mr)(struct ib_mr *mr, int flags, void __user *start,
+                            u64 length, u64 virt_addr, int mr_access_flags,
                             struct ib_pd *pd, struct ib_udata *udata);
        int (*dereg_mr)(struct ib_mr *mr, struct ib_udata *udata);
        struct ib_mr *(*alloc_mr)(struct ib_pd *pd, enum ib_mr_type mr_type,
#52 lore raw

Re: [PATCH v16 12/16] IB, arm64: untag user pointers in ib_uverbs_(re)reg_mr()

Quoted 35 lines · Andrey Konovalov
Quoted 30 lines · Jason Gunthorpe
Quoted 17 lines · Andrey Konovalov

This patch is a part of a series that extends arm64 kernel ABI to allow to pass tagged user pointers (with the top byte set to something else other than 0x00) as syscall arguments.

ib_uverbs_(re)reg_mr() use provided user pointers for vma lookups (through e.g. mlx4_get_umem_mr()), which can only by done with untagged pointers.

Untag user pointers in these functions.

 drivers/infiniband/core/uverbs_cmd.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/infiniband/core/uverbs_cmd.c +1 -0
index 5a3a1780ceea..f88ee733e617 100644
+++ b/drivers/infiniband/core/uverbs_cmd.c

drivers/infiniband/core/uverbs_cmd.c static int ib_uverbs_reg_mr(struct uverbs_attr_bundle *attrs) · line 715

Quoted 7 lines
drivers/infiniband/core/uverbs_cmd.c +2 -0
@@ -709,6 +709,8 @@ static int ib_uverbs_reg_mr(struct uverbs_attr_bundle *attrs)
      if (ret)
              return ret;
 
+     cmd.start = untagged_addr(cmd.start);
+
      if ((cmd.start & ~PAGE_MASK) != (cmd.hca_va & ~PAGE_MASK))
              return -EINVAL;

I feel like we shouldn't thave to do this here, surely the cmd.start should flow unmodified to get_user_pages, and gup should untag it?

Quoted 3 lines

ie, this sort of direction for the IB code (this would be a giant patch, so I didn't have time to write it all, but I think it is much saner):

Quoted 5 lines

Hi Jason,

ib_uverbs_reg_mr() passes cmd.start to mlx4_get_umem_mr(), which calls find_vma(), which only accepts untagged addresses. Could you explain how your patch helps?

That mlx4 is just a 'weird duck', it is not the normal flow, and I don't think the core code should be making special consideration for it.

Jason

#53 lore raw

Re: [PATCH v16 12/16] IB, arm64: untag user pointers in ib_uverbs_(re)reg_mr()

Quoted 39 lines · Jason Gunthorpe
Quoted 37 lines · Andrey Konovalov
Quoted 30 lines · Jason Gunthorpe
Quoted 17 lines · Andrey Konovalov

This patch is a part of a series that extends arm64 kernel ABI to allow to pass tagged user pointers (with the top byte set to something else other than 0x00) as syscall arguments.

ib_uverbs_(re)reg_mr() use provided user pointers for vma lookups (through e.g. mlx4_get_umem_mr()), which can only by done with untagged pointers.

Untag user pointers in these functions.

 drivers/infiniband/core/uverbs_cmd.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/infiniband/core/uverbs_cmd.c +1 -0
index 5a3a1780ceea..f88ee733e617 100644
+++ b/drivers/infiniband/core/uverbs_cmd.c

drivers/infiniband/core/uverbs_cmd.c static int ib_uverbs_reg_mr(struct uverbs_attr_bundle *attrs) · line 715

Quoted 7 lines
drivers/infiniband/core/uverbs_cmd.c +2 -0
@@ -709,6 +709,8 @@ static int ib_uverbs_reg_mr(struct uverbs_attr_bundle *attrs)
      if (ret)
              return ret;
 
+     cmd.start = untagged_addr(cmd.start);
+
      if ((cmd.start & ~PAGE_MASK) != (cmd.hca_va & ~PAGE_MASK))
              return -EINVAL;

I feel like we shouldn't thave to do this here, surely the cmd.start should flow unmodified to get_user_pages, and gup should untag it?

Quoted 3 lines

ie, this sort of direction for the IB code (this would be a giant patch, so I didn't have time to write it all, but I think it is much saner):

Hi Jason,

Quoted 7 lines
Quoted 3 lines

ib_uverbs_reg_mr() passes cmd.start to mlx4_get_umem_mr(), which calls find_vma(), which only accepts untagged addresses. Could you explain how your patch helps?

That mlx4 is just a 'weird duck', it is not the normal flow, and I don't think the core code should be making special consideration for it.

How do you think we should do untagging (or something else) to deal with this 'weird duck' case?

Quoted 2 lines

Jason

#54 lore raw

Re: [PATCH v16 12/16] IB, arm64: untag user pointers in ib_uverbs_(re)reg_mr()

Quoted 44 lines · Andrey Konovalov
Quoted 43 lines · Jason Gunthorpe
Quoted 40 lines · Andrey Konovalov
Quoted 30 lines · Jason Gunthorpe
Quoted 17 lines · Andrey Konovalov

This patch is a part of a series that extends arm64 kernel ABI to allow to pass tagged user pointers (with the top byte set to something else other than 0x00) as syscall arguments.

ib_uverbs_(re)reg_mr() use provided user pointers for vma lookups (through e.g. mlx4_get_umem_mr()), which can only by done with untagged pointers.

Untag user pointers in these functions.

 drivers/infiniband/core/uverbs_cmd.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/infiniband/core/uverbs_cmd.c +1 -0
index 5a3a1780ceea..f88ee733e617 100644
+++ b/drivers/infiniband/core/uverbs_cmd.c

drivers/infiniband/core/uverbs_cmd.c static int ib_uverbs_reg_mr(struct uverbs_attr_bundle *attrs) · line 715

Quoted 7 lines
drivers/infiniband/core/uverbs_cmd.c +2 -0
@@ -709,6 +709,8 @@ static int ib_uverbs_reg_mr(struct uverbs_attr_bundle *attrs)
      if (ret)
              return ret;
 
+     cmd.start = untagged_addr(cmd.start);
+
      if ((cmd.start & ~PAGE_MASK) != (cmd.hca_va & ~PAGE_MASK))
              return -EINVAL;

I feel like we shouldn't thave to do this here, surely the cmd.start should flow unmodified to get_user_pages, and gup should untag it?

Quoted 3 lines

ie, this sort of direction for the IB code (this would be a giant patch, so I didn't have time to write it all, but I think it is much saner):

Hi Jason,

ib_uverbs_reg_mr() passes cmd.start to mlx4_get_umem_mr(), which calls find_vma(), which only accepts untagged addresses. Could you explain how your patch helps?

Quoted 6 lines
Quoted 3 lines

That mlx4 is just a 'weird duck', it is not the normal flow, and I don't think the core code should be making special consideration for it.

How do you think we should do untagging (or something else) to deal with this 'weird duck' case?

mlx4 should handle it around the call to find_vma like other patches do, ideally as part of the cast from a void __user * to the unsigned long that find_vma needs

Jason

#55 lore raw

Re: [PATCH v16 12/16] IB, arm64: untag user pointers in ib_uverbs_(re)reg_mr()

Quoted 50 lines · Jason Gunthorpe
Quoted 48 lines · Andrey Konovalov
Quoted 46 lines · Jason Gunthorpe
Quoted 35 lines · Andrey Konovalov
Quoted 30 lines · Jason Gunthorpe
Quoted 17 lines · Andrey Konovalov

This patch is a part of a series that extends arm64 kernel ABI to allow to pass tagged user pointers (with the top byte set to something else other than 0x00) as syscall arguments.

ib_uverbs_(re)reg_mr() use provided user pointers for vma lookups (through e.g. mlx4_get_umem_mr()), which can only by done with untagged pointers.

Untag user pointers in these functions.

 drivers/infiniband/core/uverbs_cmd.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/infiniband/core/uverbs_cmd.c +1 -0
index 5a3a1780ceea..f88ee733e617 100644
+++ b/drivers/infiniband/core/uverbs_cmd.c

drivers/infiniband/core/uverbs_cmd.c static int ib_uverbs_reg_mr(struct uverbs_attr_bundle *attrs) · line 715

Quoted 7 lines
drivers/infiniband/core/uverbs_cmd.c +2 -0
@@ -709,6 +709,8 @@ static int ib_uverbs_reg_mr(struct uverbs_attr_bundle *attrs)
      if (ret)
              return ret;
 
+     cmd.start = untagged_addr(cmd.start);
+
      if ((cmd.start & ~PAGE_MASK) != (cmd.hca_va & ~PAGE_MASK))
              return -EINVAL;

I feel like we shouldn't thave to do this here, surely the cmd.start should flow unmodified to get_user_pages, and gup should untag it?

Quoted 3 lines

ie, this sort of direction for the IB code (this would be a giant patch, so I didn't have time to write it all, but I think it is much saner):

Quoted 5 lines

Hi Jason,

ib_uverbs_reg_mr() passes cmd.start to mlx4_get_umem_mr(), which calls find_vma(), which only accepts untagged addresses. Could you explain how your patch helps?

That mlx4 is just a 'weird duck', it is not the normal flow, and I don't think the core code should be making special consideration for it.

Quoted 6 lines
Quoted 2 lines

How do you think we should do untagging (or something else) to deal with this 'weird duck' case?

mlx4 should handle it around the call to find_vma like other patches do, ideally as part of the cast from a void __user * to the unsigned long that find_vma needs

So essentially what we had a few versions ago
(https://lkml.org/lkml/2019/4/30/785) plus changing unsigned longs to __user * across all IB code? I think the second part is something that's not related to this series and needs to be done separately. I can move untagging back to mlx4_get_umem_mr() though.

Catalin, you've initially asked to to move untagging out of mlx4_get_umem_mr(), do you have any comments on this?

Quoted 2 lines

Jason

maintainer
#56 lore raw

Re: [PATCH v16 12/16] IB, arm64: untag user pointers in ib_uverbs_(re)reg_mr()

Quoted 53 lines · Andrey Konovalov
Quoted 45 lines · Jason Gunthorpe
Quoted 44 lines · Andrey Konovalov
Quoted 42 lines · Jason Gunthorpe
Quoted 30 lines · Andrey Konovalov
Quoted 29 lines · Jason Gunthorpe
Quoted 17 lines · Andrey Konovalov

This patch is a part of a series that extends arm64 kernel ABI to allow to pass tagged user pointers (with the top byte set to something else other than 0x00) as syscall arguments.

ib_uverbs_(re)reg_mr() use provided user pointers for vma lookups (through e.g. mlx4_get_umem_mr()), which can only by done with untagged pointers.

Untag user pointers in these functions.

 drivers/infiniband/core/uverbs_cmd.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/infiniband/core/uverbs_cmd.c +1 -0
index 5a3a1780ceea..f88ee733e617 100644
+++ b/drivers/infiniband/core/uverbs_cmd.c

drivers/infiniband/core/uverbs_cmd.c static int ib_uverbs_reg_mr(struct uverbs_attr_bundle *attrs) · line 715

Quoted 7 lines
drivers/infiniband/core/uverbs_cmd.c +2 -0
@@ -709,6 +709,8 @@ static int ib_uverbs_reg_mr(struct uverbs_attr_bundle *attrs)
      if (ret)
              return ret;
 
+     cmd.start = untagged_addr(cmd.start);
+
      if ((cmd.start & ~PAGE_MASK) != (cmd.hca_va & ~PAGE_MASK))
              return -EINVAL;

I feel like we shouldn't thave to do this here, surely the cmd.start should flow unmodified to get_user_pages, and gup should untag it?

Quoted 7 lines
Quoted 3 lines

ie, this sort of direction for the IB code (this would be a giant patch, so I didn't have time to write it all, but I think it is much saner):

ib_uverbs_reg_mr() passes cmd.start to mlx4_get_umem_mr(), which calls find_vma(), which only accepts untagged addresses. Could you explain how your patch helps?

That mlx4 is just a 'weird duck', it is not the normal flow, and I don't think the core code should be making special consideration for it.

Quoted 6 lines
Quoted 2 lines

How do you think we should do untagging (or something else) to deal with this 'weird duck' case?

mlx4 should handle it around the call to find_vma like other patches do, ideally as part of the cast from a void __user * to the unsigned long that find_vma needs

Quoted 8 lines

So essentially what we had a few versions ago
(https://lkml.org/lkml/2019/4/30/785) plus changing unsigned longs to __user * across all IB code? I think the second part is something that's not related to this series and needs to be done separately. I can move untagging back to mlx4_get_umem_mr() though.

Catalin, you've initially asked to to move untagging out of mlx4_get_umem_mr(), do you have any comments on this?

It's fine by me either way. My original reasoning was to untag this at the higher level as tags may not be relevant to the mlx4 code. If that's what Jason prefers, go for it.

Signature · 1 line
Catalin
#57 lore raw

PATCH v16 13/16 media/v4l2-core, arm64: untag user pointers in videobuf_dma_contig_user_get

In reply to Andrey Konovalov

This patch is a part of a series that extends arm64 kernel ABI to allow to pass tagged user pointers (with the top byte set to something else other than 0x00) as syscall arguments.

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

Untag the pointers in this function.

---
 drivers/media/v4l2-core/videobuf-dma-contig.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
drivers/media/v4l2-core/videobuf-dma-contig.c +5 -4
index e1bf50df4c70..8a1ddd146b17 100644
@@ -160,6 +160,7 @@ static void videobuf_dma_contig_user_put(struct videobuf_dma_contig_memory *mem)
 static int videobuf_dma_contig_user_get(struct videobuf_dma_contig_memory *mem,
 					struct videobuf_buffer *vb)
 {
+	unsigned long untagged_baddr = untagged_addr(vb->baddr);
 	struct mm_struct *mm = current->mm;
 	struct vm_area_struct *vma;
 	unsigned long prev_pfn, this_pfn;
@@ -167,22 +168,22 @@ static int videobuf_dma_contig_user_get(struct videobuf_dma_contig_memory *mem,
 	unsigned int offset;
 	int ret;
 
-	offset = vb->baddr & ~PAGE_MASK;
+	offset = untagged_baddr & ~PAGE_MASK;
 	mem->size = PAGE_ALIGN(vb->size + offset);
 	ret = -EINVAL;
 
 	down_read(&mm->mmap_sem);
 
-	vma = find_vma(mm, vb->baddr);
+	vma = find_vma(mm, untagged_baddr);
 	if (!vma)
 		goto out_up;
 
-	if ((vb->baddr + mem->size) > vma->vm_end)
+	if ((untagged_baddr + mem->size) > vma->vm_end)
 		goto out_up;
 
 	pages_done = 0;
 	prev_pfn = 0; /* kill warning */
-	user_address = vb->baddr;
+	user_address = untagged_baddr;
 
 	while (pages_done < (mem->size >> PAGE_SHIFT)) {
 		ret = follow_pfn(vma, user_address, &this_pfn);
Signature · 1 line
2.22.0.rc1.311.g5d7573a151-goog
Reviewed-by on v16 13/16
#58 lore raw

Re: [PATCH v16 13/16] media/v4l2-core, arm64: untag user pointers in videobuf_dma_contig_user_get

Quoted 11 lines · Andrey Konovalov

This patch is a part of a series that extends arm64 kernel ABI to allow to pass tagged user pointers (with the top byte set to something else other than 0x00) as syscall arguments.

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

Untag the pointers in this function.

-Kees

Quoted 46 lines
---
 drivers/media/v4l2-core/videobuf-dma-contig.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
drivers/media/v4l2-core/videobuf-dma-contig.c +5 -4
index e1bf50df4c70..8a1ddd146b17 100644
@@ -160,6 +160,7 @@ static void videobuf_dma_contig_user_put(struct videobuf_dma_contig_memory *mem)
 static int videobuf_dma_contig_user_get(struct videobuf_dma_contig_memory *mem,
 					struct videobuf_buffer *vb)
 {
+	unsigned long untagged_baddr = untagged_addr(vb->baddr);
 	struct mm_struct *mm = current->mm;
 	struct vm_area_struct *vma;
 	unsigned long prev_pfn, this_pfn;
@@ -167,22 +168,22 @@ static int videobuf_dma_contig_user_get(struct videobuf_dma_contig_memory *mem,
 	unsigned int offset;
 	int ret;
 
-	offset = vb->baddr & ~PAGE_MASK;
+	offset = untagged_baddr & ~PAGE_MASK;
 	mem->size = PAGE_ALIGN(vb->size + offset);
 	ret = -EINVAL;
 
 	down_read(&mm->mmap_sem);
 
-	vma = find_vma(mm, vb->baddr);
+	vma = find_vma(mm, untagged_baddr);
 	if (!vma)
 		goto out_up;
 
-	if ((vb->baddr + mem->size) > vma->vm_end)
+	if ((untagged_baddr + mem->size) > vma->vm_end)
 		goto out_up;
 
 	pages_done = 0;
 	prev_pfn = 0; /* kill warning */
-	user_address = vb->baddr;
+	user_address = untagged_baddr;
 
 	while (pages_done < (mem->size >> PAGE_SHIFT)) {
 		ret = follow_pfn(vma, user_address, &this_pfn);
Signature · 1 line
2.22.0.rc1.311.g5d7573a151-goog
Signature · 5 lines
Kees Cook
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#59 lore raw

PATCH v16 14/16 tee, arm64: untag user pointers in tee_shm_register

In reply to Andrey Konovalov

This patch is a part of a series that extends arm64 kernel ABI to allow to pass tagged user pointers (with the top byte set to something else other than 0x00) as syscall arguments.

tee_shm_register()->optee_shm_unregister()->check_mem_type() uses provided user pointers for vma lookups (via __check_mem_type()), which can only by done with untagged pointers.

Untag user pointers in this function.

---
 drivers/tee/tee_shm.c | 1 +
 1 file changed, 1 insertion(+)
drivers/tee/tee_shm.c +1 -0
index 49fd7312e2aa..96945f4cefb8 100644
@@ -263,6 +263,7 @@ struct tee_shm *tee_shm_register(struct tee_context *ctx, unsigned long addr,
 	shm->teedev = teedev;
 	shm->ctx = ctx;
 	shm->id = -1;
+	addr = untagged_addr(addr);
 	start = rounddown(addr, PAGE_SIZE);
 	shm->offset = addr - start;
 	shm->size = length;
Signature · 1 line
2.22.0.rc1.311.g5d7573a151-goog
Acked-by on v16 14/16
#60 lore raw

Re: [PATCH v16 14/16] tee, arm64: untag user pointers in tee_shm_register

Quoted 5 lines · Andrey Konovalov

This patch is a part of a series that extends arm64 kernel ABI to allow to pass tagged user pointers (with the top byte set to something else other than 0x00) as syscall arguments.

Quoted 7 lines

tee_shm_register()->optee_shm_unregister()->check_mem_type() uses provided user pointers for vma lookups (via __check_mem_type()), which can only by done with untagged pointers.

Untag user pointers in this function.

Quoted 19 lines
---
 drivers/tee/tee_shm.c | 1 +
 1 file changed, 1 insertion(+)
drivers/tee/tee_shm.c +1 -1
index 49fd7312e2aa..96945f4cefb8 100644
@@ -263,6 +263,7 @@ struct tee_shm *tee_shm_register(struct tee_context *ctx, unsigned long addr,
        shm->teedev = teedev;
        shm->ctx = ctx;
        shm->id = -1;
+       addr = untagged_addr(addr);
        start = rounddown(addr, PAGE_SIZE);
        shm->offset = addr - start;
        shm->size = length;
--

2.22.0.rc1.311.g5d7573a151-goog

Reviewed-by on v16 14/16
#61 lore raw

Re: [PATCH v16 14/16] tee, arm64: untag user pointers in tee_shm_register

In reply to Andrey Konovalov
Quoted 11 lines · Andrey Konovalov

This patch is a part of a series that extends arm64 kernel ABI to allow to pass tagged user pointers (with the top byte set to something else other than 0x00) as syscall arguments.

tee_shm_register()->optee_shm_unregister()->check_mem_type() uses provided user pointers for vma lookups (via __check_mem_type()), which can only by done with untagged pointers.

Untag user pointers in this function.

"tee: shm: untag user pointers in tee_shm_register"

-Kees

Quoted 19 lines
---
 drivers/tee/tee_shm.c | 1 +
 1 file changed, 1 insertion(+)
drivers/tee/tee_shm.c +1 -0
index 49fd7312e2aa..96945f4cefb8 100644
@@ -263,6 +263,7 @@ struct tee_shm *tee_shm_register(struct tee_context *ctx, unsigned long addr,
 	shm->teedev = teedev;
 	shm->ctx = ctx;
 	shm->id = -1;
+	addr = untagged_addr(addr);
 	start = rounddown(addr, PAGE_SIZE);
 	shm->offset = addr - start;
 	shm->size = length;
Signature · 1 line
2.22.0.rc1.311.g5d7573a151-goog
Signature · 1 line
Kees Cook
#62 lore raw

PATCH v16 15/16 vfio/type1, arm64: untag user pointers in vaddr_get_pfn

In reply to Andrey Konovalov

This patch is a part of a series that extends arm64 kernel ABI to allow to pass tagged user pointers (with the top byte set to something else other than 0x00) as syscall arguments.

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

Untag user pointers in this function.

---
 drivers/vfio/vfio_iommu_type1.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/vfio/vfio_iommu_type1.c +2 -0
index 3ddc375e7063..528e39a1c2dd 100644
@@ -384,6 +384,8 @@ static int vaddr_get_pfn(struct mm_struct *mm, unsigned long vaddr,
 
 	down_read(&mm->mmap_sem);
 
+	vaddr = untagged_addr(vaddr);
+
 	vma = find_vma_intersection(mm, vaddr, vaddr + 1);
 
 	if (vma && vma->vm_flags & VM_PFNMAP) {
Signature · 1 line
2.22.0.rc1.311.g5d7573a151-goog
Reviewed-by on v16 15/16
#63 lore raw

Re: [PATCH v16 15/16] vfio/type1, arm64: untag user pointers in vaddr_get_pfn

Quoted 10 lines · Andrey Konovalov

This patch is a part of a series that extends arm64 kernel ABI to allow to pass tagged user pointers (with the top byte set to something else other than 0x00) as syscall arguments.

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

Untag user pointers in this function.

-Kees

Quoted 20 lines
---
 drivers/vfio/vfio_iommu_type1.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/vfio/vfio_iommu_type1.c +2 -0
index 3ddc375e7063..528e39a1c2dd 100644
@@ -384,6 +384,8 @@ static int vaddr_get_pfn(struct mm_struct *mm, unsigned long vaddr,
 
 	down_read(&mm->mmap_sem);
 
+	vaddr = untagged_addr(vaddr);
+
 	vma = find_vma_intersection(mm, vaddr, vaddr + 1);
 
 	if (vma && vma->vm_flags & VM_PFNMAP) {
Signature · 1 line
2.22.0.rc1.311.g5d7573a151-goog
Signature · 1 line
Kees Cook
maintainer Reviewed-by on v16 15/16
#64 lore raw

Re: [PATCH v16 15/16] vfio/type1, arm64: untag user pointers in vaddr_get_pfn

In reply to Andrey Konovalov
Quoted 10 lines · Andrey Konovalov

This patch is a part of a series that extends arm64 kernel ABI to allow to pass tagged user pointers (with the top byte set to something else other than 0x00) as syscall arguments.

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

Untag user pointers in this function.

#65 lore raw

PATCH v16 16/16 selftests, arm64: add a selftest for passing tagged pointers to kernel

In reply to Andrey Konovalov

This patch is a part of a series that extends arm64 kernel ABI to allow to pass tagged user pointers (with the top byte set to something else other than 0x00) as syscall arguments.

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.

---
 tools/testing/selftests/arm64/.gitignore      |  1 +
 tools/testing/selftests/arm64/Makefile        | 22 ++++++++++
 .../testing/selftests/arm64/run_tags_test.sh  | 12 ++++++
 tools/testing/selftests/arm64/tags_lib.c      | 42 +++++++++++++++++++
 tools/testing/selftests/arm64/tags_test.c     | 18 ++++++++
 5 files changed, 95 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_lib.c
 create mode 100644 tools/testing/selftests/arm64/tags_test.c
tools/testing/selftests/arm64/.gitignore new file +1 -0
new file mode 100644
index 000000000000..e8fae8d61ed6
@@ -0,0 +1 @@
+tags_test
tools/testing/selftests/arm64/Makefile new file +22 -0
new file mode 100644
index 000000000000..9dee18727923
@@ -0,0 +1,22 @@
+# SPDX-License-Identifier: GPL-2.0
+
+include ../lib.mk
+
+# 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_CUSTOM_PROGS := $(OUTPUT)/tags_test
+
+$(OUTPUT)/tags_test: tags_test.c $(OUTPUT)/tags_lib.so
+	$(CC) -o $@ $(CFLAGS) $(LDFLAGS) $<
+
+$(OUTPUT)/tags_lib.so: tags_lib.c
+	$(CC) -o $@ -shared $(CFLAGS) $(LDFLAGS) $^
+
+TEST_PROGS := run_tags_test.sh
+
+all: $(TEST_CUSTOM_PROGS)
+
+endif
tools/testing/selftests/arm64/run_tags_test.sh new file +12 -0
new file mode 100755
index 000000000000..2bbe0cd4220b
@@ -0,0 +1,12 @@
+#!/bin/sh
+# SPDX-License-Identifier: GPL-2.0
+
+echo "--------------------"
+echo "running tags test"
+echo "--------------------"
+LD_PRELOAD=./tags_lib.so ./tags_test
+if [ $? -ne 0 ]; then
+	echo "[FAIL]"
+else
+	echo "[PASS]"
+fi
tools/testing/selftests/arm64/tags_lib.c new file +42 -0

42 lines not on this page · raw message

tools/testing/selftests/arm64/tags_test.c new file +18 -0
new file mode 100644
index 000000000000..263b302874ed
@@ -0,0 +1,18 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <stdint.h>
+#include <sys/utsname.h>
+
+int main(void)
+{
+	struct utsname *ptr;
+	int err;
+
+	ptr = (struct utsname *)malloc(sizeof(*ptr));
+	err = uname(ptr);
+	free(ptr);
+	return err;
+}
Signature · 1 line
2.22.0.rc1.311.g5d7573a151-goog
Reviewed-by on v16 16/16
#66 lore raw

Re: [PATCH v16 16/16] selftests, arm64: add a selftest for passing tagged pointers to kernel

Quoted 9 lines · Andrey Konovalov

This patch is a part of a series that extends arm64 kernel ABI to allow to pass tagged user pointers (with the top byte set to something else other than 0x00) as syscall arguments.

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.

I'm adding Shuah to CC in case she has some suggestions about the new selftest.

-Kees

Quoted 141 lines
---
 tools/testing/selftests/arm64/.gitignore      |  1 +
 tools/testing/selftests/arm64/Makefile        | 22 ++++++++++
 .../testing/selftests/arm64/run_tags_test.sh  | 12 ++++++
 tools/testing/selftests/arm64/tags_lib.c      | 42 +++++++++++++++++++
 tools/testing/selftests/arm64/tags_test.c     | 18 ++++++++
 5 files changed, 95 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_lib.c
 create mode 100644 tools/testing/selftests/arm64/tags_test.c
tools/testing/selftests/arm64/.gitignore new file +1 -0
new file mode 100644
index 000000000000..e8fae8d61ed6
@@ -0,0 +1 @@
+tags_test
tools/testing/selftests/arm64/Makefile new file +22 -0
new file mode 100644
index 000000000000..9dee18727923
@@ -0,0 +1,22 @@
+# SPDX-License-Identifier: GPL-2.0
+
+include ../lib.mk
+
+# 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_CUSTOM_PROGS := $(OUTPUT)/tags_test
+
+$(OUTPUT)/tags_test: tags_test.c $(OUTPUT)/tags_lib.so
+	$(CC) -o $@ $(CFLAGS) $(LDFLAGS) $<
+
+$(OUTPUT)/tags_lib.so: tags_lib.c
+	$(CC) -o $@ -shared $(CFLAGS) $(LDFLAGS) $^
+
+TEST_PROGS := run_tags_test.sh
+
+all: $(TEST_CUSTOM_PROGS)
+
+endif
tools/testing/selftests/arm64/run_tags_test.sh new file +12 -0
new file mode 100755
index 000000000000..2bbe0cd4220b
@@ -0,0 +1,12 @@
+#!/bin/sh
+# SPDX-License-Identifier: GPL-2.0
+
+echo "--------------------"
+echo "running tags test"
+echo "--------------------"
+LD_PRELOAD=./tags_lib.so ./tags_test
+if [ $? -ne 0 ]; then
+	echo "[FAIL]"
+else
+	echo "[PASS]"
+fi
tools/testing/selftests/arm64/tags_lib.c new file +42 -0
new file mode 100644
index 000000000000..8a674509216e
@@ -0,0 +1,42 @@
+#include <stdlib.h>
+
+#define TAG_SHIFT	(56)
+#define TAG_MASK	(0xffUL << TAG_SHIFT)
+
+void *__libc_malloc(size_t size);
+void __libc_free(void *ptr);
+void *__libc_realloc(void *ptr, size_t size);
+void *__libc_calloc(size_t nmemb, size_t size);
+
+static void *tag_ptr(void *ptr)
+{
+	unsigned long tag = rand() & 0xff;
+	if (!ptr)
+		return ptr;
+	return (void *)((unsigned long)ptr | (tag << TAG_SHIFT));
+}
+
+static void *untag_ptr(void *ptr)
+{
+	return (void *)((unsigned long)ptr & ~TAG_MASK);
+}
+
+void *malloc(size_t size)
+{
+	return tag_ptr(__libc_malloc(size));
+}
+
+void free(void *ptr)
+{
+	__libc_free(untag_ptr(ptr));
+}
+
+void *realloc(void *ptr, size_t size)
+{
+	return tag_ptr(__libc_realloc(untag_ptr(ptr), size));
+}
+
+void *calloc(size_t nmemb, size_t size)
+{
+	return tag_ptr(__libc_calloc(nmemb, size));
+}
tools/testing/selftests/arm64/tags_test.c new file +18 -0
new file mode 100644
index 000000000000..263b302874ed
@@ -0,0 +1,18 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <stdint.h>
+#include <sys/utsname.h>
+
+int main(void)
+{
+	struct utsname *ptr;
+	int err;
+
+	ptr = (struct utsname *)malloc(sizeof(*ptr));
+	err = uname(ptr);
+	free(ptr);
+	return err;
+}
Signature · 1 line
2.22.0.rc1.311.g5d7573a151-goog
Signature · 1 line
Kees Cook
maintainer Acked-by Shuah Khan
#67 lore raw

Re: [PATCH v16 16/16] selftests, arm64: add a selftest for passing tagged pointers to kernel

Quoted 5 lines · Kees Cook
Quoted 4 lines · Andrey Konovalov

This patch is a part of a series that extends arm64 kernel ABI to allow to pass tagged user pointers (with the top byte set to something else other than 0x00) as syscall arguments.

Quoted 8 lines
Quoted 5 lines

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.

I'm adding Shuah to CC in case she has some suggestions about the new selftest.

Thanks Kees.

Quoted 5 lines

-Kees

Looks good to me.

thanks,
-- Shuah

maintainer
#68 lore raw

Re: [PATCH v16 16/16] selftests, arm64: add a selftest for passing tagged pointers to kernel

In reply to Andrey Konovalov
Quoted 9 lines · Andrey Konovalov

This patch is a part of a series that extends arm64 kernel ABI to allow to pass tagged user pointers (with the top byte set to something else other than 0x00) as syscall arguments.

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.

BTW, you could add

since I wrote the malloc() etc. hooks.

tools/testing/selftests/arm64/tags_lib.c line 17

Quoted 7 lines
tools/testing/selftests/arm64/tags_lib.c new file +7 -0
+static void *tag_ptr(void *ptr)
+{
+	unsigned long tag = rand() & 0xff;
+	if (!ptr)
+		return ptr;
+	return (void *)((unsigned long)ptr | (tag << TAG_SHIFT));
+}

With the prctl() option, this function becomes (if you have a better idea, fine by me):

----------8<---------------
#include <stdlib.h>
#include <sys/prctl.h>

#define TAG_SHIFT (56)
#define TAG_MASK (0xffUL << TAG_SHIFT)

#define PR_SET_TAGGED_ADDR_CTRL 55
#define PR_GET_TAGGED_ADDR_CTRL 56

# define PR_TAGGED_ADDR_ENABLE		(1UL << 0)

void *__libc_malloc(size_t size);
void __libc_free(void *ptr);
void *__libc_realloc(void *ptr, size_t size);
void *__libc_calloc(size_t nmemb, size_t size);

static void *tag_ptr(void *ptr)

{
	static int tagged_addr_err = 1;
	unsigned long tag = 0;

	if (tagged_addr_err == 1)
		tagged_addr_err = prctl(PR_SET_TAGGED_ADDR_CTRL,
					PR_TAGGED_ADDR_ENABLE, 0, 0, 0);

	if (!ptr)
		return ptr;
	if (!tagged_addr_err)
		tag = rand() & 0xff;

	return (void *)((unsigned long)ptr | (tag << TAG_SHIFT));
}
Signature · 1 line
Catalin
#69 lore raw

Re: [PATCH v16 16/16] selftests, arm64: add a selftest for passing tagged pointers to kernel

Quoted 10 lines · Catalin Marinas
Quoted 8 lines · Andrey Konovalov

This patch is a part of a series that extends arm64 kernel ABI to allow to pass tagged user pointers (with the top byte set to something else other than 0x00) as syscall arguments.

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.

Quoted 7 lines
Quoted 1 line

BTW, you could add

since I wrote the malloc() etc. hooks.

Sure!

Quoted 29 lines

tools/testing/selftests/arm64/tags_lib.c line 17

Quoted 9 lines
tools/testing/selftests/arm64/tags_lib.c new file +7 -0
+static void *tag_ptr(void *ptr)
+{
+     unsigned long tag = rand() & 0xff;
+     if (!ptr)
+             return ptr;
+     return (void *)((unsigned long)ptr | (tag << TAG_SHIFT));
+}

With the prctl() option, this function becomes (if you have a better idea, fine by me):

----------8<---------------
#include <stdlib.h>
#include <sys/prctl.h>

#define TAG_SHIFT (56)
#define TAG_MASK (0xffUL << TAG_SHIFT)

#define PR_SET_TAGGED_ADDR_CTRL         55
#define PR_GET_TAGGED_ADDR_CTRL         56
# define PR_TAGGED_ADDR_ENABLE          (1UL << 0)

void *__libc_malloc(size_t size);
void __libc_free(void *ptr);
void *__libc_realloc(void *ptr, size_t size);
void *__libc_calloc(size_t nmemb, size_t size);

Quoted 8 lines

static void *tag_ptr(void *ptr)

{
        static int tagged_addr_err = 1;
        unsigned long tag = 0;

        if (tagged_addr_err == 1)
                tagged_addr_err = prctl(PR_SET_TAGGED_ADDR_CTRL,
                                        PR_TAGGED_ADDR_ENABLE, 0, 0, 0);

I think this requires atomics. malloc() can be called from multiple threads.

Quoted 11 lines
        if (!ptr)
                return ptr;
        if (!tagged_addr_err)
                tag = rand() & 0xff;

        return (void *)((unsigned long)ptr | (tag << TAG_SHIFT));
}

--
Catalin

maintainer
#70 lore raw

Re: [PATCH v16 16/16] selftests, arm64: add a selftest for passing tagged pointers to kernel

Quoted 11 lines · Andrey Konovalov
Quoted 8 lines · Catalin Marinas

static void *tag_ptr(void *ptr)

{
        static int tagged_addr_err = 1;
        unsigned long tag = 0;

        if (tagged_addr_err == 1)
                tagged_addr_err = prctl(PR_SET_TAGGED_ADDR_CTRL,
                                        PR_TAGGED_ADDR_ENABLE, 0, 0, 0);

I think this requires atomics. malloc() can be called from multiple threads.

It's slightly racy but I assume in a real libc it can be initialised earlier than the hook calls while still in single-threaded mode (I had a quick attempt with __attribute__((constructor)) but didn't get far).

Even with the race, under normal circumstances calling the prctl() twice is not a problem. I think the risk here is that someone disables the ABI via sysctl and the ABI is enabled for some of the threads only.

Signature · 1 line
Catalin
#71 lore raw

Re: [PATCH v16 16/16] selftests, arm64: add a selftest for passing tagged pointers to kernel

Quoted 14 lines · Catalin Marinas
Quoted 11 lines · Andrey Konovalov
Quoted 8 lines · Catalin Marinas

static void *tag_ptr(void *ptr)

{
        static int tagged_addr_err = 1;
        unsigned long tag = 0;

        if (tagged_addr_err == 1)
                tagged_addr_err = prctl(PR_SET_TAGGED_ADDR_CTRL,
                                        PR_TAGGED_ADDR_ENABLE, 0, 0, 0);

I think this requires atomics. malloc() can be called from multiple threads.

Quoted 7 lines

It's slightly racy but I assume in a real libc it can be initialised earlier than the hook calls while still in single-threaded mode (I had a quick attempt with __attribute__((constructor)) but didn't get far).

Even with the race, under normal circumstances calling the prctl() twice is not a problem. I think the risk here is that someone disables the ABI via sysctl and the ABI is enabled for some of the threads only.

OK, I'll keep the code racy, but add a comment pointing it out. Thanks!

Quoted 3 lines

--
Catalin