[PATCH 3/3] vfs: make mounts and mountstats honor root dir like mountinfo does

2013-01-29 Thread Dmitry V. Levin
Change show_vfsmnt() and show_vfsstat() to show mountpoints relative
to the root directory and skip mountpoints outside of chroot jail
the same way as show_mountinfo() does.

Signed-off-by: Dmitry V. Levin 
---
 fs/proc_namespace.c | 13 +++--
 1 file changed, 11 insertions(+), 2 deletions(-)

diff --git a/fs/proc_namespace.c b/fs/proc_namespace.c
index 10aa92d..2033f74 100644
--- a/fs/proc_namespace.c
+++ b/fs/proc_namespace.c
@@ -91,6 +91,7 @@ static void show_type(struct seq_file *m, struct super_block 
*sb)
 
 static int show_vfsmnt(struct seq_file *m, struct vfsmount *mnt)
 {
+   struct proc_mounts *p = proc_mounts(m);
struct mount *r = real_mount(mnt);
int err = 0;
struct path mnt_path = { .dentry = mnt->mnt_root, .mnt = mnt };
@@ -104,7 +105,10 @@ static int show_vfsmnt(struct seq_file *m, struct vfsmount 
*mnt)
mangle(m, r->mnt_devname ? r->mnt_devname : "none");
}
seq_putc(m, ' ');
-   seq_path(m, &mnt_path, " \t\n\\");
+   /* mountpoints outside of chroot jail will give SEQ_SKIP on this */
+   err = seq_path_root(m, &mnt_path, &p->root, " \t\n\\");
+   if (err)
+   goto out;
seq_putc(m, ' ');
show_type(m, sb);
seq_puts(m, __mnt_is_readonly(mnt) ? " ro" : " rw");
@@ -181,6 +185,7 @@ out:
 
 static int show_vfsstat(struct seq_file *m, struct vfsmount *mnt)
 {
+   struct proc_mounts *p = proc_mounts(m);
struct mount *r = real_mount(mnt);
struct path mnt_path = { .dentry = mnt->mnt_root, .mnt = mnt };
struct super_block *sb = mnt_path.dentry->d_sb;
@@ -200,7 +205,10 @@ static int show_vfsstat(struct seq_file *m, struct 
vfsmount *mnt)
 
/* mount point */
seq_puts(m, " mounted on ");
-   seq_path(m, &mnt_path, " \t\n\\");
+   /* mountpoints outside of chroot jail will give SEQ_SKIP on this */
+   err = seq_path_root(m, &mnt_path, &p->root, " \t\n\\");
+   if (err)
+   goto out;
seq_putc(m, ' ');
 
/* file system type */
@@ -215,6 +223,7 @@ static int show_vfsstat(struct seq_file *m, struct vfsmount 
*mnt)
}
 
seq_putc(m, '\n');
+out:
return err;
 }
 
-- 
ldv
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


[PATCH 1/3] Documentation: update seq_file

2013-01-29 Thread Dmitry V. Levin
Starting with commit v3.2-rc4-1-g02125a8, seq_path_root() no longer
changes the value of root.
Starting with commit v3.2-rc7-104-g8c9379e, some arguments of seq_path()
and seq_path_root() are const.

Signed-off-by: Dmitry V. Levin 
---
 Documentation/filesystems/seq_file.txt | 12 ++--
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/Documentation/filesystems/seq_file.txt 
b/Documentation/filesystems/seq_file.txt
index a1e2e0d..5367f08 100644
--- a/Documentation/filesystems/seq_file.txt
+++ b/Documentation/filesystems/seq_file.txt
@@ -189,16 +189,16 @@ which is in the string esc will be represented in octal 
form in the output.
 
 There is also a pair of functions for printing filenames:
 
-   int seq_path(struct seq_file *m, struct path *path, char *esc);
-   int seq_path_root(struct seq_file *m, struct path *path,
- struct path *root, char *esc)
+   int seq_path(struct seq_file *m, const struct path *path,
+const char *esc);
+   int seq_path_root(struct seq_file *m, const struct path *path,
+ const struct path *root, const char *esc)
 
 Here, path indicates the file of interest, and esc is a set of characters
 which should be escaped in the output.  A call to seq_path() will output
 the path relative to the current process's filesystem root.  If a different
-root is desired, it can be used with seq_path_root().  Note that, if it
-turns out that path cannot be reached from root, the value of root will be
-changed in seq_file_root() to a root which *does* work.
+root is desired, it can be used with seq_path_root().  If it turns out that
+path cannot be reached from root, seq_path_root() returns SEQ_SKIP.
 
 
 Making it all work

-- 
ldv
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


[PATCH 2/3] vfs: cleanup show_mountinfo

2013-01-29 Thread Dmitry V. Levin
Starting with commit v3.2-rc4-1-g02125a8, seq_path_root() no longer
changes the value of its "struct path *root" argument.
Starting with commit v3.2-rc7-104-g8c9379e, the "struct path *root"
argument of seq_path_root() is const.
As result, the temporary variable "root" in show_mountinfo() that
holds a copy of struct path root is no longer needed.

Signed-off-by: Dmitry V. Levin 
---
 fs/proc_namespace.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/fs/proc_namespace.c b/fs/proc_namespace.c
index 5fe34c3..10aa92d 100644
--- a/fs/proc_namespace.c
+++ b/fs/proc_namespace.c
@@ -125,7 +125,6 @@ static int show_mountinfo(struct seq_file *m, struct 
vfsmount *mnt)
struct mount *r = real_mount(mnt);
struct super_block *sb = mnt->mnt_sb;
struct path mnt_path = { .dentry = mnt->mnt_root, .mnt = mnt };
-   struct path root = p->root;
int err = 0;
 
seq_printf(m, "%i %i %u:%u ", r->mnt_id, r->mnt_parent->mnt_id,
@@ -139,7 +138,7 @@ static int show_mountinfo(struct seq_file *m, struct 
vfsmount *mnt)
seq_putc(m, ' ');
 
/* mountpoints outside of chroot jail will give SEQ_SKIP on this */
-   err = seq_path_root(m, &mnt_path, &root, " \t\n\\");
+   err = seq_path_root(m, &mnt_path, &p->root, " \t\n\\");
if (err)
goto out;
 
-- 
ldv
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


[PATCH 2/3] vfs: cleanup show_mountinfo

2012-10-17 Thread Dmitry V. Levin
Starting with commit v3.2-rc4-1-g02125a8, seq_path_root() no longer
changes the value of its "struct path *root" argument.
Starting with commit v3.2-rc7-104-g8c9379e, the "struct path *root"
argument of seq_path_root() is const.
As result, the temporary variable "root" in show_mountinfo() that
holds a copy of struct path root is no longer needed.

Signed-off-by: Dmitry V. Levin 
---
 fs/proc_namespace.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/fs/proc_namespace.c b/fs/proc_namespace.c
index 5fe34c3..10aa92d 100644
--- a/fs/proc_namespace.c
+++ b/fs/proc_namespace.c
@@ -125,7 +125,6 @@ static int show_mountinfo(struct seq_file *m, struct 
vfsmount *mnt)
struct mount *r = real_mount(mnt);
struct super_block *sb = mnt->mnt_sb;
struct path mnt_path = { .dentry = mnt->mnt_root, .mnt = mnt };
-   struct path root = p->root;
int err = 0;
 
seq_printf(m, "%i %i %u:%u ", r->mnt_id, r->mnt_parent->mnt_id,
@@ -139,7 +138,7 @@ static int show_mountinfo(struct seq_file *m, struct 
vfsmount *mnt)
seq_putc(m, ' ');
 
/* mountpoints outside of chroot jail will give SEQ_SKIP on this */
-   err = seq_path_root(m, &mnt_path, &root, " \t\n\\");
+   err = seq_path_root(m, &mnt_path, &p->root, " \t\n\\");
if (err)
goto out;
 
-- 
ldv
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


[PATCH 3/3] vfs: make mounts and mountstats honor root dir like mountinfo does

2012-10-17 Thread Dmitry V. Levin
Change show_vfsmnt() and show_vfsstat() to show mountpoints relative
to the root directory and skip mountpoints outside of chroot jail
the same way as show_mountinfo() does.

Signed-off-by: Dmitry V. Levin 
---
 fs/proc_namespace.c | 13 +++--
 1 file changed, 11 insertions(+), 2 deletions(-)

diff --git a/fs/proc_namespace.c b/fs/proc_namespace.c
index 10aa92d..2033f74 100644
--- a/fs/proc_namespace.c
+++ b/fs/proc_namespace.c
@@ -91,6 +91,7 @@ static void show_type(struct seq_file *m, struct super_block 
*sb)
 
 static int show_vfsmnt(struct seq_file *m, struct vfsmount *mnt)
 {
+   struct proc_mounts *p = proc_mounts(m);
struct mount *r = real_mount(mnt);
int err = 0;
struct path mnt_path = { .dentry = mnt->mnt_root, .mnt = mnt };
@@ -104,7 +105,10 @@ static int show_vfsmnt(struct seq_file *m, struct vfsmount 
*mnt)
mangle(m, r->mnt_devname ? r->mnt_devname : "none");
}
seq_putc(m, ' ');
-   seq_path(m, &mnt_path, " \t\n\\");
+   /* mountpoints outside of chroot jail will give SEQ_SKIP on this */
+   err = seq_path_root(m, &mnt_path, &p->root, " \t\n\\");
+   if (err)
+   goto out;
seq_putc(m, ' ');
show_type(m, sb);
seq_puts(m, __mnt_is_readonly(mnt) ? " ro" : " rw");
@@ -181,6 +185,7 @@ out:
 
 static int show_vfsstat(struct seq_file *m, struct vfsmount *mnt)
 {
+   struct proc_mounts *p = proc_mounts(m);
struct mount *r = real_mount(mnt);
struct path mnt_path = { .dentry = mnt->mnt_root, .mnt = mnt };
struct super_block *sb = mnt_path.dentry->d_sb;
@@ -200,7 +205,10 @@ static int show_vfsstat(struct seq_file *m, struct 
vfsmount *mnt)
 
/* mount point */
seq_puts(m, " mounted on ");
-   seq_path(m, &mnt_path, " \t\n\\");
+   /* mountpoints outside of chroot jail will give SEQ_SKIP on this */
+   err = seq_path_root(m, &mnt_path, &p->root, " \t\n\\");
+   if (err)
+   goto out;
seq_putc(m, ' ');
 
/* file system type */
@@ -215,6 +223,7 @@ static int show_vfsstat(struct seq_file *m, struct vfsmount 
*mnt)
}
 
seq_putc(m, '\n');
+out:
return err;
 }
 
-- 
ldv
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


[PATCH 1/3] Documentation: update seq_file

2012-10-17 Thread Dmitry V. Levin
Starting with commit v3.2-rc4-1-g02125a8, seq_path_root() no longer
changes the value of root.
Starting with commit v3.2-rc7-104-g8c9379e, some arguments of seq_path()
and seq_path_root() are const.

Signed-off-by: Dmitry V. Levin 
---
 Documentation/filesystems/seq_file.txt | 12 ++--
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/Documentation/filesystems/seq_file.txt 
b/Documentation/filesystems/seq_file.txt
index a1e2e0d..5367f08 100644
--- a/Documentation/filesystems/seq_file.txt
+++ b/Documentation/filesystems/seq_file.txt
@@ -189,16 +189,16 @@ which is in the string esc will be represented in octal 
form in the output.
 
 There is also a pair of functions for printing filenames:
 
-   int seq_path(struct seq_file *m, struct path *path, char *esc);
-   int seq_path_root(struct seq_file *m, struct path *path,
- struct path *root, char *esc)
+   int seq_path(struct seq_file *m, const struct path *path,
+const char *esc);
+   int seq_path_root(struct seq_file *m, const struct path *path,
+ const struct path *root, const char *esc)
 
 Here, path indicates the file of interest, and esc is a set of characters
 which should be escaped in the output.  A call to seq_path() will output
 the path relative to the current process's filesystem root.  If a different
-root is desired, it can be used with seq_path_root().  Note that, if it
-turns out that path cannot be reached from root, the value of root will be
-changed in seq_file_root() to a root which *does* work.
+root is desired, it can be used with seq_path_root().  If it turns out that
+path cannot be reached from root, seq_path_root() returns SEQ_SKIP.
 
 
 Making it all work

-- 
ldv
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


[PATCH] uapi: fix linux/kfd_ioctl.h userspace compilation errors

2017-11-12 Thread Dmitry V. Levin
Consistently use types provided by  via 
to fix the following linux/kfd_ioctl.h userspace compilation errors:

/usr/include/linux/kfd_ioctl.h:236:2: error: unknown type name 'uint64_t'
  uint64_t va_addr; /* to KFD */
/usr/include/linux/kfd_ioctl.h:237:2: error: unknown type name 'uint32_t'
  uint32_t gpu_id; /* to KFD */
/usr/include/linux/kfd_ioctl.h:238:2: error: unknown type name 'uint32_t'
  uint32_t pad;
/usr/include/linux/kfd_ioctl.h:243:2: error: unknown type name 'uint64_t'
  uint64_t tile_config_ptr;
/usr/include/linux/kfd_ioctl.h:245:2: error: unknown type name 'uint64_t'
  uint64_t macro_tile_config_ptr;
/usr/include/linux/kfd_ioctl.h:249:2: error: unknown type name 'uint32_t'
  uint32_t num_tile_configs;
/usr/include/linux/kfd_ioctl.h:253:2: error: unknown type name 'uint32_t'
  uint32_t num_macro_tile_configs;
/usr/include/linux/kfd_ioctl.h:255:2: error: unknown type name 'uint32_t'
  uint32_t gpu_id;  /* to KFD */
/usr/include/linux/kfd_ioctl.h:256:2: error: unknown type name 'uint32_t'
  uint32_t gb_addr_config; /* from KFD */
/usr/include/linux/kfd_ioctl.h:257:2: error: unknown type name 'uint32_t'
  uint32_t num_banks;  /* from KFD */
/usr/include/linux/kfd_ioctl.h:258:2: error: unknown type name 'uint32_t'
  uint32_t num_ranks;  /* from KFD */

Fixes: 6a1c9510694fe ("drm/amdkfd: Adding new IOCTL for scratch memory v2")
Fixes: 5d71dbc3a5886 ("drm/amdkfd: Implement image tiling mode support v2")
Signed-off-by: Dmitry V. Levin 
---
 include/uapi/linux/kfd_ioctl.h | 22 +++---
 1 file changed, 11 insertions(+), 11 deletions(-)

diff --git a/include/uapi/linux/kfd_ioctl.h b/include/uapi/linux/kfd_ioctl.h
index 26283fefdf5f..f7015aa12347 100644
--- a/include/uapi/linux/kfd_ioctl.h
+++ b/include/uapi/linux/kfd_ioctl.h
@@ -233,29 +233,29 @@ struct kfd_ioctl_wait_events_args {
 };
 
 struct kfd_ioctl_set_scratch_backing_va_args {
-   uint64_t va_addr;   /* to KFD */
-   uint32_t gpu_id;/* to KFD */
-   uint32_t pad;
+   __u64 va_addr;  /* to KFD */
+   __u32 gpu_id;   /* to KFD */
+   __u32 pad;
 };
 
 struct kfd_ioctl_get_tile_config_args {
/* to KFD: pointer to tile array */
-   uint64_t tile_config_ptr;
+   __u64 tile_config_ptr;
/* to KFD: pointer to macro tile array */
-   uint64_t macro_tile_config_ptr;
+   __u64 macro_tile_config_ptr;
/* to KFD: array size allocated by user mode
 * from KFD: array size filled by kernel
 */
-   uint32_t num_tile_configs;
+   __u32 num_tile_configs;
/* to KFD: array size allocated by user mode
 * from KFD: array size filled by kernel
 */
-   uint32_t num_macro_tile_configs;
+   __u32 num_macro_tile_configs;
 
-   uint32_t gpu_id;/* to KFD */
-   uint32_t gb_addr_config;/* from KFD */
-   uint32_t num_banks; /* from KFD */
-   uint32_t num_ranks; /* from KFD */
+   __u32 gpu_id;   /* to KFD */
+   __u32 gb_addr_config;   /* from KFD */
+   __u32 num_banks;/* from KFD */
+   __u32 num_ranks;/* from KFD */
/* struct size can be extended later if needed
 * without breaking ABI compatibility
 */
-- 
ldv


[PATCH] uapi: fix linux/rxrpc.h userspace compilation errors

2017-11-12 Thread Dmitry V. Levin
Consistently use types provided by  to fix the following
linux/rxrpc.h userspace compilation errors:

/usr/include/linux/rxrpc.h:24:2: error: unknown type name 'u16'
  u16  srx_service; /* service desired */
/usr/include/linux/rxrpc.h:25:2: error: unknown type name 'u16'
  u16  transport_type; /* type of transport socket (SOCK_DGRAM) */
/usr/include/linux/rxrpc.h:26:2: error: unknown type name 'u16'
  u16  transport_len; /* length of transport address */

Use __kernel_sa_family_t instead of sa_family_t the same way
as uapi/linux/in.h does, to fix the following
linux/rxrpc.h userspace compilation errors:

/usr/include/linux/rxrpc.h:23:2: error: unknown type name 'sa_family_t'
  sa_family_t srx_family; /* address family */
/usr/include/linux/rxrpc.h:28:3: error: unknown type name 'sa_family_t'
  sa_family_t family;  /* transport address family */

Fixes: 727f8914477e ("rxrpc: Expose UAPI definitions to userspace")
Cc:  # v4.14
Signed-off-by: Dmitry V. Levin 
---
 include/uapi/linux/rxrpc.h | 10 +-
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/include/uapi/linux/rxrpc.h b/include/uapi/linux/rxrpc.h
index 9656aad8f8f7..9d4afea308a4 100644
--- a/include/uapi/linux/rxrpc.h
+++ b/include/uapi/linux/rxrpc.h
@@ -20,12 +20,12 @@
  * RxRPC socket address
  */
 struct sockaddr_rxrpc {
-   sa_family_t srx_family; /* address family */
-   u16 srx_service;/* service desired */
-   u16 transport_type; /* type of transport socket 
(SOCK_DGRAM) */
-   u16 transport_len;  /* length of transport address */
+   __kernel_sa_family_tsrx_family; /* address family */
+   __u16   srx_service;/* service desired */
+   __u16   transport_type; /* type of transport socket 
(SOCK_DGRAM) */
+   __u16   transport_len;  /* length of transport address 
*/
union {
-   sa_family_t family; /* transport address family */
+   __kernel_sa_family_t family;/* transport address family */
struct sockaddr_in sin; /* IPv4 transport address */
struct sockaddr_in6 sin6;   /* IPv6 transport address */
} transport;
-- 
ldv


[PATCH] uapi: fix linux/tls.h userspace compilation error

2017-11-13 Thread Dmitry V. Levin
Move inclusion of a private kernel header 
from uapi/linux/tls.h to its only user - net/tls.h,
to fix the following linux/tls.h userspace compilation error:

/usr/include/linux/tls.h:41:21: fatal error: net/tcp.h: No such file or 
directory

As to this point uapi/linux/tls.h was totaly unusuable for userspace,
cleanup this header file further by moving other redundant includes
to net/tls.h.

Fixes: 3c4d7559159b ("tls: kernel TLS support")
Cc:  # v4.13+
Signed-off-by: Dmitry V. Levin 
---
 include/net/tls.h| 4 
 include/uapi/linux/tls.h | 4 
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/include/net/tls.h b/include/net/tls.h
index b89d397dd62f..c06db1eadac2 100644
--- a/include/net/tls.h
+++ b/include/net/tls.h
@@ -35,6 +35,10 @@
 #define _TLS_OFFLOAD_H
 
 #include 
+#include 
+#include 
+#include 
+#include 
 
 #include 
 
diff --git a/include/uapi/linux/tls.h b/include/uapi/linux/tls.h
index d5e0682ab837..293b2cdad88d 100644
--- a/include/uapi/linux/tls.h
+++ b/include/uapi/linux/tls.h
@@ -35,10 +35,6 @@
 #define _UAPI_LINUX_TLS_H
 
 #include 
-#include 
-#include 
-#include 
-#include 
 
 /* TLS socket options */
 #define TLS_TX 1   /* Set transmit parameters */

-- 
ldv


[PATCH] uapi: fix sound/skl-tplg-interface.h userspace compilation errors

2018-08-13 Thread Dmitry V. Levin
Include  and consistently use types it provides
to fix the following sound/skl-tplg-interface.h userspace compilation errors:

/usr/include/sound/skl-tplg-interface.h:146:2: error: unknown type name 'u32'
  u32 set_params:2;
/usr/include/sound/skl-tplg-interface.h:147:2: error: unknown type name 'u32'
  u32 rsvd:30;
/usr/include/sound/skl-tplg-interface.h:148:2: error: unknown type name 'u32'
  u32 param_id;
/usr/include/sound/skl-tplg-interface.h:149:2: error: unknown type name 'u32'
  u32 max;
/usr/include/sound/skl-tplg-interface.h:166:2: error: unknown type name 'u16'
  u16 module_id;
/usr/include/sound/skl-tplg-interface.h:167:2: error: unknown type name 'u16'
  u16 instance_id;
/usr/include/sound/skl-tplg-interface.h:171:2: error: unknown type name 'u32'
  u32 channels;
/usr/include/sound/skl-tplg-interface.h:172:2: error: unknown type name 'u32'
  u32 freq;
/usr/include/sound/skl-tplg-interface.h:173:2: error: unknown type name 'u32'
  u32 bit_depth;
/usr/include/sound/skl-tplg-interface.h:174:2: error: unknown type name 'u32'
  u32 valid_bit_depth;
/usr/include/sound/skl-tplg-interface.h:175:2: error: unknown type name 'u32'
  u32 ch_cfg;
/usr/include/sound/skl-tplg-interface.h:176:2: error: unknown type name 'u32'
  u32 interleaving_style;
/usr/include/sound/skl-tplg-interface.h:177:2: error: unknown type name 'u32'
  u32 sample_type;
/usr/include/sound/skl-tplg-interface.h:178:2: error: unknown type name 'u32'
  u32 ch_map;
/usr/include/sound/skl-tplg-interface.h:182:2: error: unknown type name 'u32'
  u32 set_params:2;
/usr/include/sound/skl-tplg-interface.h:183:2: error: unknown type name 'u32'
  u32 rsvd:30;
/usr/include/sound/skl-tplg-interface.h:184:2: error: unknown type name 'u32'
  u32 param_id;
/usr/include/sound/skl-tplg-interface.h:185:2: error: unknown type name 'u32'
  u32 caps_size;
/usr/include/sound/skl-tplg-interface.h:186:2: error: unknown type name 'u32'
  u32 caps[HDA_SST_CFG_MAX];
/usr/include/sound/skl-tplg-interface.h:190:2: error: unknown type name 'u8'
  u8 pipe_id;
/usr/include/sound/skl-tplg-interface.h:191:2: error: unknown type name 'u8'
  u8 pipe_priority;
/usr/include/sound/skl-tplg-interface.h:192:2: error: unknown type name 'u16'
  u16 conn_type:4;
/usr/include/sound/skl-tplg-interface.h:193:2: error: unknown type name 'u16'
  u16 rsvd:4;
/usr/include/sound/skl-tplg-interface.h:194:2: error: unknown type name 'u16'
  u16 memory_pages:8;
/usr/include/sound/skl-tplg-interface.h:200:2: error: unknown type name 'u16'
  u16 module_id;
/usr/include/sound/skl-tplg-interface.h:201:2: error: unknown type name 'u16'
  u16 instance_id;
/usr/include/sound/skl-tplg-interface.h:202:2: error: unknown type name 'u32'
  u32 max_mcps;
/usr/include/sound/skl-tplg-interface.h:203:2: error: unknown type name 'u32'
  u32 mem_pages;
/usr/include/sound/skl-tplg-interface.h:204:2: error: unknown type name 'u32'
  u32 obs;
/usr/include/sound/skl-tplg-interface.h:205:2: error: unknown type name 'u32'
  u32 ibs;
/usr/include/sound/skl-tplg-interface.h:206:2: error: unknown type name 'u32'
  u32 vbus_id;
/usr/include/sound/skl-tplg-interface.h:208:2: error: unknown type name 'u32'
  u32 max_in_queue:8;
/usr/include/sound/skl-tplg-interface.h:209:2: error: unknown type name 'u32'
  u32 max_out_queue:8;
/usr/include/sound/skl-tplg-interface.h:210:2: error: unknown type name 'u32'
  u32 time_slot:8;
/usr/include/sound/skl-tplg-interface.h:211:2: error: unknown type name 'u32'
  u32 core_id:4;
/usr/include/sound/skl-tplg-interface.h:212:2: error: unknown type name 'u32'
  u32 rsvd1:4;
/usr/include/sound/skl-tplg-interface.h:214:2: error: unknown type name 'u32'
  u32 module_type:8;
/usr/include/sound/skl-tplg-interface.h:215:2: error: unknown type name 'u32'
  u32 conn_type:4;
/usr/include/sound/skl-tplg-interface.h:216:2: error: unknown type name 'u32'
  u32 dev_type:4;
/usr/include/sound/skl-tplg-interface.h:217:2: error: unknown type name 'u32'
  u32 hw_conn_type:4;
/usr/include/sound/skl-tplg-interface.h:218:2: error: unknown type name 'u32'
  u32 rsvd2:12;
/usr/include/sound/skl-tplg-interface.h:220:2: error: unknown type name 'u32'
  u32 params_fixup:8;
/usr/include/sound/skl-tplg-interface.h:221:2: error: unknown type name 'u32'
  u32 converter:8;
/usr/include/sound/skl-tplg-interface.h:222:2: error: unknown type name 'u32'
  u32 input_pin_type:1;
/usr/include/sound/skl-tplg-interface.h:223:2: error: unknown type name 'u32'
  u32 output_pin_type:1;
/usr/include/sound/skl-tplg-interface.h:224:2: error: unknown type name 'u32'
  u32 is_

[PATCH] uapi: fix linux/usb/audio.h userspace compilation error

2018-08-13 Thread Dmitry V. Levin
Replace NULL with 0 in uac_mixer_unit_bmControls() to fix the following
linux/usb/audio.h userspace compilation error:

/usr/include/linux/usb/audio.h: In function 'uac_mixer_unit_bmControls':
/usr/include/linux/usb/audio.h:304:10: error: 'NULL' undeclared (first use in 
this function)
   return NULL;

Fixes: 6cfd839ae78e ("ALSA: usb-audio: UAC3. Add support for mixer unit.")
Cc:  # v4.18
Signed-off-by: Dmitry V. Levin 
---
 include/uapi/linux/usb/audio.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/include/uapi/linux/usb/audio.h b/include/uapi/linux/usb/audio.h
index 74e520fb944f..2b61fe34b2ca 100644
--- a/include/uapi/linux/usb/audio.h
+++ b/include/uapi/linux/usb/audio.h
@@ -301,7 +301,7 @@ static inline __u8 *uac_mixer_unit_bmControls(struct 
uac_mixer_unit_descriptor *
case UAC_VERSION_3:
return &desc->baSourceID[desc->bNrInPins + 2];
default:
-   return NULL;
+   return 0;
}
 }
 
-- 
ldv


Re: [PATCH] uapi: fix linux/usb/audio.h userspace compilation error

2018-08-13 Thread Dmitry V. Levin
On Mon, Aug 13, 2018 at 05:55:25PM +0200, Takashi Iwai wrote:
> On Mon, 13 Aug 2018 17:46:51 +0200, Dmitry V. Levin wrote:
> > 
> > Replace NULL with 0 in uac_mixer_unit_bmControls() to fix the following
> > linux/usb/audio.h userspace compilation error:
> > 
> > /usr/include/linux/usb/audio.h: In function 'uac_mixer_unit_bmControls':
> > /usr/include/linux/usb/audio.h:304:10: error: 'NULL' undeclared (first use 
> > in this function)
> >return NULL;
> > 
> > Fixes: 6cfd839ae78e ("ALSA: usb-audio: UAC3. Add support for mixer unit.")
> > Cc:  # v4.18
> > Signed-off-by: Dmitry V. Levin 
> 
> Hrm, can we include the standard header for definition of NULL
> instead?  It's way too ugly to use 0 just for that.

It's fine to return 0 as a null pointer.  If you prefer NULL,
please include a header that won't break userspace,
or just move the function out of UAPI.

Thanks,


-- 
ldv


signature.asc
Description: PGP signature


Re: sparc/ppc/arm compat siginfo ABI regressions: sending SIGFPE via kill() returns wrong values in si_pid and si_uid

2018-04-11 Thread Dmitry V. Levin
Hi,

On Mon, Apr 09, 2018 at 06:22:53PM +0300, Dmitry V. Levin wrote:
> There seems to be a regression in v4.16 on ppc compat very similar
> to sparc compat regression reported earlier at
> https://marc.info/?l=linux-sparc&m=151501500704383 .
> 
> The symptoms are exactly the same: the same signal_receive test from
> the strace test suite fails with the same diagnostics:
> https://build.opensuse.org/public/build/home:ldv_alt/openSUSE_Factory_PowerPC/ppc/strace/_log

The log is big, just look for "KERNEL BUG".

> Unfortunately, I do not have any means to investigate further,
> so just passing this information on to those who care.

OK, the faulty commit is v4.16-rc1~159^2~39
("signal/powerpc: Document conflicts with SI_USER and SIGFPE and SIGTRAP").

One might think that a commit called "Document conflicts" shouldn't
introduce an ABI regression, but this one definitely does by defining
FPE_FIXME and TRAP_FIXME in arch/powerpc/include/uapi/asm/siginfo.h
that affect siginfo_layout().

A similar commit v4.16-rc1~159^2~37
("signal/arm: Document conflicts with SI_USER and SIGFPE") must have
introduced a similar ABI regression to compat arm.

An earlier commit v4.14-rc1~60^2^2~5
("signal/sparc: Document a conflict with SI_USER with SIGFPE") introduced
a similar ABI regression to compat sparc.

There is a clear pattern of sneaking in ABI changes using innocently
looking commit messages.


-- 
ldv


signature.asc
Description: PGP signature


Re: sparc/ppc/arm compat siginfo ABI regressions: sending SIGFPE via kill() returns wrong values in si_pid and si_uid

2018-04-12 Thread Dmitry V. Levin
On Thu, Apr 12, 2018 at 10:58:11AM +0100, Russell King - ARM Linux wrote:
> On Thu, Apr 12, 2018 at 04:34:35AM +0300, Dmitry V. Levin wrote:
> > A similar commit v4.16-rc1~159^2~37
> > ("signal/arm: Document conflicts with SI_USER and SIGFPE") must have
> > introduced a similar ABI regression to compat arm.
> 
> So, could you explain how can this change cause a regression?
> 
> +#define FPE_FIXME  0
> -   vfp_raise_sigfpe(0, regs);
> +   vfp_raise_sigfpe(FPE_FIXME, regs);

No, this hunk hasn't caused the regression, but another one did:

diff --git a/arch/arm/include/uapi/asm/siginfo.h 
b/arch/arm/include/uapi/asm/siginfo.h
new file mode 100644
index 000..d051388
--- /dev/null
+++ b/arch/arm/include/uapi/asm/siginfo.h
@@ -0,0 +1,13 @@
+#ifndef __ASM_SIGINFO_H
+#define __ASM_SIGINFO_H
+
+#include 
+
+/*
+ * SIGFPE si_codes
+ */
+#ifdef __KERNEL__
+#define FPE_FIXME  0   /* Broken dup of SI_USER */
+#endif /* __KERNEL__ */
+
+#endif

This is due to FPE_FIXME handling in kernel/signal.c


-- 
ldv


signature.asc
Description: PGP signature


Re: sparc/ppc/arm compat siginfo ABI regressions: sending SIGFPE via kill() returns wrong values in si_pid and si_uid

2018-04-12 Thread Dmitry V. Levin
On Thu, Apr 12, 2018 at 01:19:49PM +0100, Russell King - ARM Linux wrote:
> On Thu, Apr 12, 2018 at 02:03:14PM +0300, Dmitry V. Levin wrote:
> > On Thu, Apr 12, 2018 at 10:58:11AM +0100, Russell King - ARM Linux wrote:
> > > On Thu, Apr 12, 2018 at 04:34:35AM +0300, Dmitry V. Levin wrote:
> > > > A similar commit v4.16-rc1~159^2~37
> > > > ("signal/arm: Document conflicts with SI_USER and SIGFPE") must have
> > > > introduced a similar ABI regression to compat arm.
> > > 
> > > So, could you explain how can this change cause a regression?
> > > 
> > > +#define FPE_FIXME  0
> > > -   vfp_raise_sigfpe(0, regs);
> > > +   vfp_raise_sigfpe(FPE_FIXME, regs);
> > 
> > No, this hunk hasn't caused the regression, but another one did:
> > 
> > diff --git a/arch/arm/include/uapi/asm/siginfo.h 
> > b/arch/arm/include/uapi/asm/siginfo.h
> > new file mode 100644
> > index 000..d051388
> > --- /dev/null
> > +++ b/arch/arm/include/uapi/asm/siginfo.h
> > @@ -0,0 +1,13 @@
> > +#ifndef __ASM_SIGINFO_H
> > +#define __ASM_SIGINFO_H
> > +
> > +#include 
> > +
> > +/*
> > + * SIGFPE si_codes
> > + */
> > +#ifdef __KERNEL__
> > +#define FPE_FIXME  0   /* Broken dup of SI_USER */
> > +#endif /* __KERNEL__ */
> > +
> > +#endif
> > 
> > This is due to FPE_FIXME handling in kernel/signal.c
> 
> Building strace 4.22 on ARM and running the test suite reveals no
> problems with the signal_receive test, tested on both 4.14 and 4.16
> kernels - there's no "KERNEL BUG" reports in any of the test results.

https://build.opensuse.org/public/build/home:ldv_alt/openSUSE_Factory_ARM/armv7l/strace/_log
- the test just fails there with
[   50s] + uname -a
[   50s] Linux armbuild01 4.16.0-1-lpae #1 SMP PREEMPT Wed Apr 4 13:35:56 UTC 
2018 (e16f96d) armv7l armv7l armv7l GNU/Linux
...
[  570s] FAIL: signal_receive.gen
[  570s]  SIGFPE {si_signo=SIGFPE, si_code=SI_USER, si_pid=25332, 
si_uid=399} ---
[  570s] +--- SIGFPE {si_signo=SIGFPE, si_code=SI_USER, si_pid=25332, si_uid=0} 
---
[  570s] signal_receive.gen.test: failed test: ../../strace -a16 -e trace=kill 
../signal_receive output mismatch

> However, stock strace 4.22 source doesn't appear to contain the
> "KERNEL BUG" string anywhere, so this may be a Suse specific addition
> to the test:

The "KERNEL BUG" diagnostics I was talking about was added to strace yesterday
as a part of workaround commit, see
https://github.com/strace/strace/commit/34c7794cc16e2511eda7b1d5767c655a83b17309
Before that change the test just failed.

[...]
> Any ideas where the "KERNEL BUG" in Suse builds is coming from?

strace developers use OBS to test strace.git for regressions.
The build environment is provided by OBS, all the rest comes from strace.git.

> Any ideas how to test it on other architectures (iow, where can we get
> source that contains this test?)

Just use master branch of https://github.com/strace/strace
or https://gitlab.com/strace/strace (they are the same).

> Based on previous experience, unfortunately folk don't tend to report
> user ABI regressions to kernel developers, so we'd probably never know
> that there's a problem - I do think the safer thing would've been to
> leave it well alone, and just accept that we'll end up copying more
> words to userspace than is actually intended.

Well, these changes caused visible regressions in strace test suite on arm, ppc,
and sparc - this is the reason why I have reported them to kernel developers.


-- 
ldv


signature.asc
Description: PGP signature


Re: sparc/ppc/arm compat siginfo ABI regressions: sending SIGFPE via kill() returns wrong values in si_pid and si_uid

2018-04-12 Thread Dmitry V. Levin
On Thu, Apr 12, 2018 at 09:50:26AM -0700, Linus Torvalds wrote:
> Does this attached patch perhaps fix the ARM case?
> 
> It just uses FPE_FLTUNK as the default si_code for SIGFPE, which seems
> sane enough. And then gets rid of FPE_FIXME, which should resolve the
> nasty case.
> 
> Hmm? Entirely untested, and I didn't really look at the test-case in
> question since I can't really run it anyway.
> 
> Well, I could run it all on x86-64, but it doesn't have that FPE_FIXME
> case at all.
> 
>  Linus

>  arch/arm/include/uapi/asm/siginfo.h | 7 ---
>  arch/arm/vfp/vfpmodule.c| 4 ++--
>  2 files changed, 2 insertions(+), 9 deletions(-)
> 
> diff --git a/arch/arm/include/uapi/asm/siginfo.h 
> b/arch/arm/include/uapi/asm/siginfo.h
> index d0513880be21..d87beeedb4c4 100644
> --- a/arch/arm/include/uapi/asm/siginfo.h
> +++ b/arch/arm/include/uapi/asm/siginfo.h
> @@ -3,11 +3,4 @@
>  
>  #include 
>  
> -/*
> - * SIGFPE si_codes
> - */
> -#ifdef __KERNEL__
> -#define FPE_FIXME0   /* Broken dup of SI_USER */
> -#endif /* __KERNEL__ */
> -
>  #endif

Looks like the whole file should go away.

> diff --git a/arch/arm/vfp/vfpmodule.c b/arch/arm/vfp/vfpmodule.c
> index 4c375e11ae95..012c6e690303 100644
> --- a/arch/arm/vfp/vfpmodule.c
> +++ b/arch/arm/vfp/vfpmodule.c
> @@ -251,13 +251,13 @@ static void vfp_panic(char *reason, u32 inst)
>   */
>  static void vfp_raise_exceptions(u32 exceptions, u32 inst, u32 fpscr, struct 
> pt_regs *regs)
>  {
> - int si_code = 0;
> + int si_code = FPE_FLTUNK;

Note that this change would affect the following code
at the end of vfp_raise_exceptions:

if (si_code)
vfp_raise_sigfpe(si_code, regs);

>   pr_debug("VFP: raising exceptions %08x\n", exceptions);
>  
>   if (exceptions == VFP_EXCEPTION_ERROR) {
>   vfp_panic("unhandled bounce", inst);
> - vfp_raise_sigfpe(FPE_FIXME, regs);
> + vfp_raise_sigfpe(si_code, regs);
>   return;
>   }
>  

To be on the safe side, I'd just change it this way:

diff --git a/arch/arm/vfp/vfpmodule.c b/arch/arm/vfp/vfpmodule.c
index 4c375e1..66a73ba 100644
--- a/arch/arm/vfp/vfpmodule.c
+++ b/arch/arm/vfp/vfpmodule.c
@@ -257,7 +257,7 @@ static void vfp_raise_exceptions(u32 exceptions, u32 inst, 
u32 fpscr, struct pt_
 
if (exceptions == VFP_EXCEPTION_ERROR) {
vfp_panic("unhandled bounce", inst);
-   vfp_raise_sigfpe(FPE_FIXME, regs);
+   vfp_raise_sigfpe(FPE_FLTUNK, regs);
return;
}

-- 
ldv


signature.asc
Description: PGP signature


[PATCH] sparc: fix compat siginfo ABI regression

2018-04-13 Thread Dmitry V. Levin
Starting with commit v4.14-rc1~60^2^2~1, a SIGFPE signal sent via kill
results to wrong values in si_pid and si_uid fields of compat siginfo_t.

This happens due to FPE_FIXME being defined to 0 for sparc, and at the
same time siginfo_layout() introduced by the same commit returns
SIL_FAULT for SIGFPE if si_code == SI_USER and FPE_FIXME is defined to 0.

Fix this regression by removing FPE_FIXME macro and changing all its users
to assign FPE_FLTUNK to si_code instead of FPE_FIXME.

Note that FPE_FLTUNK is a new macro introduced by commit
266da65e9156d93e1126e185259a4aae68188d0e.

Tested with commit v4.16-11958-g16e205cf42da.

This bug was found by strace test suite.

Link: https://github.com/strace/strace/issues/21
Fixes: cc731525f26a ("signal: Remove kernel interal si_code magic")
Thanks-to: Anatoly Pugachev 
Signed-off-by: Dmitry V. Levin 
---
 arch/sparc/include/uapi/asm/siginfo.h | 7 ---
 arch/sparc/kernel/traps_32.c  | 2 +-
 arch/sparc/kernel/traps_64.c  | 2 +-
 3 files changed, 2 insertions(+), 9 deletions(-)

diff --git a/arch/sparc/include/uapi/asm/siginfo.h 
b/arch/sparc/include/uapi/asm/siginfo.h
index 896ce44..e704955 100644
--- a/arch/sparc/include/uapi/asm/siginfo.h
+++ b/arch/sparc/include/uapi/asm/siginfo.h
@@ -18,13 +18,6 @@
 #define SI_NOINFO  32767   /* no information in siginfo_t */
 
 /*
- * SIGFPE si_codes
- */
-#ifdef __KERNEL__
-#define FPE_FIXME  0   /* Broken dup of SI_USER */
-#endif /* __KERNEL__ */
-
-/*
  * SIGEMT si_codes
  */
 #define EMT_TAGOVF 1   /* tag overflow */
diff --git a/arch/sparc/kernel/traps_32.c b/arch/sparc/kernel/traps_32.c
index b1ed763..33cd35b 100644
--- a/arch/sparc/kernel/traps_32.c
+++ b/arch/sparc/kernel/traps_32.c
@@ -307,7 +307,7 @@ void do_fpe_trap(struct pt_regs *regs, unsigned long pc, 
unsigned long npc,
info.si_errno = 0;
info.si_addr = (void __user *)pc;
info.si_trapno = 0;
-   info.si_code = FPE_FIXME;
+   info.si_code = FPE_FLTUNK;
if ((fsr & 0x1c000) == (1 << 14)) {
if (fsr & 0x10)
info.si_code = FPE_FLTINV;
diff --git a/arch/sparc/kernel/traps_64.c b/arch/sparc/kernel/traps_64.c
index 462a21a..e81072a 100644
--- a/arch/sparc/kernel/traps_64.c
+++ b/arch/sparc/kernel/traps_64.c
@@ -2372,7 +2372,7 @@ static void do_fpe_common(struct pt_regs *regs)
info.si_errno = 0;
info.si_addr = (void __user *)regs->tpc;
info.si_trapno = 0;
-   info.si_code = FPE_FIXME;
+   info.si_code = FPE_FLTUNK;
if ((fsr & 0x1c000) == (1 << 14)) {
if (fsr & 0x10)
info.si_code = FPE_FLTINV;
-- 
ldv


ppc compat v4.16 regression: sending SIGTRAP or SIGFPE via kill() returns wrong values in si_pid and si_uid

2018-04-09 Thread Dmitry V. Levin
Hi,

There seems to be a regression in v4.16 on ppc compat very similar
to sparc compat regression reported earlier at
https://marc.info/?l=linux-sparc&m=151501500704383 .

The symptoms are exactly the same: the same signal_receive test from
the strace test suite fails with the same diagnostics:
https://build.opensuse.org/public/build/home:ldv_alt/openSUSE_Factory_PowerPC/ppc/strace/_log

Unfortunately, I do not have any means to investigate further,
so just passing this information on to those who care.


-- 
ldv


signature.asc
Description: PGP signature


Re: [RFC PATCH v2] ptrace: add PTRACE_GET_SYSCALL_INFO request

2018-11-22 Thread Dmitry V. Levin
On Thu, Nov 22, 2018 at 06:55:29AM -0800, Andy Lutomirski wrote:
> On Wed, Nov 21, 2018 at 3:56 PM Dmitry V. Levin wrote:
> > On Wed, Nov 21, 2018 at 02:56:57PM -0800, Andy Lutomirski wrote:
> > > Please cc linux-...@vger.kernel.org for future versions.
> > >
> > > On Wed, Nov 21, 2018 at 7:58 AM Elvira Khabirova wrote:
> > > >
> > > > struct ptrace_syscall_info {
> > > > __u8 op; /* 0 for entry, 1 for exit */
> > >
> > > Can you add proper defines, like:
> > >
> > > #define PTRACE_SYSCALL_ENTRY 0
> > > #define PTRACE_SYSCALL_EXIT 1
> > > #define PTRACE_SYSCALL_SECCOMP 2
> > >
> > > and make seccomp work from the start?  I'd rather we don't merge an
> > > implementation that doesn't work for seccomp and then have to rework
> > > it later.
> >
> > What's the difference between PTRACE_EVENT_SECCOMP and syscall-entry-stop
> > with regards to PTRACE_GET_SYSCALL_INFO request?  At least they have the
> > same entry_info to return.
> 
> I'm not sure there's any material difference.

In that case we don't really need PTRACE_SYSCALL_SECCOMP: op field
describes the structure inside the union to use, not the ptrace stop.

> > As long as implementation (ab)uses ptrace_message to tell one kind of stop
> > from another, it can distinguish syscall-entry-stop and syscall-exit-stop
> > from each other and from many other kinds of stops, but it cannot
> > distinguish PTRACE_EVENT_SECCOMP from e.g. PTRACE_EVENT_EXIT.
> 
> Hmm.  PTRACE_GET_SYSCALL_INFO should fail for PTRACE_EVENT_EXIT, I think.

Unless we can change PTRACE_EVENT_SECCOMP to set some higher bits of
ptrace_message (beyond SECCOMP_RET_DATA) which is very unlikely because
it would qualify as an ABI change, this would require an additional field
in struct task_struct because ptrace_message wouldn't be enough
to distinguish PTRACE_EVENT_SECCOMP from PTRACE_EVENT_EXIT.


-- 
ldv


signature.asc
Description: PGP signature


Re: [RFC PATCH v2] ptrace: add PTRACE_GET_SYSCALL_INFO request

2018-11-22 Thread Dmitry V. Levin
On Thu, Nov 22, 2018 at 04:19:10PM -0800, Andy Lutomirski wrote:
> On Thu, Nov 22, 2018 at 11:15 AM Dmitry V. Levin  wrote:
> >
> > On Thu, Nov 22, 2018 at 06:55:29AM -0800, Andy Lutomirski wrote:
> > > On Wed, Nov 21, 2018 at 3:56 PM Dmitry V. Levin wrote:
> > > > On Wed, Nov 21, 2018 at 02:56:57PM -0800, Andy Lutomirski wrote:
> > > > > Please cc linux-...@vger.kernel.org for future versions.
> > > > >
> > > > > On Wed, Nov 21, 2018 at 7:58 AM Elvira Khabirova wrote:
> > > > > >
> > > > > > struct ptrace_syscall_info {
> > > > > > __u8 op; /* 0 for entry, 1 for exit */
> > > > >
> > > > > Can you add proper defines, like:
> > > > >
> > > > > #define PTRACE_SYSCALL_ENTRY 0
> > > > > #define PTRACE_SYSCALL_EXIT 1
> > > > > #define PTRACE_SYSCALL_SECCOMP 2
> > > > >
> > > > > and make seccomp work from the start?  I'd rather we don't merge an
> > > > > implementation that doesn't work for seccomp and then have to rework
> > > > > it later.
> > > >
> > > > What's the difference between PTRACE_EVENT_SECCOMP and 
> > > > syscall-entry-stop
> > > > with regards to PTRACE_GET_SYSCALL_INFO request?  At least they have the
> > > > same entry_info to return.
> > >
> > > I'm not sure there's any material difference.
> >
> > In that case we don't really need PTRACE_SYSCALL_SECCOMP: op field
> > describes the structure inside the union to use, not the ptrace stop.
> 
> Unless we think the structures might diverge in the future.

If these structures ever diverge, then a seccomp structure will be added
to the union, and a portable userspace code will likely look this way:

#include 
...
struct ptrace_syscall_info info;
long rc = ptrace(PTRACE_GET_SYSCALL_INFO, pid, (void *) sizeof(info), &info);
...
switch (info.op) {
case PTRACE_SYSCALL_INFO_ENTRY:
/* handle info.entry */
case PTRACE_SYSCALL_INFO_EXIT:
/* handle info.exit */
#ifdef PTRACE_SYSCALL_INFO_SECCOMP
case PTRACE_SYSCALL_INFO_SECCOMP:
/* handle info.seccomp */
#endif
default:
/* handle unknown info.op */
}

In other words, it would be better if PTRACE_SYSCALL_INFO_* selector
constants were introduced along with corresponding structures in the
union.


-- 
ldv


signature.asc
Description: PGP signature


Re: Official Linux system wrapper library?

2018-11-23 Thread Dmitry V. Levin
On Fri, Nov 23, 2018 at 12:15:39PM -0800, Daniel Colascione wrote:
> On Fri, Nov 23, 2018 at 5:34 AM Florian Weimer wrote:
> > > On Mon, Nov 12, 2018 at 12:11 AM, Florian Weimer wrote:
> > >>
> > >>> If the kernel provides a system call, libc should provide a C wrapper
> > >>> for it, even if in the opinion of the libc maintainers, that system
> > >>> call is flawed.
> > >>
> > >> It's not that simple, I think.  What about bdflush?  socketcall?
> > >> getxpid?  osf_gettimeofday?  set_robust_list?
> > >
> > > What about them? Mentioning that these system calls exist is not in
> > > itself an argument.
> >
> > But socketcall does not exist on all architectures.  Neither does
> > getpid, it's called getxpid on some architectures.
> 
> So what? On systems on which a given system call does not exist,
> attempts to link against that system call should fail, or attempts to
> make that system call should fail at runtime with ENOSYS. That's
> completely expected and unsurprising behavior, not some unavoidable
> source of catastrophic confusion.

I'm sorry but you've just said that getpid() must either be unavailable or
fail on those architectures that provide no syscall with exactly the same
semantics as getpid syscall.  Nobody is going to use a libc that doesn't
provide getpid() in a reliable way.

If you really need a 1-1 correspondence between syscalls and C wrappers,
there is syscall(3) with all associated portability issues.

If you need something else, please be more specific, i.e. be ready to give
a detailed answer about every syscall ever supported by the kernel,
on every supported architecture.

My first trivial question is, do you need C wrappers for
__NR_epoll_create, __NR_eventfd, __NR_inotify_init,
and __NR_signalfd syscalls?


-- 
ldv


signature.asc
Description: PGP signature


Re: [PATCH RESEND v3 1/3] ptrace: pass type of a syscall-stop in ptrace_message

2018-11-24 Thread Dmitry V. Levin
On Sat, Nov 24, 2018 at 03:54:02PM -1000, Joey Pabalinas wrote:
> On Sun, Nov 25, 2018 at 02:22:27AM +0100, Elvira Khabirova wrote:
> > Define two constants, PTRACE_EVENTMSG_SYSCALL_ENTRY and
> > PTRACE_EVENTMSG_SYSCALL_EXIT, and place them in ptrace_message
> > for the duration of syscall-stops.
> > This way ptracers can distinguish syscall-enter-stops
> > from syscall-exit-stops using PTRACE_GETEVENTMSG request.
> 
> Is there an advantage to using two constants instead of a single
> sys_exit bit (set/unset for syscall-exit-stop/syscall-enter-stop)?

Given that without this patch the value returned by PTRACE_GETEVENTMSG
during syscall stop is undefined, we need two different ptrace_message
values that cannot be set by other ptrace events to enable reliable
identification of syscall-enter-stop and syscall-exit-stop in userspace:
if we make PTRACE_GETEVENTMSG return 0 or any other value routinely set by
other ptrace events, it would be hard for userspace to find out whether
the kernel implements new semantics or not.


-- 
ldv


signature.asc
Description: PGP signature


Re: [RFC PATCH v2] ptrace: add PTRACE_GET_SYSCALL_INFO request

2018-11-24 Thread Dmitry V. Levin
On Fri, Nov 23, 2018 at 07:01:39AM +0300, Dmitry V. Levin wrote:
> On Thu, Nov 22, 2018 at 04:19:10PM -0800, Andy Lutomirski wrote:
> > On Thu, Nov 22, 2018 at 11:15 AM Dmitry V. Levin wrote:
> > > On Thu, Nov 22, 2018 at 06:55:29AM -0800, Andy Lutomirski wrote:
> > > > On Wed, Nov 21, 2018 at 3:56 PM Dmitry V. Levin wrote:
> > > > > On Wed, Nov 21, 2018 at 02:56:57PM -0800, Andy Lutomirski wrote:
> > > > > > Please cc linux-...@vger.kernel.org for future versions.
> > > > > >
> > > > > > On Wed, Nov 21, 2018 at 7:58 AM Elvira Khabirova wrote:
> > > > > > >
> > > > > > > struct ptrace_syscall_info {
> > > > > > > __u8 op; /* 0 for entry, 1 for exit */
> > > > > >
> > > > > > Can you add proper defines, like:
> > > > > >
> > > > > > #define PTRACE_SYSCALL_ENTRY 0
> > > > > > #define PTRACE_SYSCALL_EXIT 1
> > > > > > #define PTRACE_SYSCALL_SECCOMP 2
> > > > > >
> > > > > > and make seccomp work from the start?  I'd rather we don't merge an
> > > > > > implementation that doesn't work for seccomp and then have to rework
> > > > > > it later.
> > > > >
> > > > > What's the difference between PTRACE_EVENT_SECCOMP and 
> > > > > syscall-entry-stop
> > > > > with regards to PTRACE_GET_SYSCALL_INFO request?  At least they have 
> > > > > the
> > > > > same entry_info to return.
> > > >
> > > > I'm not sure there's any material difference.
> > >
> > > In that case we don't really need PTRACE_SYSCALL_SECCOMP: op field
> > > describes the structure inside the union to use, not the ptrace stop.
> > 
> > Unless we think the structures might diverge in the future.
> 
> If these structures ever diverge, then a seccomp structure will be added
> to the union, and a portable userspace code will likely look this way:
> 
> #include 
> ...
> struct ptrace_syscall_info info;
> long rc = ptrace(PTRACE_GET_SYSCALL_INFO, pid, (void *) sizeof(info), &info);
> ...
> switch (info.op) {
>   case PTRACE_SYSCALL_INFO_ENTRY:
>   /* handle info.entry */
>   case PTRACE_SYSCALL_INFO_EXIT:
>   /* handle info.exit */
> #ifdef PTRACE_SYSCALL_INFO_SECCOMP
>   case PTRACE_SYSCALL_INFO_SECCOMP:
>   /* handle info.seccomp */
> #endif
>   default:
>   /* handle unknown info.op */
> }
> 
> In other words, it would be better if PTRACE_SYSCALL_INFO_* selector
> constants were introduced along with corresponding structures in the
> union.

However, the approach I suggested doesn't provide forward compatibility:
if userspace is compiled with kernel headers that don't define
PTRACE_SYSCALL_INFO_SECCOMP, it will break when the kernel
starts to use PTRACE_SYSCALL_INFO_SECCOMP instead of
PTRACE_SYSCALL_INFO_ENTRY for PTRACE_EVENT_SECCOMP support
in PTRACE_GET_SYSCALL_INFO.

The solution is to introduce PTRACE_SYSCALL_INFO_SECCOMP and struct
ptrace_syscall_info.seccomp along with PTRACE_EVENT_SECCOMP support
in PTRACE_GET_SYSCALL_INFO.  The initial revision of the seccomp
structure could be made the same as the entry structure, or it can
diverge from the beginning, e.g., by adding ret_data field containing
SECCOMP_RET_DATA return value stored in ptrace_message, this would save
ptracers an extra PTRACE_GETEVENTMSG call currently required to obtain it.


-- 
ldv


signature.asc
Description: PGP signature


Re: [RFC PATCH RESEND v3 3/3] ptrace: add PTRACE_EVENT_SECCOMP support to PTRACE_GET_SYSCALL_INFO

2018-11-26 Thread Dmitry V. Levin
On Tue, Nov 27, 2018 at 04:07:32AM +0100, Elvira Khabirova wrote:
> On Mon, 26 Nov 2018 15:35:24 +0100, Oleg Nesterov wrote:
> > On 11/25, Elvira Khabirova wrote:
> > >
> > > Extend PTRACE_GET_SYSCALL_INFO to support PTRACE_EVENT_SECCOMP stops.
> > > The information returned is the same as for syscall-enter-stops.  
> > 
> > Oh, this is not nice ;) there must be a better option, I hope... Plus
> > 
> > Can't ptrace_get_syscall() check
> > 
> > child->exit_code == (PTRACE_EVENT_SECCOMP << 8) | SIGTRAP;
> > 
> > to detect the PTRACE_EVENT_SECCOMP case?
> 
> Nope; looks like exit_code is zeroed after wait().

It's explicitly reset to zero in wait_task_stopped() unless WNOWAIT wait
option is set.  When strace requests PTRACE_GET_SYSCALL_INFO after
wait4(), child->exit_code is already set to zero.


-- 
ldv


signature.asc
Description: PGP signature


Re: [PATCH RESEND v3 1/3] ptrace: pass type of a syscall-stop in ptrace_message

2018-11-26 Thread Dmitry V. Levin
On Tue, Nov 27, 2018 at 03:53:57AM +0100, Elvira Khabirova wrote:
> On Mon, 26 Nov 2018 15:56:43 +0100, Oleg Nesterov wrote:
> > On 11/25, Elvira Khabirova wrote:
> > >
> > > + * These values are stored in task->ptrace_message by 
> > > tracehook_report_syscall_*
> > > + * to describe current syscall-stop.
> > > + *
> > > + * Values for these constants are chosen so that they do not appear
> > > + * in task->ptrace_message by other means.
> > > + */
> > > +#define PTRACE_EVENTMSG_SYSCALL_ENTRY0x8000U
> > > +#define PTRACE_EVENTMSG_SYSCALL_EXIT 0x9000U  
> > 
> > Stupid question, why not
> > 
> > #define PTRACE_EVENT_SYSCALL_ENTRY  8
> > #define PTRACE_EVENT_SYSCALL_EXIT   9
> > 
> > right after other PTRACE_EVENT_* constants?
> 
> I thought about adding new events for syscall {entry,exit}.
> For tracers, using new events means setting new options and checking
> for new values after waitpid(). They will also have to switch from using
> PTRACE_SYSCALL to PTRACE_CONT.
> Right now (with this version of the patch) tracers can use
> PTRACE_GETEVENTMSG without doing any additional configuration.
> More importantly, adding these events would require much more complex
> modifications of kernel code than this patch does.

To be honest, we don't see any way of introducing PTRACE_EVENT_* support
in PTRACE_GET_SYSCALL_INFO without adding new fields to struct task_struct.

> The only benefit I see from adding these events instead of letting
> syscall-stops put a value in ptrace_message is an ability to subscribe
> to syscall entries, but not to exits, and vice-versa, and I don't think
> it is worth it.


-- 
ldv


signature.asc
Description: PGP signature


[PATCH v2 03/15] Move EM_UNICORE to uapi/linux/elf-em.h

2018-11-19 Thread Dmitry V. Levin
This should never have been defined in the arch tree to begin with,
and now uapi/linux/audit.h header is going to use EM_UNICORE
in order to define AUDIT_ARCH_UNICORE which is needed to implement
syscall_get_arch() which in turn is required to extend
the generic ptrace API with PTRACE_GET_SYSCALL_INFO request.

Signed-off-by: Dmitry V. Levin 
---
v2: unchanged since v1

 arch/unicore32/include/asm/elf.h | 3 +--
 include/uapi/linux/elf-em.h  | 1 +
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/arch/unicore32/include/asm/elf.h b/arch/unicore32/include/asm/elf.h
index 829042d07722..ae66dc1be49e 100644
--- a/arch/unicore32/include/asm/elf.h
+++ b/arch/unicore32/include/asm/elf.h
@@ -19,6 +19,7 @@
  * ELF register definitions..
  */
 #include 
+#include 
 
 typedef unsigned long elf_greg_t;
 typedef unsigned long elf_freg_t[3];
@@ -28,8 +29,6 @@ typedef elf_greg_t elf_gregset_t[ELF_NGREG];
 
 typedef struct fp_state elf_fpregset_t;
 
-#define EM_UNICORE 110
-
 #define R_UNICORE_NONE 0
 #define R_UNICORE_PC24 1
 #define R_UNICORE_ABS322
diff --git a/include/uapi/linux/elf-em.h b/include/uapi/linux/elf-em.h
index 91b33833630b..a4fba79abbb9 100644
--- a/include/uapi/linux/elf-em.h
+++ b/include/uapi/linux/elf-em.h
@@ -36,6 +36,7 @@
 #define EM_OPENRISC 92 /* OpenRISC 32-bit embedded processor */
 #define EM_ARCOMPACT   93  /* ARCompact processor */
 #define EM_BLACKFIN 106 /* ADI Blackfin Processor */
+#define EM_UNICORE 110 /* UniCore-32 */
 #define EM_ALTERA_NIOS2113 /* Altera Nios II soft-core processor */
 #define EM_TI_C6000140 /* TI C6X DSPs */
 #define EM_HEXAGON 164 /* QUALCOMM Hexagon */
-- 
ldv


[PATCH v2 02/15] Move EM_ARCOMPACT and EM_ARCV2 to uapi/linux/elf-em.h

2018-11-19 Thread Dmitry V. Levin
These should never have been defined in the arch tree to begin with, and
now uapi/linux/audit.h header is going to use EM_ARCOMPACT and EM_ARCV2
in order to define AUDIT_ARCH_ARCOMPACT and AUDIT_ARCH_ARCV2 which are
needed to implement syscall_get_arch() which in turn is required to
extend the generic ptrace API with PTRACE_GET_SYSCALL_INFO request.

Signed-off-by: Dmitry V. Levin 
Acked-by: Vineet Gupta 
---
v2: added Acked-by to [PATCH 02/13 v2]

 arch/arc/include/asm/elf.h  | 6 +-
 include/uapi/linux/elf-em.h | 2 ++
 2 files changed, 3 insertions(+), 5 deletions(-)

diff --git a/arch/arc/include/asm/elf.h b/arch/arc/include/asm/elf.h
index aa2d6da9d187..2b80c184c9c8 100644
--- a/arch/arc/include/asm/elf.h
+++ b/arch/arc/include/asm/elf.h
@@ -10,13 +10,9 @@
 #define __ASM_ARC_ELF_H
 
 #include 
+#include 
 #include 
 
-/* These ELF defines belong to uapi but libc elf.h already defines them */
-#define EM_ARCOMPACT   93
-
-#define EM_ARCV2   195 /* ARCv2 Cores */
-
 #define EM_ARC_INUSE   (IS_ENABLED(CONFIG_ISA_ARCOMPACT) ? \
EM_ARCOMPACT : EM_ARCV2)
 
diff --git a/include/uapi/linux/elf-em.h b/include/uapi/linux/elf-em.h
index ba3696e3d694..91b33833630b 100644
--- a/include/uapi/linux/elf-em.h
+++ b/include/uapi/linux/elf-em.h
@@ -34,6 +34,7 @@
 #define EM_M32R88  /* Renesas M32R */
 #define EM_MN10300 89  /* Panasonic/MEI MN10300, AM33 */
 #define EM_OPENRISC 92 /* OpenRISC 32-bit embedded processor */
+#define EM_ARCOMPACT   93  /* ARCompact processor */
 #define EM_BLACKFIN 106 /* ADI Blackfin Processor */
 #define EM_ALTERA_NIOS2113 /* Altera Nios II soft-core processor */
 #define EM_TI_C6000140 /* TI C6X DSPs */
@@ -42,6 +43,7 @@
 #define EM_TILEPRO 188 /* Tilera TILEPro */
 #define EM_MICROBLAZE  189 /* Xilinx MicroBlaze */
 #define EM_TILEGX  191 /* Tilera TILE-Gx */
+#define EM_ARCV2   195 /* ARCv2 Cores */
 #define EM_RISCV   243 /* RISC-V */
 #define EM_BPF 247 /* Linux BPF - in-kernel virtual machine */
 #define EM_FRV 0x5441  /* Fujitsu FR-V */
-- 
ldv


[PATCH v2 01/15] Move EM_HEXAGON to uapi/linux/elf-em.h

2018-11-19 Thread Dmitry V. Levin
This should never have been defined in the arch tree to begin with,
and now uapi/linux/audit.h header is going to use EM_HEXAGON
in order to define AUDIT_ARCH_HEXAGON which is needed to implement
syscall_get_arch() which in turn is required to extend
the generic ptrace API with PTRACE_GET_SYSCALL_INFO request.

Signed-off-by: Dmitry V. Levin 
---
v2: unchanged since v1

 arch/hexagon/include/asm/elf.h | 6 +-
 include/uapi/linux/elf-em.h| 1 +
 2 files changed, 2 insertions(+), 5 deletions(-)

diff --git a/arch/hexagon/include/asm/elf.h b/arch/hexagon/include/asm/elf.h
index 80311e7b8ca6..d10fbd54ae51 100644
--- a/arch/hexagon/include/asm/elf.h
+++ b/arch/hexagon/include/asm/elf.h
@@ -23,11 +23,7 @@
 
 #include 
 #include 
-
-/*
- * This should really be in linux/elf-em.h.
- */
-#define EM_HEXAGON 164   /* QUALCOMM Hexagon */
+#include 
 
 struct elf32_hdr;
 
diff --git a/include/uapi/linux/elf-em.h b/include/uapi/linux/elf-em.h
index 93722e60204c..ba3696e3d694 100644
--- a/include/uapi/linux/elf-em.h
+++ b/include/uapi/linux/elf-em.h
@@ -37,6 +37,7 @@
 #define EM_BLACKFIN 106 /* ADI Blackfin Processor */
 #define EM_ALTERA_NIOS2113 /* Altera Nios II soft-core processor */
 #define EM_TI_C6000140 /* TI C6X DSPs */
+#define EM_HEXAGON 164 /* QUALCOMM Hexagon */
 #define EM_AARCH64 183 /* ARM 64 bit */
 #define EM_TILEPRO 188 /* Tilera TILEPro */
 #define EM_MICROBLAZE  189 /* Xilinx MicroBlaze */
-- 
ldv


[PATCH v2 05/15] elf-em.h: add EM_XTENSA

2018-11-19 Thread Dmitry V. Levin
The uapi/linux/audit.h header is going to use EM_XTENSA in order
to define AUDIT_ARCH_XTENSA which is needed to implement
syscall_get_arch() which in turn is required to extend
the generic ptrace API with PTRACE_GET_SYSCALL_INFO request.

The value for EM_XTENSA has been taken from
http://www.sco.com/developers/gabi/2012-12-31/ch4.eheader.html

Signed-off-by: Dmitry V. Levin 
Reviewed-by: Max Filippov 
---
v2: added Reviewed-by to v1

 include/uapi/linux/elf-em.h | 1 +
 1 file changed, 1 insertion(+)

diff --git a/include/uapi/linux/elf-em.h b/include/uapi/linux/elf-em.h
index ba2e64cdbb6f..7b02cf339d8f 100644
--- a/include/uapi/linux/elf-em.h
+++ b/include/uapi/linux/elf-em.h
@@ -35,6 +35,7 @@
 #define EM_MN10300 89  /* Panasonic/MEI MN10300, AM33 */
 #define EM_OPENRISC 92 /* OpenRISC 32-bit embedded processor */
 #define EM_ARCOMPACT   93  /* ARCompact processor */
+#define EM_XTENSA  94  /* Tensilica Xtensa Architecture */
 #define EM_BLACKFIN 106 /* ADI Blackfin Processor */
 #define EM_UNICORE 110 /* UniCore-32 */
 #define EM_ALTERA_NIOS2113 /* Altera Nios II soft-core processor */
-- 
ldv


[PATCH v2 06/15] m68k: define syscall_get_arch()

2018-11-19 Thread Dmitry V. Levin
syscall_get_arch() is required to be implemented on all architectures
in order to extend the generic ptrace API with PTRACE_GET_SYSCALL_INFO
request.

Signed-off-by: Dmitry V. Levin 
---
v2: unchanged since v1

 arch/m68k/include/asm/syscall.h | 12 
 1 file changed, 12 insertions(+)
 create mode 100644 arch/m68k/include/asm/syscall.h

diff --git a/arch/m68k/include/asm/syscall.h b/arch/m68k/include/asm/syscall.h
new file mode 100644
index ..d4d7deda8d50
--- /dev/null
+++ b/arch/m68k/include/asm/syscall.h
@@ -0,0 +1,12 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _ASM_M68K_SYSCALL_H
+#define _ASM_M68K_SYSCALL_H
+
+#include 
+
+static inline int syscall_get_arch(void)
+{
+   return AUDIT_ARCH_M68K;
+}
+
+#endif /* _ASM_M68K_SYSCALL_H */
-- 
ldv


[PATCH v2 04/15] elf-em.h: add EM_NDS32

2018-11-19 Thread Dmitry V. Levin
The uapi/linux/audit.h header is going to use EM_NDS32 in order
to define AUDIT_ARCH_NDS32 which is needed to implement
syscall_get_arch() which in turn is required to extend
the generic ptrace API with PTRACE_GET_SYSCALL_INFO request.

The value for EM_NDS32 has been taken from
http://www.sco.com/developers/gabi/2012-12-31/ch4.eheader.html

Signed-off-by: Dmitry V. Levin 
---
v2: unchanged since v1

 include/uapi/linux/elf-em.h | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/include/uapi/linux/elf-em.h b/include/uapi/linux/elf-em.h
index a4fba79abbb9..ba2e64cdbb6f 100644
--- a/include/uapi/linux/elf-em.h
+++ b/include/uapi/linux/elf-em.h
@@ -40,6 +40,8 @@
 #define EM_ALTERA_NIOS2113 /* Altera Nios II soft-core processor */
 #define EM_TI_C6000140 /* TI C6X DSPs */
 #define EM_HEXAGON 164 /* QUALCOMM Hexagon */
+#define EM_NDS32   167 /* Andes Technology compact code size
+  embedded RISC processor family */
 #define EM_AARCH64 183 /* ARM 64 bit */
 #define EM_TILEPRO 188 /* Tilera TILEPro */
 #define EM_MICROBLAZE  189 /* Xilinx MicroBlaze */
-- 
ldv


[PATCH v2 11/15] nds32: define syscall_get_arch()

2018-11-19 Thread Dmitry V. Levin
syscall_get_arch() is required to be implemented on all architectures
in order to extend the generic ptrace API with PTRACE_GET_SYSCALL_INFO
request.

Signed-off-by: Dmitry V. Levin 
---
v2: unchanged since [PATCH 10/13 v2]

 arch/nds32/include/asm/syscall.h | 8 
 include/uapi/linux/audit.h   | 2 ++
 2 files changed, 10 insertions(+)

diff --git a/arch/nds32/include/asm/syscall.h b/arch/nds32/include/asm/syscall.h
index f7e5e86765fe..569149ca25da 100644
--- a/arch/nds32/include/asm/syscall.h
+++ b/arch/nds32/include/asm/syscall.h
@@ -5,6 +5,7 @@
 #ifndef _ASM_NDS32_SYSCALL_H
 #define _ASM_NDS32_SYSCALL_H   1
 
+#include 
 #include 
 struct task_struct;
 struct pt_regs;
@@ -185,4 +186,11 @@ void syscall_set_arguments(struct task_struct *task, 
struct pt_regs *regs,
 
memcpy(®s->uregs[0] + i, args, n * sizeof(args[0]));
 }
+
+static inline int syscall_get_arch(void)
+{
+   return IS_ENABLED(CONFIG_CPU_BIG_ENDIAN)
+   ? AUDIT_ARCH_NDS32BE : AUDIT_ARCH_NDS32;
+}
+
 #endif /* _ASM_NDS32_SYSCALL_H */
diff --git a/include/uapi/linux/audit.h b/include/uapi/linux/audit.h
index 421953fc2f13..b9ce3016e85b 100644
--- a/include/uapi/linux/audit.h
+++ b/include/uapi/linux/audit.h
@@ -400,6 +400,8 @@ enum {
 #define AUDIT_ARCH_MIPSEL64(EM_MIPS|__AUDIT_ARCH_64BIT|__AUDIT_ARCH_LE)
 #define AUDIT_ARCH_MIPSEL64N32 (EM_MIPS|__AUDIT_ARCH_64BIT|__AUDIT_ARCH_LE|\
 __AUDIT_ARCH_CONVENTION_MIPS64_N32)
+#define AUDIT_ARCH_NDS32   (EM_NDS32|__AUDIT_ARCH_LE)
+#define AUDIT_ARCH_NDS32BE (EM_NDS32)
 #define AUDIT_ARCH_OPENRISC(EM_OPENRISC)
 #define AUDIT_ARCH_PARISC  (EM_PARISC)
 #define AUDIT_ARCH_PARISC64(EM_PARISC|__AUDIT_ARCH_64BIT)
-- 
ldv


[PATCH] mips: fix mips_get_syscall_arg o32 check

2018-11-21 Thread Dmitry V. Levin
When checking for TIF_32BIT_REGS flag, mips_get_syscall_arg() should
use the task specified as its argument instead of the current task.

This potentially affects all syscall_get_arguments() users
who specify tasks different from the current.

Fixes: c0ff3c53d4f99 ("MIPS: Enable HAVE_ARCH_TRACEHOOK.")
Signed-off-by: Dmitry V. Levin 
---
 arch/mips/include/asm/syscall.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/mips/include/asm/syscall.h b/arch/mips/include/asm/syscall.h
index 0170602a1e4e..6cf8ffb5367e 100644
--- a/arch/mips/include/asm/syscall.h
+++ b/arch/mips/include/asm/syscall.h
@@ -73,7 +73,7 @@ static inline unsigned long mips_get_syscall_arg(unsigned 
long *arg,
 #ifdef CONFIG_64BIT
case 4: case 5: case 6: case 7:
 #ifdef CONFIG_MIPS32_O32
-   if (test_thread_flag(TIF_32BIT_REGS))
+   if (test_tsk_thread_flag(task, TIF_32BIT_REGS))
return get_user(*arg, (int *)usp + n);
else
 #endif
-- 
ldv


Re: [RFC PATCH v2] ptrace: add PTRACE_GET_SYSCALL_INFO request

2018-11-21 Thread Dmitry V. Levin
On Wed, Nov 21, 2018 at 02:56:57PM -0800, Andy Lutomirski wrote:
> Please cc linux-...@vger.kernel.org for future versions.
> 
> On Wed, Nov 21, 2018 at 7:58 AM Elvira Khabirova wrote:
> >
> > struct ptrace_syscall_info {
> > __u8 op; /* 0 for entry, 1 for exit */
> 
> Can you add proper defines, like:
> 
> #define PTRACE_SYSCALL_ENTRY 0
> #define PTRACE_SYSCALL_EXIT 1
> #define PTRACE_SYSCALL_SECCOMP 2
> 
> and make seccomp work from the start?  I'd rather we don't merge an
> implementation that doesn't work for seccomp and then have to rework
> it later.

What's the difference between PTRACE_EVENT_SECCOMP and syscall-entry-stop
with regards to PTRACE_GET_SYSCALL_INFO request?  At least they have the
same entry_info to return.

As long as implementation (ab)uses ptrace_message to tell one kind of stop
from another, it can distinguish syscall-entry-stop and syscall-exit-stop
from each other and from many other kinds of stops, but it cannot
distinguish PTRACE_EVENT_SECCOMP from e.g. PTRACE_EVENT_EXIT.

> > __u8 __pad0[7];
> > union {
> > struct {
> > __s32 nr;
> 
> __u64 please.  Syscall numbers are, as a practical matter, 64 bits.
> Admittedly, the actual effects of setting the high bits are unclear,
> and seccomp has issues with it, but let's not perpetuate the problem.

I agree.  Although the implementation uses syscall_get_nr()
which returns int, this could potentially be fixed in the future.

> > __u32 arch;
> > __u64 instruction_pointer;
> > __u64 args[6];
> > } entry_info;
> > struct {
> > __s64 rval;
> > __u8 is_error;
> > __u8 __pad1[7];
> > } exit_info;
> > };
> > };
> 
> Should seccomp events use entry_info or should they just literally
> supply seccomp_data?

It certainly can use entry_info.
I'd prefer to avoid using in uapi/linux/ptrace.h those types
that are defined in uapi/linux/seccomp.h.


-- 
ldv


signature.asc
Description: PGP signature


Re: [RFC PATCH RESEND v3 3/3] ptrace: add PTRACE_EVENT_SECCOMP support to PTRACE_GET_SYSCALL_INFO

2018-11-27 Thread Dmitry V. Levin
On Tue, Nov 27, 2018 at 01:31:17PM +0100, Oleg Nesterov wrote:
> On 11/27, Elvira Khabirova wrote:
> > On Mon, 26 Nov 2018 15:35:24 +0100, Oleg Nesterov wrote:
> > > On 11/25, Elvira Khabirova wrote:
> > > >
> > > > Extend PTRACE_GET_SYSCALL_INFO to support PTRACE_EVENT_SECCOMP stops.
> > > > The information returned is the same as for syscall-enter-stops.
> > >
> > > Oh, this is not nice ;) there must be a better option, I hope... Plus
> > >
> > >
> > > Can't ptrace_get_syscall() check
> > >
> > >   child->exit_code == (PTRACE_EVENT_SECCOMP << 8) | SIGTRAP;
> > >
> > > to detect the PTRACE_EVENT_SECCOMP case?
> >
> > Nope; looks like exit_code is zeroed after wait().
> 
> Yes, thanks for correcting me,
> 
> but we can use child->last_siginfo->si_code.

Yes, this approach works, thanks!

> Just like ptrace_request(PTRACE_LISTEN)
> does but you can do this lockless (no need to lock_task_sighand()).

Why this can be done lockless?  All other places in that file do
the locking, so I'd rather add a comment in the new code.

> And if we require that the user of ptrace_get_syscall() should also use 
> TRACESYSGOOD
> then ptrace_get_syscall() can probably do something like
> 
>   int entry;
> 
>   if (!child->last_siginfo)
>   return -EINVAL;
>   else if (child->last_siginfo->si_code == (PTRACE_EVENT_SECCOMP << 8) | 
> SIGTRAP)
>   entry = 1;
>   else if (child->last_siginfo->si_code == SIGTRAP | 0x80)
>   entry = child->ptrace_message == PTRACE_EVENTMSG_SYSCALL_ENTRY;
>   else
>   return -EINVAL;
> 
> and this way PTRACE_EVENTMSG_SYSCALL_ENTRY/EXIT can't confict with seccomp or
> anything else.
> 
> No?
> 
> Of course, debugger can do PTRACE_SETSIGINFO and confuse itself but probably 
> we
> do not care?

The only potential issue I could think of is whether PTRACE_SETSIGINFO
could be used this way to cause an information leak by making
PTRACE_GET_SYSCALL_INFO access some unrelated data.


-- 
ldv


signature.asc
Description: PGP signature


Re: [RFC PATCH RESEND v3 3/3] ptrace: add PTRACE_EVENT_SECCOMP support to PTRACE_GET_SYSCALL_INFO

2018-11-28 Thread Dmitry V. Levin
On Wed, Nov 28, 2018 at 01:35:46PM +0100, Oleg Nesterov wrote:
> On 11/28, Dmitry V. Levin wrote:
> >
> > > Just like ptrace_request(PTRACE_LISTEN)
> > > does but you can do this lockless (no need to lock_task_sighand()).
> >
> > Why this can be done lockless?  All other places in that file do
> > the locking,
> 
> PTRACE_LISTEN too doesn't need lock_task_sighand() to access ->last_siginfo,
> this code predates ptrace_freeze_traced() which ensures that the tracee can't
> go away and clear ->last_siginfo.
> 
> However, unlike ptrace_get_syscall(), PTRACE_LISTEN needs 
> spin_lock_irq(siglock),
> it modifies ->jobctl and calls signal_wake_up().

What about PTRACE_GETSIGINFO?  Can it also be done lockless because
ptrace_check_attach() has already called ptrace_freeze_traced()?

> > > Of course, debugger can do PTRACE_SETSIGINFO and confuse itself but 
> > > probably we
> > > do not care?
> >
> > The only potential issue I could think of is whether PTRACE_SETSIGINFO
> > could be used this way to cause an information leak by making
> > PTRACE_GET_SYSCALL_INFO access some unrelated data.
> 
> Well, afaics ptrace_get_syscall() does nothing "special", debugger can use 
> other
> PTRACE_ requests to get the same info?

I agree.


-- 
ldv


signature.asc
Description: PGP signature


[PATCH v4 0/2] ptrace: add PTRACE_GET_SYSCALL_INFO request

2018-11-28 Thread Dmitry V. Levin
PTRACE_GET_SYSCALL_INFO lets ptracer obtain details of the syscall
the tracee is blocked in.  The request succeeds when the tracee is in a
syscall-enter-stop, syscall-exit-stop or PTRACE_EVENT_SECCOMP stop,
and fails with -EINVAL otherwise.

There are two reasons for a special syscall-related ptrace request.

Firstly, with the current ptrace API there are cases when ptracer cannot
retrieve necessary information about syscalls.  Some examples include:
* The notorious int-0x80-from-64-bit-task issue.  See [1] for details.
In short, if a 64-bit task performs a syscall through int 0x80, its tracer
has no reliable means to find out that the syscall was, in fact,
a compat syscall, and misidentifies it.
* Syscall-enter-stop and syscall-exit-stop look the same for the tracer.
Common practice is to keep track of the sequence of ptrace-stops in order
not to mix the two syscall-stops up.  But it is not as simple as it looks;
for example, strace had a (just recently fixed) long-standing bug where
attaching strace to a tracee that is performing the execve system call
led to the tracer identifying the following syscall-exit-stop as
syscall-enter-stop, which messed up all the state tracking.
* Since the introduction of commit 84d77d3f06e7e8dea057d10e8ec77ad71f721be3
("ptrace: Don't allow accessing an undumpable mm"), both PTRACE_PEEKDATA
and process_vm_readv become unavailable when the process dumpable flag
is cleared.  On such architectures as ia64 this results in all syscall
arguments being unavailable.

Secondly, ptracers also have to support a lot of arch-specific code for
obtaining information about the tracee.  For some architectures, this
requires a ptrace(PTRACE_PEEKUSER, ...) invocation for every syscall
argument and return value.

PTRACE_GET_SYSCALL_INFO returns the following structure:

struct ptrace_syscall_info {
__u8 op;/* PTRACE_SYSCALL_INFO_* */
__u8 __pad0[3];
__u32 arch;
union {
struct {
__u64 nr;
__u64 instruction_pointer;
__u64 stack_pointer;
__u64 frame_pointer;
__u64 args[6];
} entry;
struct {
__s64 rval;
__u8 is_error;
__u8 __pad1[7];
} exit;
struct {
__u64 nr;
__u64 instruction_pointer;
__u64 stack_pointer;
__u64 frame_pointer;
__u64 args[6];
__u32 ret_data;
__u8 __pad2[4];
} seccomp;
};
};

The structure was chosen according to [2], except for the following
changes:
* arch is returned unconditionally to aid with tracing system calls such as
execve();
* the type of nr field was changed from int to __u64 because syscall
numbers are, as a practical matter, 64 bits;
* stack_pointer and frame_pointer fields were added along with
instruction_pointer field since they are readily available and can save
the tracer from extra PTRACE_GETREGSET calls;
* a boolean is_error field was added along with rval field, this way
the tracer can more reliably distinguish a return value
from an error value.

This changeset should be applied on top of [3] and [4].

[1] 
https://lore.kernel.org/lkml/ca+55afzcsvmddj9lh_gdbz1ozhyem6zrgpbdajnywm2lf_e...@mail.gmail.com/
[2] 
https://lore.kernel.org/lkml/caobl_7gm0n80n7j_dfw_eqyflyzq+sf4y2avsccv88tb3aw...@mail.gmail.com/
[3] https://lore.kernel.org/lkml/20181119210139.ga8...@altlinux.org/
[4] https://lore.kernel.org/lkml/20181120001128.ga11...@altlinux.org/

v4:
* Re-split into two commits.
* Do not introduce task_struct.ptrace_event, use child->last_siginfo->si_code
  instead.
* Implement PTRACE_SYSCALL_INFO_SECCOMP and ptrace_syscall_info.seccomp
  support along with PTRACE_SYSCALL_INFO_{ENTRY,EXIT} and
  ptrace_syscall_info.{entry,exit}.

v3:
* Split into three commits.
* Change struct ptrace_syscall_info.
* Support PTRACE_EVENT_SECCOMP by adding ptrace_event to task_struct.
* Add proper defines for ptrace_syscall_info.op values.
* Rename PT_SYSCALL_IS_ENTERING and PT_SYSCALL_IS_EXITING to
* PTRACE_EVENTMSG_SYSCALL_ENTRY and PTRACE_EVENTMSG_SYSCALL_EXIT
* and move them to uapi.

v2:
* Do not use task->ptrace.
* Replace entry_info.is_compat with entry_info.arch, use syscall_get_arch().
* Use addr argument of sys_ptrace to get expected size of the struct;
  return full size of the struct.

Elvira Khabirova (2):
  ptrace: save the type of syscall-stop in ptrace_message
  ptrace: add PTRACE_GET_SYSCALL_INFO request

 include/linux/tracehook.h   |   9 ++--
 include/uapi/linux/ptrace.h |  44 +++
 kernel/ptrace.c | 103 +++-
 3 files changed, 152 insertions(+), 4 deletions(-)

-- 
ldv


[PATCH v4 1/2] ptrace: save the type of syscall-stop in ptrace_message

2018-11-28 Thread Dmitry V. Levin
From: Elvira Khabirova 

Define two constants, PTRACE_EVENTMSG_SYSCALL_ENTRY and
PTRACE_EVENTMSG_SYSCALL_EXIT, and place them in ptrace_message
for the duration of syscall-stops.
This way ptracers can distinguish syscall-enter-stops
from syscall-exit-stops using PTRACE_GETEVENTMSG request.

Signed-off-by: Elvira Khabirova 
Signed-off-by: Dmitry V. Levin 
---
 include/linux/tracehook.h   |  9 ++---
 include/uapi/linux/ptrace.h | 10 ++
 2 files changed, 16 insertions(+), 3 deletions(-)

diff --git a/include/linux/tracehook.h b/include/linux/tracehook.h
index 40b0b4c1bf7b..633a83fe7051 100644
--- a/include/linux/tracehook.h
+++ b/include/linux/tracehook.h
@@ -57,13 +57,15 @@ struct linux_binprm;
 /*
  * ptrace report for syscall entry and exit looks identical.
  */
-static inline int ptrace_report_syscall(struct pt_regs *regs)
+static inline int ptrace_report_syscall(struct pt_regs *regs,
+   unsigned long message)
 {
int ptrace = current->ptrace;
 
if (!(ptrace & PT_PTRACED))
return 0;
 
+   current->ptrace_message = message;
ptrace_notify(SIGTRAP | ((ptrace & PT_TRACESYSGOOD) ? 0x80 : 0));
 
/*
@@ -76,6 +78,7 @@ static inline int ptrace_report_syscall(struct pt_regs *regs)
current->exit_code = 0;
}
 
+   current->ptrace_message = 0;
return fatal_signal_pending(current);
 }
 
@@ -101,7 +104,7 @@ static inline int ptrace_report_syscall(struct pt_regs 
*regs)
 static inline __must_check int tracehook_report_syscall_entry(
struct pt_regs *regs)
 {
-   return ptrace_report_syscall(regs);
+   return ptrace_report_syscall(regs, PTRACE_EVENTMSG_SYSCALL_ENTRY);
 }
 
 /**
@@ -126,7 +129,7 @@ static inline void tracehook_report_syscall_exit(struct 
pt_regs *regs, int step)
if (step)
user_single_step_report(regs);
else
-   ptrace_report_syscall(regs);
+   ptrace_report_syscall(regs, PTRACE_EVENTMSG_SYSCALL_EXIT);
 }
 
 /**
diff --git a/include/uapi/linux/ptrace.h b/include/uapi/linux/ptrace.h
index d5a1b8a492b9..cb138902d042 100644
--- a/include/uapi/linux/ptrace.h
+++ b/include/uapi/linux/ptrace.h
@@ -104,6 +104,16 @@ struct seccomp_metadata {
 #define PTRACE_O_MASK  (\
0x00ff | PTRACE_O_EXITKILL | PTRACE_O_SUSPEND_SECCOMP)
 
+/*
+ * These values are stored in task->ptrace_message by 
tracehook_report_syscall_*
+ * to describe current syscall-stop.
+ *
+ * Values for these constants are chosen so that they do not appear
+ * in task->ptrace_message by other means.
+ */
+#define PTRACE_EVENTMSG_SYSCALL_ENTRY  0x8000U
+#define PTRACE_EVENTMSG_SYSCALL_EXIT   0x9000U
+
 #include 
 
 
-- 
ldv


[PATCH v4 2/2] ptrace: add PTRACE_GET_SYSCALL_INFO request

2018-11-28 Thread Dmitry V. Levin
From: Elvira Khabirova 

PTRACE_GET_SYSCALL_INFO lets ptracer obtain details of the syscall
the tracee is blocked in.  The request succeeds when the tracee is
in a syscall-enter-stop, syscall-exit-stop, or PTRACE_EVENT_SECCOMP,
and fails with -EINVAL otherwise.

There are two reasons for a special syscall-related ptrace request.

Firstly, with the current ptrace API there are cases when ptracer cannot
retrieve necessary information about syscalls.  Some examples include:
* The notorious int-0x80-from-64-bit-task issue.  See [1] for details.
In short, if a 64-bit task performs a syscall through int 0x80, its tracer
has no reliable means to find out that the syscall was, in fact,
a compat syscall, and misidentifies it.
* Syscall-enter-stop and syscall-exit-stop look the same for the tracer.
Common practice is to keep track of the sequence of ptrace-stops in order
not to mix the two syscall-stops up.  But it is not as simple as it looks;
for example, strace had a (just recently fixed) long-standing bug where
attaching strace to a tracee that is performing the execve system call
led to the tracer identifying the following syscall-exit-stop as
syscall-enter-stop, which messed up all the state tracking.
* Since the introduction of commit 84d77d3f06e7e8dea057d10e8ec77ad71f721be3
("ptrace: Don't allow accessing an undumpable mm"), both PTRACE_PEEKDATA
and process_vm_readv become unavailable when the process dumpable flag
is cleared.  On such architectures as ia64 this results in all syscall
arguments being unavailable.

Secondly, ptracers also have to support a lot of arch-specific code for
obtaining information about the tracee.  For some architectures, this
requires a ptrace(PTRACE_PEEKUSER, ...) invocation for every syscall
argument and return value.

ptrace(2) man page:

long ptrace(enum __ptrace_request request, pid_t pid,
void *addr, void *data);
...
PTRACE_GET_SYSCALL_INFO
   Retrieve information about the syscall that caused the stop.
   The information is placed into the buffer pointed by "data"
   argument, which should be a pointer to a buffer of type
   "struct ptrace_syscall_info".
   The "addr" argument contains the size of the buffer pointed to
   by "data" argument (i.e., sizeof(struct ptrace_syscall_info)).
   The return value contains the number of bytes available
   to be written by the kernel.
   If the size of data to be written by the kernel exceeds the size
   specified by "addr" argument, the output is truncated.
   This operation fails with EINVAL if the tracee is not
   in a syscall-enter-stop, a syscall-exit-stop, or
   a PTRACE_EVENT_SECCOMP stop.

Co-authored-by: Dmitry V. Levin 
Signed-off-by: Elvira Khabirova 
Signed-off-by: Dmitry V. Levin 
---
 include/uapi/linux/ptrace.h |  34 +++
 kernel/ptrace.c | 110 +++-
 2 files changed, 143 insertions(+), 1 deletion(-)

diff --git a/include/uapi/linux/ptrace.h b/include/uapi/linux/ptrace.h
index cb138902d042..ef42149e77f4 100644
--- a/include/uapi/linux/ptrace.h
+++ b/include/uapi/linux/ptrace.h
@@ -73,6 +73,40 @@ struct seccomp_metadata {
__u64 flags;/* Output: filter's flags */
 };
 
+#define PTRACE_GET_SYSCALL_INFO0x420f
+#define PTRACE_SYSCALL_INFO_ENTRY  0
+#define PTRACE_SYSCALL_INFO_EXIT   1
+#define PTRACE_SYSCALL_INFO_SECCOMP2
+
+struct ptrace_syscall_info {
+   __u8 op;/* PTRACE_SYSCALL_INFO_* */
+   __u8 __pad0[3];
+   __u32 arch;
+   union {
+   struct {
+   __u64 nr;
+   __u64 instruction_pointer;
+   __u64 stack_pointer;
+   __u64 frame_pointer;
+   __u64 args[6];
+   } entry;
+   struct {
+   __s64 rval;
+   __u8 is_error;
+   __u8 __pad1[7];
+   } exit;
+   struct {
+   __u64 nr;
+   __u64 instruction_pointer;
+   __u64 stack_pointer;
+   __u64 frame_pointer;
+   __u64 args[6];
+   __u32 ret_data;
+   __u8 __pad2[4];
+   } seccomp;
+   };
+};
+
 /* Read signals from a shared (process wide) queue */
 #define PTRACE_PEEKSIGINFO_SHARED  (1 << 0)
 
diff --git a/kernel/ptrace.c b/kernel/ptrace.c
index 80b34dffdfb9..32ff9c97f941 100644
--- a/kernel/ptrace.c
+++ b/kernel/ptrace.c
@@ -30,6 +30,10 @@
 #include 
 #include 
 
+#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
+#include/* For syscall_get_* */
+#endif
+
 /*
  * Access another process' address space via ptrace.
  * Source/target buffer must be kernel space,
@@ -888,7 +892,105 @@ static int ptrace_regset(struct task_struct *task, int 
req

Re: [PATCH v4 1/2] ptrace: save the type of syscall-stop in ptrace_message

2018-11-28 Thread Dmitry V. Levin
On Wed, Nov 28, 2018 at 02:49:14PM +0100, Oleg Nesterov wrote:
> On 11/28, Dmitry V. Levin wrote:
> >
> > +/*
> > + * These values are stored in task->ptrace_message by 
> > tracehook_report_syscall_*
> > + * to describe current syscall-stop.
> > + *
> > + * Values for these constants are chosen so that they do not appear
> > + * in task->ptrace_message by other means.
> > + */
> > +#define PTRACE_EVENTMSG_SYSCALL_ENTRY  0x8000U
> > +#define PTRACE_EVENTMSG_SYSCALL_EXIT   0x9000U
> 
> Again, I do not really understand the comment... Why should we care about
> "do not appear in task->ptrace_message by other means" ?
> 
> 2/2 should detect ptrace_report_syscall() case correctly, so we can use any
> numbers, say, 1 and 2?
> 
> If debugger does PTRACE_GETEVENTMSG it should know how to interpet the value
> anyway after wait(status).

Given that without this patch the value returned by PTRACE_GETEVENTMSG
during syscall stop is undefined, we need two different ptrace_message
values that cannot be set by other ptrace events to enable reliable
identification of syscall-enter-stop and syscall-exit-stop in userspace:
if we make PTRACE_GETEVENTMSG return 0 or any other value routinely set by
other ptrace events, it would be hard for userspace to find out whether
the kernel implements new semantics or not.


-- 
ldv


signature.asc
Description: PGP signature


Re: [PATCH v4 1/2] ptrace: save the type of syscall-stop in ptrace_message

2018-11-28 Thread Dmitry V. Levin
On Wed, Nov 28, 2018 at 03:20:06PM +0100, Oleg Nesterov wrote:
> On 11/28, Dmitry V. Levin wrote:
> > On Wed, Nov 28, 2018 at 02:49:14PM +0100, Oleg Nesterov wrote:
> > > On 11/28, Dmitry V. Levin wrote:
> > > >
> > > > +/*
> > > > + * These values are stored in task->ptrace_message by 
> > > > tracehook_report_syscall_*
> > > > + * to describe current syscall-stop.
> > > > + *
> > > > + * Values for these constants are chosen so that they do not appear
> > > > + * in task->ptrace_message by other means.
> > > > + */
> > > > +#define PTRACE_EVENTMSG_SYSCALL_ENTRY  0x8000U
> > > > +#define PTRACE_EVENTMSG_SYSCALL_EXIT   0x9000U
> > > 
> > > Again, I do not really understand the comment... Why should we care about
> > > "do not appear in task->ptrace_message by other means" ?
> > > 
> > > 2/2 should detect ptrace_report_syscall() case correctly, so we can use 
> > > any
> > > numbers, say, 1 and 2?
> > > 
> > > If debugger does PTRACE_GETEVENTMSG it should know how to interpet the 
> > > value
> > > anyway after wait(status).
> > 
> > Given that without this patch the value returned by PTRACE_GETEVENTMSG
> > during syscall stop is undefined, we need two different ptrace_message
> > values that cannot be set by other ptrace events to enable reliable
> > identification of syscall-enter-stop and syscall-exit-stop in userspace:
> > if we make PTRACE_GETEVENTMSG return 0 or any other value routinely set by
> > other ptrace events, it would be hard for userspace to find out whether
> > the kernel implements new semantics or not.
> 
> Hmm, why? Debugger can just do ptrace(PTRACE_GET_SYSCALL_INFO, NULL), if it
> returns EIO then it is not implemented?

The debugger that uses PTRACE_GET_SYSCALL_INFO does not need to call
PTRACE_GETEVENTMSG for syscall stops.
My concern here is the PTRACE_GETEVENTMSG interface itself.  If we use
ptrace_message to implement PTRACE_GET_SYSCALL_INFO and expose
PTRACE_EVENTMSG_SYSCALL_{ENTRY,EXIT} for regular PTRACE_GETEVENTMSG users,
it should have clear semantics.


-- 
ldv


signature.asc
Description: PGP signature


Re: [PATCH v4 1/2] ptrace: save the type of syscall-stop in ptrace_message

2018-11-28 Thread Dmitry V. Levin
On Wed, Nov 28, 2018 at 06:23:46PM +0300, Dmitry V. Levin wrote:
> On Wed, Nov 28, 2018 at 03:20:06PM +0100, Oleg Nesterov wrote:
> > On 11/28, Dmitry V. Levin wrote:
> > > On Wed, Nov 28, 2018 at 02:49:14PM +0100, Oleg Nesterov wrote:
> > > > On 11/28, Dmitry V. Levin wrote:
> > > > >
> > > > > +/*
> > > > > + * These values are stored in task->ptrace_message by 
> > > > > tracehook_report_syscall_*
> > > > > + * to describe current syscall-stop.
> > > > > + *
> > > > > + * Values for these constants are chosen so that they do not appear
> > > > > + * in task->ptrace_message by other means.
> > > > > + */
> > > > > +#define PTRACE_EVENTMSG_SYSCALL_ENTRY0x8000U
> > > > > +#define PTRACE_EVENTMSG_SYSCALL_EXIT 0x9000U
> > > > 
> > > > Again, I do not really understand the comment... Why should we care 
> > > > about
> > > > "do not appear in task->ptrace_message by other means" ?
> > > > 
> > > > 2/2 should detect ptrace_report_syscall() case correctly, so we can use 
> > > > any
> > > > numbers, say, 1 and 2?
> > > > 
> > > > If debugger does PTRACE_GETEVENTMSG it should know how to interpet the 
> > > > value
> > > > anyway after wait(status).
> > > 
> > > Given that without this patch the value returned by PTRACE_GETEVENTMSG
> > > during syscall stop is undefined, we need two different ptrace_message
> > > values that cannot be set by other ptrace events to enable reliable
> > > identification of syscall-enter-stop and syscall-exit-stop in userspace:
> > > if we make PTRACE_GETEVENTMSG return 0 or any other value routinely set by
> > > other ptrace events, it would be hard for userspace to find out whether
> > > the kernel implements new semantics or not.
> > 
> > Hmm, why? Debugger can just do ptrace(PTRACE_GET_SYSCALL_INFO, NULL), if it
> > returns EIO then it is not implemented?
> 
> The debugger that uses PTRACE_GET_SYSCALL_INFO does not need to call
> PTRACE_GETEVENTMSG for syscall stops.
> My concern here is the PTRACE_GETEVENTMSG interface itself.  If we use
> ptrace_message to implement PTRACE_GET_SYSCALL_INFO and expose
> PTRACE_EVENTMSG_SYSCALL_{ENTRY,EXIT} for regular PTRACE_GETEVENTMSG users,
> it should have clear semantics.

Since our implementation of PTRACE_GET_SYSCALL_INFO uses ptrace_message
to distinguish syscall-enter-stop from syscall-exit-stop, we could choose
one of the following approaches:

1. Do not document the values saved into ptrace_message during syscall
stops (and exposed via PTRACE_GETEVENTMSG) as a part of ptrace API,
leaving the value returned by PTRACE_GETEVENTMSG during syscall stops
as undefined.

2. Document these values chosen to avoid collisions with ptrace_message values
set by other ptrace events so that PTRACE_GETEVENTMSG users can easily tell
whether this new semantics is supported by the kernel or not.

The first approach was implemented in v2 of this series: the constants
were PT_SYSCALL_IS_{ENTERING,EXITING} defined in include/linux/ptrace.h.

The second approach was implemented in v3: the constants are
PTRACE_EVENTMSG_SYSCALL_{ENTRY,EXIT} defined in include/uapi/linux/ptrace.h,
they are also going to be documented in ptrace(2) man page.

Since the use of ptrace_message is exposed to PTRACE_GETEVENTMSG users
anyway, I do not see any reason to choose the first approach over the
second.


-- 
ldv


signature.asc
Description: PGP signature


Re: [PATCH v4 1/2] ptrace: save the type of syscall-stop in ptrace_message

2018-11-29 Thread Dmitry V. Levin
On Wed, Nov 28, 2018 at 03:17:49PM -0800, Andy Lutomirski wrote:
> On Wed, Nov 28, 2018 at 2:11 PM Dmitry V. Levin  wrote:
> >
> > On Wed, Nov 28, 2018 at 06:23:46PM +0300, Dmitry V. Levin wrote:
> > > On Wed, Nov 28, 2018 at 03:20:06PM +0100, Oleg Nesterov wrote:
> > > > On 11/28, Dmitry V. Levin wrote:
> > > > > On Wed, Nov 28, 2018 at 02:49:14PM +0100, Oleg Nesterov wrote:
> > > > > > On 11/28, Dmitry V. Levin wrote:
> > > > > > >
> > > > > > > +/*
> > > > > > > + * These values are stored in task->ptrace_message by 
> > > > > > > tracehook_report_syscall_*
> > > > > > > + * to describe current syscall-stop.
> > > > > > > + *
> > > > > > > + * Values for these constants are chosen so that they do not 
> > > > > > > appear
> > > > > > > + * in task->ptrace_message by other means.
> > > > > > > + */
> > > > > > > +#define PTRACE_EVENTMSG_SYSCALL_ENTRY0x8000U
> > > > > > > +#define PTRACE_EVENTMSG_SYSCALL_EXIT 0x9000U
> > > > > >
> > > > > > Again, I do not really understand the comment... Why should we care 
> > > > > > about
> > > > > > "do not appear in task->ptrace_message by other means" ?
> > > > > >
> > > > > > 2/2 should detect ptrace_report_syscall() case correctly, so we can 
> > > > > > use any
> > > > > > numbers, say, 1 and 2?
> > > > > >
> > > > > > If debugger does PTRACE_GETEVENTMSG it should know how to interpet 
> > > > > > the value
> > > > > > anyway after wait(status).
> > > > >
> > > > > Given that without this patch the value returned by PTRACE_GETEVENTMSG
> > > > > during syscall stop is undefined, we need two different ptrace_message
> > > > > values that cannot be set by other ptrace events to enable reliable
> > > > > identification of syscall-enter-stop and syscall-exit-stop in 
> > > > > userspace:
> > > > > if we make PTRACE_GETEVENTMSG return 0 or any other value routinely 
> > > > > set by
> > > > > other ptrace events, it would be hard for userspace to find out 
> > > > > whether
> > > > > the kernel implements new semantics or not.
> > > >
> > > > Hmm, why? Debugger can just do ptrace(PTRACE_GET_SYSCALL_INFO, NULL), 
> > > > if it
> > > > returns EIO then it is not implemented?
> > >
> > > The debugger that uses PTRACE_GET_SYSCALL_INFO does not need to call
> > > PTRACE_GETEVENTMSG for syscall stops.
> > > My concern here is the PTRACE_GETEVENTMSG interface itself.  If we use
> > > ptrace_message to implement PTRACE_GET_SYSCALL_INFO and expose
> > > PTRACE_EVENTMSG_SYSCALL_{ENTRY,EXIT} for regular PTRACE_GETEVENTMSG users,
> > > it should have clear semantics.
> >
> > Since our implementation of PTRACE_GET_SYSCALL_INFO uses ptrace_message
> > to distinguish syscall-enter-stop from syscall-exit-stop, we could choose
> > one of the following approaches:
> >
> > 1. Do not document the values saved into ptrace_message during syscall
> > stops (and exposed via PTRACE_GETEVENTMSG) as a part of ptrace API,
> > leaving the value returned by PTRACE_GETEVENTMSG during syscall stops
> > as undefined.
> >
> > 2. Document these values chosen to avoid collisions with ptrace_message 
> > values
> > set by other ptrace events so that PTRACE_GETEVENTMSG users can easily tell
> > whether this new semantics is supported by the kernel or not.
> 
> I don't like any of this at all.  Can we please choose a sensible API
> design and let the API drive the implementation instead of vice versa?

What are your concerns?  Do you see something wrong in exposing this
information via PTRACE_GETEVENTMSG?

Anyway, can we agree on the PTRACE_GET_SYSCALL_INFO API, please?

>  ISTM the correct solution is to add some new state to task_struct for
> this.
> 
> If we're concerned about making task_struct bigger, I have a
> half-finished patch to factor all the ptrace tracee state into a
> separate struct.

This is refactoring of the kernel - a thing userspace people are not
the best equipped to do.  This part should rather be sorted out by kernel
people.


-- 
ldv


signature.asc
Description: PGP signature


Re: [PATCH v4 1/2] ptrace: save the type of syscall-stop in ptrace_message

2018-11-29 Thread Dmitry V. Levin
On Thu, Nov 29, 2018 at 03:47:43PM +0100, Oleg Nesterov wrote:
> On 11/29, Dmitry V. Levin wrote:
> >
> > 2. Document these values
> 
> sure, they should be documented and live in include/uapi/,
> 
> > chosen to avoid collisions with ptrace_message values
> > set by other ptrace events
> 
> this is what I can't understand. But to clarify, I don't really care and
> won't argue.
> 
> If an application wants to use PTRACE_GETEVENTMSG to distinguish entry/exit
> (without PTRACE_GET_SYSCALL_INFO) it needs to do wait(status) and check status
> anyway, otherwise PTRACE_GETEVENTMSG is simply pointless (wrt syscall entry/
> exit). So we do not care if PTRACE_EVENTMSG_SYSCALL_ENTRY conflicts with, say,
> SECCOMP_RET_DATA.

Yes, once the application has verified that the kernel implements this
feature, there is no risk of collision.

> > so that PTRACE_GETEVENTMSG users can easily tell
> > whether this new semantics is supported by the kernel or not.
> 
> Yes. And how much this can help? Again, an application can trivially detect
> if this feature implemented or not, and it should do this anyway if it wants
> to (try to) use PTRACE_EVENTMSG_SYSCALL_ENTRY/EXIT ?

How an application can easily detect whether this feature is implemented?
By invoking PTRACE_GETEVENTMSG after the first syscall stop reported by
wait and checking whether the returned value is either
PTRACE_EVENTMSG_SYSCALL_ENTRY or PTRACE_EVENTMSG_SYSCALL_EXIT.

So the question is, how can this value be equal to one of these constants
when this feature is not implemented?  Can a value saved to ptrace_message
earlier by one of ptrace events be equal to one of these constants?

Imagine an application attaches to already existing process, enables
PTRACE_O_TRACESECCOMP, and a PTRACE_EVENT_SECCOMP arrives with
ptrace_message set to 1.  If this application then exits and a new invocation
of the same application attaches to the same process, it will very likely see
this 1 returned by PTRACE_GETEVENTMSG if the feature is not implemented
in the kernel.

To avoid that kind of collisions, kernel should use different ptrace_message
values for syscall stops.

> Again, I won't reallly argue. But if you insist that these values must
> be unique then you probably need to add
> 
>   BUILD_BUG_ON(PTRACE_EVENTMSG_SYSCALL_ENTRY <= PID_MAX_LIMIT);

Yes, it's a good idea.  What is the proper place for this check?


-- 
ldv


signature.asc
Description: PGP signature


Re: [RFC PATCH] ptrace: add PTRACE_GET_SYSCALL_INFO request

2018-11-12 Thread Dmitry V. Levin
On Wed, Nov 07, 2018 at 12:44:58PM -0800, Andy Lutomirski wrote:
> > On Nov 6, 2018, at 7:27 PM, Elvira Khabirova  
> > wrote:
> >
> > PTRACE_GET_SYSCALL_INFO lets ptracer obtain details of the syscall
> > the tracee is blocked in. The request returns meaningful data only
> > when the tracee is in a syscall-enter-stop or a syscall-exit-stop.
> >
> > There are two reasons for a special syscall-related ptrace request.
> >
> > Firstly, with the current ptrace API there are cases when ptracer cannot
> > retrieve necessary information about syscalls. Some examples include:
> > * The notorious int-0x80-from-64-bit-task issue. See [1] for details.
> > In short, if a 64-bit task performs a syscall through int 0x80, its tracer
> > has no reliable means to find out that the syscall was, in fact,
> > a compat syscall, and misidentifies it.
> > * Syscall-enter-stop and syscall-exit-stop look the same for the tracer.
> > Common practice is to keep track of the sequence of ptrace-stops in order
> > not to mix the two syscall-stops up. But it is not as simple as it looks;
> > for example, strace had a (just recently fixed) long-standing bug where
> > attaching strace to a tracee that is performing the execve system call
> > led to the tracer identifying the following syscall-exit-stop as
> > syscall-enter-stop, which messed up all the state tracking.
> > * Since the introduction of commit 84d77d3f06e7e8dea057d10e8ec77ad71f721be3
> > ("ptrace: Don't allow accessing an undumpable mm"), both PTRACE_PEEKDATA
> > and process_vm_readv become unavailable when the process dumpable flag
> > is cleared. On ia64 this results in all syscall arguments being unavailable.
> >
> > Secondly, ptracers also have to support a lot of arch-specific code for
> > obtaining information about the tracee. For some architectures, this
> > requires a ptrace(PTRACE_PEEKUSER, ...) invocation for every syscall
> > argument and return value.
> >
> > PTRACE_GET_SYSCALL_INFO returns the following structure:
> >
> > struct ptrace_syscall_info {
> >__u8 op; /* 0 for entry, 1 for exit */
> 
> Please consider adding another op for a seccomp stop.

If there are going to be more than two values, I'd suggest introducing
a enum or at least define appropriate macros.

wrt PTRACE_EVENT_SECCOMP, I don't see how the current proposed
implementation of PTRACE_GET_SYSCALL_INFO (based on ptrace_message)
could work in case of PTRACE_EVENT_SECCOMP (which also sets
ptrace_message).  Any ideas?


-- 
ldv


Re: [PATCH v4] powerpc/ptrace: replace ptrace_report_syscall() with a tracehook call

2018-12-06 Thread Dmitry V. Levin
On Mon, Dec 03, 2018 at 06:18:23AM +0300, Dmitry V. Levin wrote:
> From: Elvira Khabirova 
> 
> Arch code should use tracehook_*() helpers, as documented
> in include/linux/tracehook.h,
> ptrace_report_syscall() is not expected to be used outside that file.
> 
> Co-authored-by: Dmitry V. Levin 
> Fixes: 5521eb4bca2d ("powerpc/ptrace: Add support for PTRACE_SYSEMU")
> Signed-off-by: Elvira Khabirova 
> Signed-off-by: Dmitry V. Levin 
> ---
> v4: rewritten to call tracehook_report_syscall_entry() once, compile-tested
> v3: add a descriptive comment
> v2: explicitly ignore tracehook_report_syscall_entry() return code
> 
>  arch/powerpc/kernel/ptrace.c | 54 +++-
>  1 file changed, 35 insertions(+), 19 deletions(-)

Sorry, this patch does not work, please ignore it.
However, the bug blocks PTRACE_GET_SYSCALL_INFO, so please fix it.

I'm going to use
if (tracehook_report_syscall_entry(regs))
return -1;
return -1;
in the series until you have a better fix.


-- 
ldv


signature.asc
Description: PGP signature


Re: [PATCH v4] powerpc/ptrace: replace ptrace_report_syscall() with a tracehook call

2018-12-07 Thread Dmitry V. Levin
On Fri, Dec 07, 2018 at 10:12:49PM +1100, Michael Ellerman wrote:
> "Dmitry V. Levin"  writes:
> > On Mon, Dec 03, 2018 at 06:18:23AM +0300, Dmitry V. Levin wrote:
> >> From: Elvira Khabirova 
> >> 
> >> Arch code should use tracehook_*() helpers, as documented
> >> in include/linux/tracehook.h,
> >> ptrace_report_syscall() is not expected to be used outside that file.
> >> 
> >> Co-authored-by: Dmitry V. Levin 
> >> Fixes: 5521eb4bca2d ("powerpc/ptrace: Add support for PTRACE_SYSEMU")
> >> Signed-off-by: Elvira Khabirova 
> >> Signed-off-by: Dmitry V. Levin 
> >> ---
> >> v4: rewritten to call tracehook_report_syscall_entry() once, compile-tested
> >> v3: add a descriptive comment
> >> v2: explicitly ignore tracehook_report_syscall_entry() return code
> >> 
> >>  arch/powerpc/kernel/ptrace.c | 54 +++-
> >>  1 file changed, 35 insertions(+), 19 deletions(-)
> >
> > Sorry, this patch does not work, please ignore it.
> 
> Hmm OK. Why exactly?

Unfortunately, I have no idea why it doesn't work.
All I can say is it breaks strace because the kernel no longer sends
syscall entry stops.

> I wrote more or less the same patch, although I used a temporary bool.
> 
> > However, the bug blocks PTRACE_GET_SYSCALL_INFO, so please fix it.
> 
> Sorry, didn't realise it was blocking you.

We are changing ptrace_report_syscall signature to implement
PTRACE_GET_SYSCALL_INFO, and this is the only place in the kernel besides
tracehook_report_syscall_*() that invokes ptrace_report_syscall() directly.

> > I'm going to use
> > if (tracehook_report_syscall_entry(regs))
> > return -1;
> > return -1;
> > in the series until you have a better fix.
> 
> Yeah that's fine by me. I could send that to Linus for 4.20 if you want
> me to, otherwise I'm fine for you to carry it in your series.

Yes, please.  I'll send a v5 shortly.


-- 
ldv


signature.asc
Description: PGP signature


[PATCH v5] powerpc/ptrace: replace ptrace_report_syscall() with a tracehook call

2018-12-07 Thread Dmitry V. Levin
From: Elvira Khabirova 

Arch code should use tracehook_*() helpers, as documented
in include/linux/tracehook.h,
ptrace_report_syscall() is not expected to be used outside that file.

The patch does not look very nice, but at least it is correct
and opens the way for PTRACE_GET_SYSCALL_INFO API.

Co-authored-by: Dmitry V. Levin 
Fixes: 5521eb4bca2d ("powerpc/ptrace: Add support for PTRACE_SYSEMU")
Cc: Michael Ellerman 
Cc: Benjamin Herrenschmidt 
Cc: Paul Mackerras 
Cc: Oleg Nesterov 
Cc: Breno Leitao 
Cc: Andy Lutomirski 
Cc: Eugene Syromyatnikov 
Cc: linuxppc-...@lists.ozlabs.org
Signed-off-by: Elvira Khabirova 
Signed-off-by: Dmitry V. Levin 
---
v5: reverted to a simple approach, compile- and run-tested
v4: rewritten to call tracehook_report_syscall_entry() once, compile-tested
v3: add a descriptive comment
v2: explicitly ignore tracehook_report_syscall_entry() return code

 arch/powerpc/kernel/ptrace.c | 7 ++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/arch/powerpc/kernel/ptrace.c b/arch/powerpc/kernel/ptrace.c
index afb819f4ca68..714c3480c52d 100644
--- a/arch/powerpc/kernel/ptrace.c
+++ b/arch/powerpc/kernel/ptrace.c
@@ -3266,12 +3266,17 @@ long do_syscall_trace_enter(struct pt_regs *regs)
user_exit();
 
if (test_thread_flag(TIF_SYSCALL_EMU)) {
-   ptrace_report_syscall(regs);
/*
+* A nonzero return code from tracehook_report_syscall_entry()
+* tells us to prevent the syscall execution, but we are not
+* going to execute it anyway.
+*
 * Returning -1 will skip the syscall execution. We want to
 * avoid clobbering any register also, thus, not 'gotoing'
 * skip label.
 */
+   if (tracehook_report_syscall_entry(regs))
+   ;
return -1;
}
 
-- 
ldv


Re: [PATCH v4] powerpc/ptrace: replace ptrace_report_syscall() with a tracehook call

2018-12-07 Thread Dmitry V. Levin
On Fri, Dec 07, 2018 at 05:34:10PM +0100, Oleg Nesterov wrote:
> On 12/07, Dmitry V. Levin wrote:
> > On Fri, Dec 07, 2018 at 10:12:49PM +1100, Michael Ellerman wrote:
> >
> > > > Sorry, this patch does not work, please ignore it.
> > >
> > > Hmm OK. Why exactly?
> >
> > Unfortunately, I have no idea why it doesn't work.
> > All I can say is it breaks strace because the kernel no longer sends
> > syscall entry stops.
> 
> May be because TIF_SYSCALL_EMU/etc is a bit number, not a mask? IOW, rather
> than
> 
>   whatever & TIF_XXX
> 
> you should do
> 
>   whatever & _TIF_XXX
> 
> intstead?

Thanks Oleg, this was exactly the reason why it didn't work.
That kind of things happens when you let userspace people hack you kernel. :)


-- 
ldv


signature.asc
Description: PGP signature


[PATCH v6] powerpc/ptrace: replace ptrace_report_syscall() with a tracehook call

2018-12-07 Thread Dmitry V. Levin
From: Elvira Khabirova 

Arch code should use tracehook_*() helpers, as documented
in include/linux/tracehook.h,
ptrace_report_syscall() is not expected to be used outside that file.

Co-authored-by: Dmitry V. Levin 
Fixes: 5521eb4bca2d ("powerpc/ptrace: Add support for PTRACE_SYSEMU")
Cc: Michael Ellerman 
Cc: Benjamin Herrenschmidt 
Cc: Paul Mackerras 
Cc: Oleg Nesterov 
Cc: Breno Leitao 
Cc: Andy Lutomirski 
Cc: Eugene Syromyatnikov 
Cc: linuxppc-...@lists.ozlabs.org
Signed-off-by: Elvira Khabirova 
Signed-off-by: Dmitry V. Levin 
---
Please make either v5 or v6 edition of this fix, or any similar fix,
into v4.20.

v6: reverted to a fixed version of v4, compile- and run-tested with strace
v5: reverted to a simple approach, compile- and run-tested
v4: rewritten to call tracehook_report_syscall_entry() once, compile-tested
v3: add a descriptive comment
v2: explicitly ignore tracehook_report_syscall_entry() return code
 arch/powerpc/kernel/ptrace.c | 54 +++-
 1 file changed, 35 insertions(+), 19 deletions(-)

diff --git a/arch/powerpc/kernel/ptrace.c b/arch/powerpc/kernel/ptrace.c
index afb819f4ca68..fcfdc1229f08 100644
--- a/arch/powerpc/kernel/ptrace.c
+++ b/arch/powerpc/kernel/ptrace.c
@@ -3263,27 +3263,43 @@ static inline int do_seccomp(struct pt_regs *regs) { 
return 0; }
  */
 long do_syscall_trace_enter(struct pt_regs *regs)
 {
+   struct thread_info *ti;
+   u32 cached_flags;
+
user_exit();
 
-   if (test_thread_flag(TIF_SYSCALL_EMU)) {
-   ptrace_report_syscall(regs);
-   /*
-* Returning -1 will skip the syscall execution. We want to
-* avoid clobbering any register also, thus, not 'gotoing'
-* skip label.
-*/
-   return -1;
-   }
+   ti = current_thread_info();
+   cached_flags = READ_ONCE(ti->flags) &
+  (_TIF_SYSCALL_EMU | _TIF_SYSCALL_TRACE |
+   _TIF_SYSCALL_TRACEPOINT);
 
-   /*
-* The tracer may decide to abort the syscall, if so tracehook
-* will return !0. Note that the tracer may also just change
-* regs->gpr[0] to an invalid syscall number, that is handled
-* below on the exit path.
-*/
-   if (test_thread_flag(TIF_SYSCALL_TRACE) &&
-   tracehook_report_syscall_entry(regs))
-   goto skip;
+   if (cached_flags & (_TIF_SYSCALL_EMU | _TIF_SYSCALL_TRACE)) {
+   int rc = tracehook_report_syscall_entry(regs);
+
+   if (unlikely(cached_flags & _TIF_SYSCALL_EMU)) {
+   /*
+* A nonzero return code from
+* tracehook_report_syscall_entry() tells us
+* to prevent the syscall execution, but
+* we are not going to execute it anyway.
+*
+* Returning -1 will skip the syscall execution.
+* We want to avoid clobbering any register also,
+* thus, not 'gotoing' skip label.
+*/
+   return -1;
+   }
+
+   if (rc) {
+   /*
+* The tracer decided to abort the syscall.
+* Note that the tracer may also just change
+* regs->gpr[0] to an invalid syscall number,
+* that is handled below on the exit path.
+*/
+   goto skip;
+   }
+   }
 
/* Run seccomp after ptrace; allow it to set gpr[3]. */
if (do_seccomp(regs))
@@ -3293,7 +3309,7 @@ long do_syscall_trace_enter(struct pt_regs *regs)
if (regs->gpr[0] >= NR_syscalls)
goto skip;
 
-   if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
+   if (unlikely(cached_flags & _TIF_SYSCALL_TRACEPOINT))
trace_sys_enter(regs, regs->gpr[0]);
 
 #ifdef CONFIG_PPC64
-- 
ldv


[PATCH] selftests: do not macro-expand failed assertion expressions

2018-12-09 Thread Dmitry V. Levin
I've stumbled over the current macro-expand behaviour of the test
harness:

$ gcc -Wall -xc - <<'__EOF__'
TEST(macro) {
int status = 0;
ASSERT_TRUE(WIFSIGNALED(status));
}
TEST_HARNESS_MAIN
__EOF__
$ ./a.out
[==] Running 1 tests from 1 test cases.
[ RUN  ] global.macro
:4:global.macro:Expected 0 (0) != (((signed char) (((status) & 0x7f) + 
1) >> 1) > 0) (0)
global.macro: Test terminated by assertion
[ FAIL ] global.macro
[==] 0 / 1 tests passed.
[  FAILED  ]

With this change the output of the same test looks much more
comprehensible:

[==] Running 1 tests from 1 test cases.
[ RUN  ] global.macro
:4:global.macro:Expected 0 (0) != WIFSIGNALED(status) (0)
global.macro: Test terminated by assertion
[ FAIL ] global.macro
[==] 0 / 1 tests passed.
[  FAILED  ]

The issue is very similar to the bug fixed in glibc assert(3)
three years ago:
https://sourceware.org/bugzilla/show_bug.cgi?id=18604

Cc: Shuah Khan 
Cc: Kees Cook 
Cc: Andy Lutomirski 
Cc: Will Drewry 
Cc: linux-kselft...@vger.kernel.org
Signed-off-by: Dmitry V. Levin 
---
 tools/testing/selftests/kselftest_harness.h | 42 ++---
 1 file changed, 21 insertions(+), 21 deletions(-)

diff --git a/tools/testing/selftests/kselftest_harness.h 
b/tools/testing/selftests/kselftest_harness.h
index 6ae3730c4ee3..76d654ef3234 100644
--- a/tools/testing/selftests/kselftest_harness.h
+++ b/tools/testing/selftests/kselftest_harness.h
@@ -354,7 +354,7 @@
  * ASSERT_EQ(expected, measured): expected == measured
  */
 #define ASSERT_EQ(expected, seen) \
-   __EXPECT(expected, seen, ==, 1)
+   __EXPECT(expected, #expected, seen, #seen, ==, 1)
 
 /**
  * ASSERT_NE(expected, seen)
@@ -365,7 +365,7 @@
  * ASSERT_NE(expected, measured): expected != measured
  */
 #define ASSERT_NE(expected, seen) \
-   __EXPECT(expected, seen, !=, 1)
+   __EXPECT(expected, #expected, seen, #seen, !=, 1)
 
 /**
  * ASSERT_LT(expected, seen)
@@ -376,7 +376,7 @@
  * ASSERT_LT(expected, measured): expected < measured
  */
 #define ASSERT_LT(expected, seen) \
-   __EXPECT(expected, seen, <, 1)
+   __EXPECT(expected, #expected, seen, #seen, <, 1)
 
 /**
  * ASSERT_LE(expected, seen)
@@ -387,7 +387,7 @@
  * ASSERT_LE(expected, measured): expected <= measured
  */
 #define ASSERT_LE(expected, seen) \
-   __EXPECT(expected, seen, <=, 1)
+   __EXPECT(expected, #expected, seen, #seen, <=, 1)
 
 /**
  * ASSERT_GT(expected, seen)
@@ -398,7 +398,7 @@
  * ASSERT_GT(expected, measured): expected > measured
  */
 #define ASSERT_GT(expected, seen) \
-   __EXPECT(expected, seen, >, 1)
+   __EXPECT(expected, #expected, seen, #seen, >, 1)
 
 /**
  * ASSERT_GE(expected, seen)
@@ -409,7 +409,7 @@
  * ASSERT_GE(expected, measured): expected >= measured
  */
 #define ASSERT_GE(expected, seen) \
-   __EXPECT(expected, seen, >=, 1)
+   __EXPECT(expected, #expected, seen, #seen, >=, 1)
 
 /**
  * ASSERT_NULL(seen)
@@ -419,7 +419,7 @@
  * ASSERT_NULL(measured): NULL == measured
  */
 #define ASSERT_NULL(seen) \
-   __EXPECT(NULL, seen, ==, 1)
+   __EXPECT(NULL, "NULL", seen, #seen, ==, 1)
 
 /**
  * ASSERT_TRUE(seen)
@@ -429,7 +429,7 @@
  * ASSERT_TRUE(measured): measured != 0
  */
 #define ASSERT_TRUE(seen) \
-   ASSERT_NE(0, seen)
+   __EXPECT(0, "0", seen, #seen, !=, 1)
 
 /**
  * ASSERT_FALSE(seen)
@@ -439,7 +439,7 @@
  * ASSERT_FALSE(measured): measured == 0
  */
 #define ASSERT_FALSE(seen) \
-   ASSERT_EQ(0, seen)
+   __EXPECT(0, "0", seen, #seen, ==, 1)
 
 /**
  * ASSERT_STREQ(expected, seen)
@@ -472,7 +472,7 @@
  * EXPECT_EQ(expected, measured): expected == measured
  */
 #define EXPECT_EQ(expected, seen) \
-   __EXPECT(expected, seen, ==, 0)
+   __EXPECT(expected, #expected, seen, #seen, ==, 0)
 
 /**
  * EXPECT_NE(expected, seen)
@@ -483,7 +483,7 @@
  * EXPECT_NE(expected, measured): expected != measured
  */
 #define EXPECT_NE(expected, seen) \
-   __EXPECT(expected, seen, !=, 0)
+   __EXPECT(expected, #expected, seen, #seen, !=, 0)
 
 /**
  * EXPECT_LT(expected, seen)
@@ -494,7 +494,7 @@
  * EXPECT_LT(expected, measured): expected < measured
  */
 #define EXPECT_LT(expected, seen) \
-   __EXPECT(expected, seen, <, 0)
+   __EXPECT(expected, #expected, seen, #seen, <, 0)
 
 /**
  * EXPECT_LE(expected, seen)
@@ -505,7 +505,7 @@
  * EXPECT_LE(expected, measured): expected <= measured
  */
 #define EXPECT_LE(expected, seen) \
-   __EXPECT(expected, seen, <=, 0)
+   __EXPECT(expected, #expected, seen, #seen, <=, 0)
 
 /**
  * EXPECT_GT(expected, seen)
@@ -516,7 +516,7 @@
  * EXPECT_GT(expected, measured): expected > measured
  */
 #define EXPECT_GT(expected, seen) \
-   __EXPECT(expected, seen, >, 0)
+   __EXPECT(expected, #expected, seen, #seen, >, 0)
 
 /**
  * EXPECT_GE(expected, seen)
@@ -527

[PATCH v5 01/25] alpha: define remaining syscall_get_* functions

2018-12-09 Thread Dmitry V. Levin
syscall_get_* functions are required to be implemented on all
architectures in order to extend the generic ptrace API with
PTRACE_GET_SYSCALL_INFO request.

This adds remaining 4 syscall_get_* functions as documented
in asm-generic/syscall.h: syscall_get_nr, syscall_get_arguments,
syscall_get_error, and syscall_get_return_value.

Cc: Richard Henderson 
Cc: Ivan Kokshaysky 
Cc: Matt Turner 
Cc: Oleg Nesterov 
Cc: Andy Lutomirski 
Cc: Elvira Khabirova 
Cc: Eugene Syromyatnikov 
Cc: linux-al...@vger.kernel.org
Signed-off-by: Dmitry V. Levin 
---
 arch/alpha/include/asm/syscall.h | 29 -
 1 file changed, 28 insertions(+), 1 deletion(-)

diff --git a/arch/alpha/include/asm/syscall.h b/arch/alpha/include/asm/syscall.h
index d73a6fcb519c..437758bdc49f 100644
--- a/arch/alpha/include/asm/syscall.h
+++ b/arch/alpha/include/asm/syscall.h
@@ -4,7 +4,34 @@
 
 #include 
 
-static inline int syscall_get_arch(void)
+static inline int
+syscall_get_nr(struct task_struct *task, struct pt_regs *regs)
+{
+   return regs->r0;
+}
+
+static inline void
+syscall_get_arguments(struct task_struct *task, struct pt_regs *regs,
+ unsigned int i, unsigned int n, unsigned long *args)
+{
+   BUG_ON(i + n > 6);
+   memcpy(args, ®s->r16 + i, n * sizeof(args[0]));
+}
+
+static inline long
+syscall_get_error(struct task_struct *task, struct pt_regs *regs)
+{
+   return regs->r19 ? -regs->r0 : 0;
+}
+
+static inline long
+syscall_get_return_value(struct task_struct *task, struct pt_regs *regs)
+{
+   return regs->r0;
+}
+
+static inline int
+syscall_get_arch(void)
 {
return AUDIT_ARCH_ALPHA;
 }
-- 
ldv


[PATCH v5 02/25] Move EM_ARCOMPACT and EM_ARCV2 to uapi/linux/elf-em.h

2018-12-09 Thread Dmitry V. Levin
These should never have been defined in the arch tree to begin with, and
now uapi/linux/audit.h header is going to use EM_ARCOMPACT and EM_ARCV2
in order to define AUDIT_ARCH_ARCOMPACT and AUDIT_ARCH_ARCV2 which are
needed to implement syscall_get_arch() which in turn is required to
extend the generic ptrace API with PTRACE_GET_SYSCALL_INFO request.

Acked-by: Vineet Gupta 
Cc: Elvira Khabirova 
Cc: Eugene Syromyatnikov 
Cc: Oleg Nesterov 
Cc: Andy Lutomirski 
Cc: Alexey Brodkin 
Cc: linux-snps-...@lists.infradead.org
Signed-off-by: Dmitry V. Levin 
---

Notes:
v5: added Cc
v2: added Acked-by

 arch/arc/include/asm/elf.h  | 6 +-
 include/uapi/linux/elf-em.h | 2 ++
 2 files changed, 3 insertions(+), 5 deletions(-)

diff --git a/arch/arc/include/asm/elf.h b/arch/arc/include/asm/elf.h
index aa2d6da9d187..2b80c184c9c8 100644
--- a/arch/arc/include/asm/elf.h
+++ b/arch/arc/include/asm/elf.h
@@ -10,13 +10,9 @@
 #define __ASM_ARC_ELF_H
 
 #include 
+#include 
 #include 
 
-/* These ELF defines belong to uapi but libc elf.h already defines them */
-#define EM_ARCOMPACT   93
-
-#define EM_ARCV2   195 /* ARCv2 Cores */
-
 #define EM_ARC_INUSE   (IS_ENABLED(CONFIG_ISA_ARCOMPACT) ? \
EM_ARCOMPACT : EM_ARCV2)
 
diff --git a/include/uapi/linux/elf-em.h b/include/uapi/linux/elf-em.h
index 93722e60204c..42b7546352a6 100644
--- a/include/uapi/linux/elf-em.h
+++ b/include/uapi/linux/elf-em.h
@@ -34,6 +34,7 @@
 #define EM_M32R88  /* Renesas M32R */
 #define EM_MN10300 89  /* Panasonic/MEI MN10300, AM33 */
 #define EM_OPENRISC 92 /* OpenRISC 32-bit embedded processor */
+#define EM_ARCOMPACT   93  /* ARCompact processor */
 #define EM_BLACKFIN 106 /* ADI Blackfin Processor */
 #define EM_ALTERA_NIOS2113 /* Altera Nios II soft-core processor */
 #define EM_TI_C6000140 /* TI C6X DSPs */
@@ -41,6 +42,7 @@
 #define EM_TILEPRO 188 /* Tilera TILEPro */
 #define EM_MICROBLAZE  189 /* Xilinx MicroBlaze */
 #define EM_TILEGX  191 /* Tilera TILE-Gx */
+#define EM_ARCV2   195 /* ARCv2 Cores */
 #define EM_RISCV   243 /* RISC-V */
 #define EM_BPF 247 /* Linux BPF - in-kernel virtual machine */
 #define EM_FRV 0x5441  /* Fujitsu FR-V */
-- 
ldv


[PATCH v5 06/25] csky: define syscall_get_arch()

2018-12-09 Thread Dmitry V. Levin
syscall_get_arch() is required to be implemented on all architectures
in order to extend the generic ptrace API with PTRACE_GET_SYSCALL_INFO
request.

Cc: Guo Ren 
Cc: Paul Moore 
Cc: Eric Paris 
Cc: Oleg Nesterov 
Cc: Andy Lutomirski 
Cc: Elvira Khabirova 
Cc: Eugene Syromyatnikov 
Cc: linux-au...@redhat.com
Signed-off-by: Dmitry V. Levin 
---
 arch/csky/include/asm/syscall.h | 7 +++
 include/uapi/linux/audit.h  | 1 +
 2 files changed, 8 insertions(+)

diff --git a/arch/csky/include/asm/syscall.h b/arch/csky/include/asm/syscall.h
index 926a64a8b4ee..d637445737b7 100644
--- a/arch/csky/include/asm/syscall.h
+++ b/arch/csky/include/asm/syscall.h
@@ -6,6 +6,7 @@
 #include 
 #include 
 #include 
+#include 
 
 static inline int
 syscall_get_nr(struct task_struct *task, struct pt_regs *regs)
@@ -68,4 +69,10 @@ syscall_set_arguments(struct task_struct *task, struct 
pt_regs *regs,
memcpy(®s->a1 + i * sizeof(regs->a1), args, n * sizeof(regs->a0));
 }
 
+static inline int
+syscall_get_arch(void)
+{
+   return AUDIT_ARCH_CSKY;
+}
+
 #endif /* __ASM_SYSCALL_H */
diff --git a/include/uapi/linux/audit.h b/include/uapi/linux/audit.h
index 72aeea0a740d..55904a40d768 100644
--- a/include/uapi/linux/audit.h
+++ b/include/uapi/linux/audit.h
@@ -384,6 +384,7 @@ enum {
 #define AUDIT_ARCH_C6X (EM_TI_C6000|__AUDIT_ARCH_LE)
 #define AUDIT_ARCH_C6XBE   (EM_TI_C6000)
 #define AUDIT_ARCH_CRIS(EM_CRIS|__AUDIT_ARCH_LE)
+#define AUDIT_ARCH_CSKY(EM_CSKY|__AUDIT_ARCH_LE)
 #define AUDIT_ARCH_FRV (EM_FRV)
 #define AUDIT_ARCH_I386(EM_386|__AUDIT_ARCH_LE)
 #define AUDIT_ARCH_IA64
(EM_IA_64|__AUDIT_ARCH_64BIT|__AUDIT_ARCH_LE)
-- 
ldv


[PATCH v5 04/25] c6x: define syscall_get_arch()

2018-12-09 Thread Dmitry V. Levin
syscall_get_arch() is required to be implemented on all architectures
in addition to already implemented syscall_get_nr(),
syscall_get_arguments(), syscall_get_error(), and
syscall_get_return_value() functions in order to extend the generic
ptrace API with PTRACE_GET_SYSCALL_INFO request.

Cc: Mark Salter 
Cc: Aurelien Jacquiot 
Cc: Paul Moore 
Cc: Eric Paris 
Cc: Oleg Nesterov 
Cc: Andy Lutomirski 
Cc: Elvira Khabirova 
Cc: Eugene Syromyatnikov 
Cc: linux-c6x-...@linux-c6x.org
Cc: linux-au...@redhat.com
Signed-off-by: Dmitry V. Levin 
---

Notes:
v5: added Cc

 arch/c6x/include/asm/syscall.h | 7 +++
 include/uapi/linux/audit.h | 2 ++
 2 files changed, 9 insertions(+)

diff --git a/arch/c6x/include/asm/syscall.h b/arch/c6x/include/asm/syscall.h
index ae2be315ee9c..39dbd1ef994c 100644
--- a/arch/c6x/include/asm/syscall.h
+++ b/arch/c6x/include/asm/syscall.h
@@ -11,6 +11,7 @@
 #ifndef __ASM_C6X_SYSCALL_H
 #define __ASM_C6X_SYSCALL_H
 
+#include 
 #include 
 #include 
 
@@ -120,4 +121,10 @@ static inline void syscall_set_arguments(struct 
task_struct *task,
}
 }
 
+static inline int syscall_get_arch(void)
+{
+   return IS_ENABLED(CONFIG_CPU_BIG_ENDIAN)
+   ? AUDIT_ARCH_C6XBE : AUDIT_ARCH_C6X;
+}
+
 #endif /* __ASM_C6X_SYSCALLS_H */
diff --git a/include/uapi/linux/audit.h b/include/uapi/linux/audit.h
index bedf3bf54c3a..72aeea0a740d 100644
--- a/include/uapi/linux/audit.h
+++ b/include/uapi/linux/audit.h
@@ -381,6 +381,8 @@ enum {
 #define AUDIT_ARCH_ARCV2BE (EM_ARCV2)
 #define AUDIT_ARCH_ARM (EM_ARM|__AUDIT_ARCH_LE)
 #define AUDIT_ARCH_ARMEB   (EM_ARM)
+#define AUDIT_ARCH_C6X (EM_TI_C6000|__AUDIT_ARCH_LE)
+#define AUDIT_ARCH_C6XBE   (EM_TI_C6000)
 #define AUDIT_ARCH_CRIS(EM_CRIS|__AUDIT_ARCH_LE)
 #define AUDIT_ARCH_FRV (EM_FRV)
 #define AUDIT_ARCH_I386(EM_386|__AUDIT_ARCH_LE)
-- 
ldv


[PATCH v5 03/25] arc: define syscall_get_arch()

2018-12-09 Thread Dmitry V. Levin
syscall_get_arch() is required to be implemented on all architectures
in addition to already implemented syscall_get_nr(),
syscall_get_arguments(), syscall_get_error(), and
syscall_get_return_value() functions in order to extend the generic
ptrace API with PTRACE_GET_SYSCALL_INFO request.

Acked-by: Vineet Gupta 
Cc: Elvira Khabirova 
Cc: Eugene Syromyatnikov 
Cc: Oleg Nesterov 
Cc: Andy Lutomirski 
Cc: Alexey Brodkin 
Cc: Paul Moore 
Cc: Eric Paris 
Cc: linux-snps-...@lists.infradead.org
Cc: linux-au...@redhat.com
Signed-off-by: Dmitry V. Levin 
---

Notes:
v5: added Cc
v2: added Acked-by

 arch/arc/include/asm/syscall.h | 11 +++
 include/uapi/linux/audit.h |  4 
 2 files changed, 15 insertions(+)

diff --git a/arch/arc/include/asm/syscall.h b/arch/arc/include/asm/syscall.h
index 29de09804306..c7fc4c0c3bcb 100644
--- a/arch/arc/include/asm/syscall.h
+++ b/arch/arc/include/asm/syscall.h
@@ -9,6 +9,7 @@
 #ifndef _ASM_ARC_SYSCALL_H
 #define _ASM_ARC_SYSCALL_H  1
 
+#include 
 #include 
 #include 
 #include 
@@ -68,4 +69,14 @@ syscall_get_arguments(struct task_struct *task, struct 
pt_regs *regs,
}
 }
 
+static inline int
+syscall_get_arch(void)
+{
+   return IS_ENABLED(CONFIG_ISA_ARCOMPACT)
+   ? (IS_ENABLED(CONFIG_CPU_BIG_ENDIAN)
+   ? AUDIT_ARCH_ARCOMPACTBE : AUDIT_ARCH_ARCOMPACT)
+   : (IS_ENABLED(CONFIG_CPU_BIG_ENDIAN)
+   ? AUDIT_ARCH_ARCV2BE : AUDIT_ARCH_ARCV2);
+}
+
 #endif
diff --git a/include/uapi/linux/audit.h b/include/uapi/linux/audit.h
index 818ae690ab79..bedf3bf54c3a 100644
--- a/include/uapi/linux/audit.h
+++ b/include/uapi/linux/audit.h
@@ -375,6 +375,10 @@ enum {
 
 #define AUDIT_ARCH_AARCH64 (EM_AARCH64|__AUDIT_ARCH_64BIT|__AUDIT_ARCH_LE)
 #define AUDIT_ARCH_ALPHA   (EM_ALPHA|__AUDIT_ARCH_64BIT|__AUDIT_ARCH_LE)
+#define AUDIT_ARCH_ARCOMPACT   (EM_ARCOMPACT|__AUDIT_ARCH_LE)
+#define AUDIT_ARCH_ARCOMPACTBE (EM_ARCOMPACT)
+#define AUDIT_ARCH_ARCV2   (EM_ARCV2|__AUDIT_ARCH_LE)
+#define AUDIT_ARCH_ARCV2BE (EM_ARCV2)
 #define AUDIT_ARCH_ARM (EM_ARM|__AUDIT_ARCH_LE)
 #define AUDIT_ARCH_ARMEB   (EM_ARM)
 #define AUDIT_ARCH_CRIS(EM_CRIS|__AUDIT_ARCH_LE)
-- 
ldv


[PATCH v5 05/25] elf-em.h: add EM_CSKY

2018-12-09 Thread Dmitry V. Levin
The uapi/linux/audit.h header is going to use EM_CSKY in order
to define AUDIT_ARCH_CSKY which is needed to implement
syscall_get_arch() which in turn is required to extend
the generic ptrace API with PTRACE_GET_SYSCALL_INFO request.

The value for EM_CSKY has been taken from arch/csky/include/asm/elf.h
and confirmed by binutils:include/elf/common.h

Cc: Guo Ren 
Cc: Oleg Nesterov 
Cc: Andy Lutomirski 
Cc: Elvira Khabirova 
Cc: Eugene Syromyatnikov 
Signed-off-by: Dmitry V. Levin 
---
 include/uapi/linux/elf-em.h | 1 +
 1 file changed, 1 insertion(+)

diff --git a/include/uapi/linux/elf-em.h b/include/uapi/linux/elf-em.h
index 42b7546352a6..ee0b26ab92b0 100644
--- a/include/uapi/linux/elf-em.h
+++ b/include/uapi/linux/elf-em.h
@@ -45,6 +45,7 @@
 #define EM_ARCV2   195 /* ARCv2 Cores */
 #define EM_RISCV   243 /* RISC-V */
 #define EM_BPF 247 /* Linux BPF - in-kernel virtual machine */
+#define EM_CSKY252 /* C-SKY processor family */
 #define EM_FRV 0x5441  /* Fujitsu FR-V */
 
 /*
-- 
ldv


[PATCH v5 07/25] h8300: define remaining syscall_get_* functions

2018-12-09 Thread Dmitry V. Levin
syscall_get_* functions are required to be implemented on all
architectures in order to extend the generic ptrace API with
PTRACE_GET_SYSCALL_INFO request.

This adds remaining 3 syscall_get_* functions as documented in
asm-generic/syscall.h: syscall_get_error, syscall_get_return_value,
and syscall_get_arch.

Cc: Yoshinori Sato 
Cc: Paul Moore 
Cc: Eric Paris 
Cc: Oleg Nesterov 
Cc: Andy Lutomirski 
Cc: Elvira Khabirova 
Cc: Eugene Syromyatnikov 
Cc: uclinux-h8-de...@lists.sourceforge.jp
Cc: linux-au...@redhat.com
Signed-off-by: Dmitry V. Levin 
---

Notes:
v5: added syscall_get_error and syscall_get_return_value

 arch/h8300/include/asm/syscall.h | 18 ++
 include/uapi/linux/audit.h   |  1 +
 2 files changed, 19 insertions(+)

diff --git a/arch/h8300/include/asm/syscall.h b/arch/h8300/include/asm/syscall.h
index 924990401237..5c881ffe962a 100644
--- a/arch/h8300/include/asm/syscall.h
+++ b/arch/h8300/include/asm/syscall.h
@@ -8,6 +8,7 @@
 #include 
 #include 
 #include 
+#include 
 
 static inline int
 syscall_get_nr(struct task_struct *task, struct pt_regs *regs)
@@ -47,6 +48,23 @@ syscall_get_arguments(struct task_struct *task, struct 
pt_regs *regs,
}
 }
 
+static inline long
+syscall_get_error(struct task_struct *task, struct pt_regs *regs)
+{
+   return IS_ERR_VALUE(regs->er0) ? regs->er0 : 0;
+}
+
+static inline long
+syscall_get_return_value(struct task_struct *task, struct pt_regs *regs)
+{
+   return regs->er0;
+}
+
+static inline int
+syscall_get_arch(void)
+{
+   return AUDIT_ARCH_H8300;
+}
 
 
 /* Misc syscall related bits */
diff --git a/include/uapi/linux/audit.h b/include/uapi/linux/audit.h
index 55904a40d768..672c6d9d7577 100644
--- a/include/uapi/linux/audit.h
+++ b/include/uapi/linux/audit.h
@@ -386,6 +386,7 @@ enum {
 #define AUDIT_ARCH_CRIS(EM_CRIS|__AUDIT_ARCH_LE)
 #define AUDIT_ARCH_CSKY(EM_CSKY|__AUDIT_ARCH_LE)
 #define AUDIT_ARCH_FRV (EM_FRV)
+#define AUDIT_ARCH_H8300   (EM_H8_300)
 #define AUDIT_ARCH_I386(EM_386|__AUDIT_ARCH_LE)
 #define AUDIT_ARCH_IA64
(EM_IA_64|__AUDIT_ARCH_64BIT|__AUDIT_ARCH_LE)
 #define AUDIT_ARCH_M32R(EM_M32R)
-- 
ldv


[PATCH v5 08/25] Move EM_HEXAGON to uapi/linux/elf-em.h

2018-12-09 Thread Dmitry V. Levin
This should never have been defined in the arch tree to begin with,
and now uapi/linux/audit.h header is going to use EM_HEXAGON
in order to define AUDIT_ARCH_HEXAGON which is needed to implement
syscall_get_arch() which in turn is required to extend
the generic ptrace API with PTRACE_GET_SYSCALL_INFO request.

Cc: Richard Kuo 
Cc: Oleg Nesterov 
Cc: Andy Lutomirski 
Cc: Elvira Khabirova 
Cc: Eugene Syromyatnikov 
Cc: linux-hexa...@vger.kernel.org
Signed-off-by: Dmitry V. Levin 
---

Notes:
v5: added Cc

 arch/hexagon/include/asm/elf.h | 6 +-
 include/uapi/linux/elf-em.h| 1 +
 2 files changed, 2 insertions(+), 5 deletions(-)

diff --git a/arch/hexagon/include/asm/elf.h b/arch/hexagon/include/asm/elf.h
index 80311e7b8ca6..d10fbd54ae51 100644
--- a/arch/hexagon/include/asm/elf.h
+++ b/arch/hexagon/include/asm/elf.h
@@ -23,11 +23,7 @@
 
 #include 
 #include 
-
-/*
- * This should really be in linux/elf-em.h.
- */
-#define EM_HEXAGON 164   /* QUALCOMM Hexagon */
+#include 
 
 struct elf32_hdr;
 
diff --git a/include/uapi/linux/elf-em.h b/include/uapi/linux/elf-em.h
index ee0b26ab92b0..e0fb2794bbad 100644
--- a/include/uapi/linux/elf-em.h
+++ b/include/uapi/linux/elf-em.h
@@ -38,6 +38,7 @@
 #define EM_BLACKFIN 106 /* ADI Blackfin Processor */
 #define EM_ALTERA_NIOS2113 /* Altera Nios II soft-core processor */
 #define EM_TI_C6000140 /* TI C6X DSPs */
+#define EM_HEXAGON 164 /* QUALCOMM Hexagon */
 #define EM_AARCH64 183 /* ARM 64 bit */
 #define EM_TILEPRO 188 /* Tilera TILEPro */
 #define EM_MICROBLAZE  189 /* Xilinx MicroBlaze */
-- 
ldv


[PATCH v5 09/25] hexagon: define remaining syscall_get_* functions

2018-12-09 Thread Dmitry V. Levin
syscall_get_* functions are required to be implemented on all
architectures in order to extend the generic ptrace API with
PTRACE_GET_SYSCALL_INFO request.

This adds remaining 3 syscall_get_* functions as documented in
asm-generic/syscall.h: syscall_get_error, syscall_get_return_value,
and syscall_get_arch.

Cc: Richard Kuo 
Cc: Paul Moore 
Cc: Eric Paris 
Cc: Oleg Nesterov 
Cc: Andy Lutomirski 
Cc: Elvira Khabirova 
Cc: Eugene Syromyatnikov 
Cc: linux-hexa...@vger.kernel.org
Cc: linux-au...@redhat.com
Signed-off-by: Dmitry V. Levin 
---

Notes:
v5: added syscall_get_error and syscall_get_return_value

 arch/hexagon/include/asm/syscall.h | 20 
 include/uapi/linux/audit.h |  1 +
 2 files changed, 21 insertions(+)

diff --git a/arch/hexagon/include/asm/syscall.h 
b/arch/hexagon/include/asm/syscall.h
index 4af9c7b6f13a..09c7b2884475 100644
--- a/arch/hexagon/include/asm/syscall.h
+++ b/arch/hexagon/include/asm/syscall.h
@@ -21,6 +21,8 @@
 #ifndef _ASM_HEXAGON_SYSCALL_H
 #define _ASM_HEXAGON_SYSCALL_H
 
+#include 
+
 typedef long (*syscall_fn)(unsigned long, unsigned long,
unsigned long, unsigned long,
unsigned long, unsigned long);
@@ -43,4 +45,22 @@ static inline void syscall_get_arguments(struct task_struct 
*task,
BUG_ON(i + n > 6);
memcpy(args, &(®s->r00)[i], n * sizeof(args[0]));
 }
+
+static inline long syscall_get_error(struct task_struct *task,
+struct pt_regs *regs)
+{
+   return IS_ERR_VALUE(regs->r00) ? regs->r00 : 0;
+}
+
+static inline long syscall_get_return_value(struct task_struct *task,
+   struct pt_regs *regs)
+{
+   return regs->r00;
+}
+
+static inline int syscall_get_arch(void)
+{
+   return AUDIT_ARCH_HEXAGON;
+}
+
 #endif
diff --git a/include/uapi/linux/audit.h b/include/uapi/linux/audit.h
index 672c6d9d7577..b8e848736031 100644
--- a/include/uapi/linux/audit.h
+++ b/include/uapi/linux/audit.h
@@ -387,6 +387,7 @@ enum {
 #define AUDIT_ARCH_CSKY(EM_CSKY|__AUDIT_ARCH_LE)
 #define AUDIT_ARCH_FRV (EM_FRV)
 #define AUDIT_ARCH_H8300   (EM_H8_300)
+#define AUDIT_ARCH_HEXAGON (EM_HEXAGON)
 #define AUDIT_ARCH_I386(EM_386|__AUDIT_ARCH_LE)
 #define AUDIT_ARCH_IA64
(EM_IA_64|__AUDIT_ARCH_64BIT|__AUDIT_ARCH_LE)
 #define AUDIT_ARCH_M32R(EM_M32R)
-- 
ldv


[PATCH v5 14/25] mips: define syscall_get_error()

2018-12-09 Thread Dmitry V. Levin
syscall_get_error() is required to be implemented on all
architectures in addition to already implemented syscall_get_nr(),
syscall_get_arguments(), syscall_get_return_value(), and
syscall_get_arch() functions in order to extend the generic
ptrace API with PTRACE_GET_SYSCALL_INFO request.

Cc: Paul Burton 
Cc: Ralf Baechle 
Cc: James Hogan 
Cc: Oleg Nesterov 
Cc: Andy Lutomirski 
Cc: Elvira Khabirova 
Cc: Eugene Syromyatnikov 
Cc: linux-m...@vger.kernel.org
Signed-off-by: Dmitry V. Levin 
---
 arch/mips/include/asm/syscall.h | 6 ++
 1 file changed, 6 insertions(+)

diff --git a/arch/mips/include/asm/syscall.h b/arch/mips/include/asm/syscall.h
index 6cf8ffb5367e..04ab927ff47d 100644
--- a/arch/mips/include/asm/syscall.h
+++ b/arch/mips/include/asm/syscall.h
@@ -89,6 +89,12 @@ static inline unsigned long mips_get_syscall_arg(unsigned 
long *arg,
unreachable();
 }
 
+static inline long syscall_get_error(struct task_struct *task,
+struct pt_regs *regs)
+{
+   return regs->regs[7] ? -regs->regs[2] : 0;
+}
+
 static inline long syscall_get_return_value(struct task_struct *task,
struct pt_regs *regs)
 {
-- 
ldv


[PATCH v5 15/25] parisc: define syscall_get_error()

2018-12-09 Thread Dmitry V. Levin
syscall_get_error() is required to be implemented on all
architectures in addition to already implemented syscall_get_nr(),
syscall_get_arguments(), syscall_get_return_value(), and
syscall_get_arch() functions in order to extend the generic
ptrace API with PTRACE_GET_SYSCALL_INFO request.

Cc: Helge Deller 
Cc: James E.J. Bottomley 
Cc: Oleg Nesterov 
Cc: Andy Lutomirski 
Cc: Elvira Khabirova 
Cc: Eugene Syromyatnikov 
Cc: linux-par...@vger.kernel.org
Signed-off-by: Dmitry V. Levin 
---
 arch/parisc/include/asm/syscall.h | 7 +++
 1 file changed, 7 insertions(+)

diff --git a/arch/parisc/include/asm/syscall.h 
b/arch/parisc/include/asm/syscall.h
index 8bff1a58c97f..477511ff7546 100644
--- a/arch/parisc/include/asm/syscall.h
+++ b/arch/parisc/include/asm/syscall.h
@@ -43,6 +43,13 @@ static inline void syscall_get_arguments(struct task_struct 
*tsk,
}
 }
 
+static inline long syscall_get_error(struct task_struct *task,
+struct pt_regs *regs)
+{
+   unsigned long error = regs->gr[28];
+   return IS_ERR_VALUE(error) ? error : 0;
+}
+
 static inline long syscall_get_return_value(struct task_struct *task,
struct pt_regs *regs)
 {
-- 
ldv


[PATCH v5 17/25] riscv: define syscall_get_arch()

2018-12-09 Thread Dmitry V. Levin
syscall_get_arch() is required to be implemented on all architectures
in addition to already implemented syscall_get_nr(),
syscall_get_arguments(), syscall_get_error(), and
syscall_get_return_value() functions in order to extend the generic
ptrace API with PTRACE_GET_SYSCALL_INFO request.

Based-on-patch-by: David Abdurachmanov 
Reviewed-by: Palmer Dabbelt 
Cc: Elvira Khabirova 
Cc: Eugene Syromyatnikov 
Cc: Oleg Nesterov 
Cc: Andy Lutomirski 
Cc: Albert Ou 
Cc: Paul Moore 
Cc: Eric Paris 
Cc: linux-ri...@lists.infradead.org
Cc: linux-au...@redhat.com
Signed-off-by: Dmitry V. Levin 
---

Notes:
v5: added Cc
v2: added Reviewed-by

 arch/riscv/include/asm/syscall.h | 10 ++
 include/uapi/linux/audit.h   |  2 ++
 2 files changed, 12 insertions(+)

diff --git a/arch/riscv/include/asm/syscall.h b/arch/riscv/include/asm/syscall.h
index 8d25f8904c00..bba3da6ef157 100644
--- a/arch/riscv/include/asm/syscall.h
+++ b/arch/riscv/include/asm/syscall.h
@@ -18,6 +18,7 @@
 #ifndef _ASM_RISCV_SYSCALL_H
 #define _ASM_RISCV_SYSCALL_H
 
+#include 
 #include 
 #include 
 
@@ -99,4 +100,13 @@ static inline void syscall_set_arguments(struct task_struct 
*task,
memcpy(®s->a1 + i * sizeof(regs->a1), args, n * sizeof(regs->a0));
 }
 
+static inline int syscall_get_arch(void)
+{
+#ifdef CONFIG_64BIT
+   return AUDIT_ARCH_RISCV64;
+#else
+   return AUDIT_ARCH_RISCV32;
+#endif
+}
+
 #endif /* _ASM_RISCV_SYSCALL_H */
diff --git a/include/uapi/linux/audit.h b/include/uapi/linux/audit.h
index 883c5f56be9c..1e9808f3a240 100644
--- a/include/uapi/linux/audit.h
+++ b/include/uapi/linux/audit.h
@@ -411,6 +411,8 @@ enum {
 /* do not define AUDIT_ARCH_PPCLE since it is not supported by audit */
 #define AUDIT_ARCH_PPC64   (EM_PPC64|__AUDIT_ARCH_64BIT)
 #define AUDIT_ARCH_PPC64LE (EM_PPC64|__AUDIT_ARCH_64BIT|__AUDIT_ARCH_LE)
+#define AUDIT_ARCH_RISCV32 (EM_RISCV|__AUDIT_ARCH_LE)
+#define AUDIT_ARCH_RISCV64 (EM_RISCV|__AUDIT_ARCH_64BIT|__AUDIT_ARCH_LE)
 #define AUDIT_ARCH_S390(EM_S390)
 #define AUDIT_ARCH_S390X   (EM_S390|__AUDIT_ARCH_64BIT)
 #define AUDIT_ARCH_SH  (EM_SH)
-- 
ldv


[PATCH v5 18/25] Move EM_XTENSA to uapi/linux/elf-em.h

2018-12-09 Thread Dmitry V. Levin
This should never have been defined in the arch tree to begin with,
and now uapi/linux/audit.h header is going to use EM_XTENSA
in order to define AUDIT_ARCH_XTENSA which is needed to implement
syscall_get_arch() which in turn is required to extend
the generic ptrace API with PTRACE_GET_SYSCALL_INFO request.

Cc: Max Filippov 
Cc: Oleg Nesterov 
Cc: Andy Lutomirski 
Cc: Elvira Khabirova 
Cc: Eugene Syromyatnikov 
Cc: Chris Zankel 
Cc: linux-xte...@linux-xtensa.org
Signed-off-by: Dmitry V. Levin 
---

Notes:
v5: move EM_XTENSA to elf-em.h instead of adding,
I didn't realize it was already defined in the arch tree
v2: added Reviewed-by

 arch/xtensa/include/asm/elf.h | 2 +-
 include/uapi/linux/elf-em.h   | 1 +
 2 files changed, 2 insertions(+), 1 deletion(-)

diff --git a/arch/xtensa/include/asm/elf.h b/arch/xtensa/include/asm/elf.h
index eacb25a41718..344f1c6a546e 100644
--- a/arch/xtensa/include/asm/elf.h
+++ b/arch/xtensa/include/asm/elf.h
@@ -15,10 +15,10 @@
 
 #include 
 #include 
+#include 
 
 /* Xtensa processor ELF architecture-magic number */
 
-#define EM_XTENSA  94
 #define EM_XTENSA_OLD  0xABC7
 
 /* Xtensa relocations defined by the ABIs */
diff --git a/include/uapi/linux/elf-em.h b/include/uapi/linux/elf-em.h
index d8695ad90f6b..f392dd9d07f7 100644
--- a/include/uapi/linux/elf-em.h
+++ b/include/uapi/linux/elf-em.h
@@ -35,6 +35,7 @@
 #define EM_MN10300 89  /* Panasonic/MEI MN10300, AM33 */
 #define EM_OPENRISC 92 /* OpenRISC 32-bit embedded processor */
 #define EM_ARCOMPACT   93  /* ARCompact processor */
+#define EM_XTENSA  94  /* Tensilica Xtensa Architecture */
 #define EM_BLACKFIN 106 /* ADI Blackfin Processor */
 #define EM_ALTERA_NIOS2113 /* Altera Nios II soft-core processor */
 #define EM_TI_C6000140 /* TI C6X DSPs */
-- 
ldv


[PATCH v5 16/25] powerpc: define syscall_get_error()

2018-12-09 Thread Dmitry V. Levin
syscall_get_error() is required to be implemented on this
architecture in addition to already implemented syscall_get_nr(),
syscall_get_arguments(), syscall_get_return_value(), and
syscall_get_arch() functions in order to extend the generic
ptrace API with PTRACE_GET_SYSCALL_INFO request.

Cc: Michael Ellerman 
Cc: Benjamin Herrenschmidt 
Cc: Paul Mackerras 
Cc: Oleg Nesterov 
Cc: Andy Lutomirski 
Cc: Elvira Khabirova 
Cc: Eugene Syromyatnikov 
Cc: linuxppc-...@lists.ozlabs.org
Signed-off-by: Dmitry V. Levin 
---

Notes:
This change has been tested with
tools/testing/selftests/ptrace/get_syscall_info.c and strace,
so it's correct from PTRACE_GET_SYSCALL_INFO point of view.

This cast doubts on commit v4.3-rc1~86^2~81 that changed
syscall_set_return_value() in a way that doesn't quite match
syscall_get_error(), but syscall_set_return_value() is out
of scope of this series, so I just air my concerns.

 arch/powerpc/include/asm/syscall.h | 10 ++
 1 file changed, 10 insertions(+)

diff --git a/arch/powerpc/include/asm/syscall.h 
b/arch/powerpc/include/asm/syscall.h
index ab9f3f0a8637..1d03e753391d 100644
--- a/arch/powerpc/include/asm/syscall.h
+++ b/arch/powerpc/include/asm/syscall.h
@@ -39,6 +39,16 @@ static inline void syscall_rollback(struct task_struct *task,
regs->gpr[3] = regs->orig_gpr3;
 }
 
+static inline long syscall_get_error(struct task_struct *task,
+struct pt_regs *regs)
+{
+   /*
+* If the system call failed,
+* regs->gpr[3] contains a positive ERRORCODE.
+*/
+   return (regs->ccr & 0x1000UL) ? -regs->gpr[3] : 0;
+}
+
 static inline long syscall_get_return_value(struct task_struct *task,
struct pt_regs *regs)
 {
-- 
ldv


[PATCH v5 13/25] m68k: add asm/syscall.h

2018-12-09 Thread Dmitry V. Levin
syscall_get_* functions are required to be implemented on all
architectures in order to extend the generic ptrace API with
PTRACE_GET_SYSCALL_INFO request.

This introduces asm/syscall.h on m68k implementing all 5 syscall_get_*
functions as documented in asm-generic/syscall.h: syscall_get_nr,
syscall_get_arguments, syscall_get_error, syscall_get_return_value,
and syscall_get_arch.

Cc: Geert Uytterhoeven 
Cc: Oleg Nesterov 
Cc: Andy Lutomirski 
Cc: Elvira Khabirova 
Cc: Eugene Syromyatnikov 
Cc: linux-m...@lists.linux-m68k.org
Signed-off-by: Dmitry V. Levin 
---

Notes:
v5: added syscall_get_nr, syscall_get_arguments, syscall_get_error,
and syscall_get_return_value
v1: added syscall_get_arch

 arch/m68k/include/asm/syscall.h | 39 +
 1 file changed, 39 insertions(+)
 create mode 100644 arch/m68k/include/asm/syscall.h

diff --git a/arch/m68k/include/asm/syscall.h b/arch/m68k/include/asm/syscall.h
new file mode 100644
index ..75a24cf90620
--- /dev/null
+++ b/arch/m68k/include/asm/syscall.h
@@ -0,0 +1,39 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _ASM_M68K_SYSCALL_H
+#define _ASM_M68K_SYSCALL_H
+
+#include 
+
+static inline int
+syscall_get_nr(struct task_struct *task, struct pt_regs *regs)
+{
+   return regs->orig_d0;
+}
+
+static inline void
+syscall_get_arguments(struct task_struct *task, struct pt_regs *regs,
+ unsigned int i, unsigned int n, unsigned long *args)
+{
+   BUG_ON(i + n > 6);
+   memcpy(args, ®s->d1 + i, n * sizeof(args[0]));
+}
+
+static inline long
+syscall_get_error(struct task_struct *task, struct pt_regs *regs)
+{
+   return IS_ERR_VALUE(regs->d0) ? regs->d0 : 0;
+}
+
+static inline long
+syscall_get_return_value(struct task_struct *task, struct pt_regs *regs)
+{
+   return regs->d0;
+}
+
+static inline int
+syscall_get_arch(void)
+{
+   return AUDIT_ARCH_M68K;
+}
+
+#endif /* _ASM_M68K_SYSCALL_H */
-- 
ldv


[PATCH v5 11/25] nds32: define syscall_get_arch()

2018-12-09 Thread Dmitry V. Levin
syscall_get_arch() is required to be implemented on all architectures
in addition to already implemented syscall_get_nr(),
syscall_get_arguments(), syscall_get_error(), and
syscall_get_return_value() functions in order to extend the generic
ptrace API with PTRACE_GET_SYSCALL_INFO request.

Cc: Greentime Hu 
Cc: Vincent Chen 
Cc: Paul Moore 
Cc: Eric Paris 
Cc: Oleg Nesterov 
Cc: Andy Lutomirski 
Cc: Elvira Khabirova 
Cc: Eugene Syromyatnikov 
Cc: linux-au...@redhat.com
Signed-off-by: Dmitry V. Levin 
---

Notes:
v5: added Cc
v2: apparently, this architecture can be configured as big-endian,
so changed AUDIT_ARCH_NDS32 to be little-endian, and added
AUDIT_ARCH_NDS32BE.

 arch/nds32/include/asm/syscall.h | 8 
 include/uapi/linux/audit.h   | 2 ++
 2 files changed, 10 insertions(+)

diff --git a/arch/nds32/include/asm/syscall.h b/arch/nds32/include/asm/syscall.h
index f7e5e86765fe..569149ca25da 100644
--- a/arch/nds32/include/asm/syscall.h
+++ b/arch/nds32/include/asm/syscall.h
@@ -5,6 +5,7 @@
 #ifndef _ASM_NDS32_SYSCALL_H
 #define _ASM_NDS32_SYSCALL_H   1
 
+#include 
 #include 
 struct task_struct;
 struct pt_regs;
@@ -185,4 +186,11 @@ void syscall_set_arguments(struct task_struct *task, 
struct pt_regs *regs,
 
memcpy(®s->uregs[0] + i, args, n * sizeof(args[0]));
 }
+
+static inline int syscall_get_arch(void)
+{
+   return IS_ENABLED(CONFIG_CPU_BIG_ENDIAN)
+   ? AUDIT_ARCH_NDS32BE : AUDIT_ARCH_NDS32;
+}
+
 #endif /* _ASM_NDS32_SYSCALL_H */
diff --git a/include/uapi/linux/audit.h b/include/uapi/linux/audit.h
index b8e848736031..54551adb3d5d 100644
--- a/include/uapi/linux/audit.h
+++ b/include/uapi/linux/audit.h
@@ -401,6 +401,8 @@ enum {
 #define AUDIT_ARCH_MIPSEL64(EM_MIPS|__AUDIT_ARCH_64BIT|__AUDIT_ARCH_LE)
 #define AUDIT_ARCH_MIPSEL64N32 (EM_MIPS|__AUDIT_ARCH_64BIT|__AUDIT_ARCH_LE|\
 __AUDIT_ARCH_CONVENTION_MIPS64_N32)
+#define AUDIT_ARCH_NDS32   (EM_NDS32|__AUDIT_ARCH_LE)
+#define AUDIT_ARCH_NDS32BE (EM_NDS32)
 #define AUDIT_ARCH_OPENRISC(EM_OPENRISC)
 #define AUDIT_ARCH_PARISC  (EM_PARISC)
 #define AUDIT_ARCH_PARISC64(EM_PARISC|__AUDIT_ARCH_64BIT)
-- 
ldv


[PATCH v5 12/25] nios2: define syscall_get_arch()

2018-12-09 Thread Dmitry V. Levin
syscall_get_arch() is required to be implemented on all architectures
in addition to already implemented syscall_get_nr(),
syscall_get_arguments(), syscall_get_error(), and
syscall_get_return_value() functions in order to extend the generic
ptrace API with PTRACE_GET_SYSCALL_INFO request.

Cc: Ley Foon Tan 
Cc: Paul Moore 
Cc: Eric Paris 
Cc: Oleg Nesterov 
Cc: Andy Lutomirski 
Cc: Elvira Khabirova 
Cc: Eugene Syromyatnikov 
Cc: nios2-...@lists.rocketboards.org
Cc: linux-au...@redhat.com
Signed-off-by: Dmitry V. Levin 
---

Notes:
v5: added Cc

 arch/nios2/include/asm/syscall.h | 6 ++
 include/uapi/linux/audit.h   | 1 +
 2 files changed, 7 insertions(+)

diff --git a/arch/nios2/include/asm/syscall.h b/arch/nios2/include/asm/syscall.h
index 9de220854c4a..cf35e210fc4d 100644
--- a/arch/nios2/include/asm/syscall.h
+++ b/arch/nios2/include/asm/syscall.h
@@ -17,6 +17,7 @@
 #ifndef __ASM_NIOS2_SYSCALL_H__
 #define __ASM_NIOS2_SYSCALL_H__
 
+#include 
 #include 
 #include 
 
@@ -135,4 +136,9 @@ static inline void syscall_set_arguments(struct task_struct 
*task,
}
 }
 
+static inline int syscall_get_arch(void)
+{
+   return AUDIT_ARCH_NIOS2;
+}
+
 #endif
diff --git a/include/uapi/linux/audit.h b/include/uapi/linux/audit.h
index 54551adb3d5d..883c5f56be9c 100644
--- a/include/uapi/linux/audit.h
+++ b/include/uapi/linux/audit.h
@@ -403,6 +403,7 @@ enum {
 __AUDIT_ARCH_CONVENTION_MIPS64_N32)
 #define AUDIT_ARCH_NDS32   (EM_NDS32|__AUDIT_ARCH_LE)
 #define AUDIT_ARCH_NDS32BE (EM_NDS32)
+#define AUDIT_ARCH_NIOS2   (EM_ALTERA_NIOS2|__AUDIT_ARCH_LE)
 #define AUDIT_ARCH_OPENRISC(EM_OPENRISC)
 #define AUDIT_ARCH_PARISC  (EM_PARISC)
 #define AUDIT_ARCH_PARISC64(EM_PARISC|__AUDIT_ARCH_64BIT)
-- 
ldv


[PATCH v5 10/25] Move EM_NDS32 to uapi/linux/elf-em.h

2018-12-09 Thread Dmitry V. Levin
This should never have been defined in the arch tree to begin with,
and now uapi/linux/audit.h header is going to use EM_NDS32
in order to define AUDIT_ARCH_NDS32 which is needed to implement
syscall_get_arch() which in turn is required to extend
the generic ptrace API with PTRACE_GET_SYSCALL_INFO request.

Cc: Greentime Hu 
Cc: Vincent Chen 
Cc: Oleg Nesterov 
Cc: Andy Lutomirski 
Cc: Elvira Khabirova 
Cc: Eugene Syromyatnikov 
Signed-off-by: Dmitry V. Levin 
---

Notes:
v5: added Cc

 arch/nds32/include/asm/elf.h | 3 +--
 include/uapi/linux/elf-em.h  | 2 ++
 2 files changed, 3 insertions(+), 2 deletions(-)

diff --git a/arch/nds32/include/asm/elf.h b/arch/nds32/include/asm/elf.h
index f5f9cf7e0544..71f9d51481a2 100644
--- a/arch/nds32/include/asm/elf.h
+++ b/arch/nds32/include/asm/elf.h
@@ -9,14 +9,13 @@
  */
 
 #include 
+#include 
 
 typedef unsigned long elf_greg_t;
 typedef unsigned long elf_freg_t[3];
 
 extern unsigned int elf_hwcap;
 
-#define EM_NDS32   167
-
 #define R_NDS32_NONE   0
 #define R_NDS32_16_RELA19
 #define R_NDS32_32_RELA20
diff --git a/include/uapi/linux/elf-em.h b/include/uapi/linux/elf-em.h
index e0fb2794bbad..d8695ad90f6b 100644
--- a/include/uapi/linux/elf-em.h
+++ b/include/uapi/linux/elf-em.h
@@ -39,6 +39,8 @@
 #define EM_ALTERA_NIOS2113 /* Altera Nios II soft-core processor */
 #define EM_TI_C6000140 /* TI C6X DSPs */
 #define EM_HEXAGON 164 /* QUALCOMM Hexagon */
+#define EM_NDS32   167 /* Andes Technology compact code size
+  embedded RISC processor family */
 #define EM_AARCH64 183 /* ARM 64 bit */
 #define EM_TILEPRO 188 /* Tilera TILEPro */
 #define EM_MICROBLAZE  189 /* Xilinx MicroBlaze */
-- 
ldv


[PATCH v5 19/25] xtensa: define syscall_get_* functions

2018-12-09 Thread Dmitry V. Levin
syscall_get_* functions are required to be implemented on all
architectures in order to extend the generic ptrace API with
PTRACE_GET_SYSCALL_INFO request.

This adds all 5 syscall_get_* functions on xtensa as documented
in asm-generic/syscall.h: syscall_get_nr, syscall_get_arguments,
syscall_get_error, syscall_get_return_value, and syscall_get_arch.

Cc: Max Filippov 
Cc: Oleg Nesterov 
Cc: Andy Lutomirski 
Cc: Elvira Khabirova 
Cc: Eugene Syromyatnikov 
Cc: Chris Zankel 
Cc: Paul Moore 
Cc: Eric Paris 
Cc: linux-xte...@linux-xtensa.org
Cc: linux-au...@redhat.com
Signed-off-by: Dmitry V. Levin 
---

Notes:
v5: added syscall_get_nr, syscall_get_arguments, syscall_get_error,
and syscall_get_return_value
v2: added Acked-by
v1: added syscall_get_arch

 arch/xtensa/include/asm/syscall.h | 69 +++
 include/uapi/linux/audit.h|  1 +
 2 files changed, 70 insertions(+)

diff --git a/arch/xtensa/include/asm/syscall.h 
b/arch/xtensa/include/asm/syscall.h
index 3673ff1f1bc5..d529c855a144 100644
--- a/arch/xtensa/include/asm/syscall.h
+++ b/arch/xtensa/include/asm/syscall.h
@@ -8,6 +8,75 @@
  * Copyright (C) 2001 - 2007 Tensilica Inc.
  */
 
+#include 
+
+static inline int
+syscall_get_nr(struct task_struct *task, struct pt_regs *regs)
+{
+   return regs->syscall;
+}
+
+static inline void
+syscall_get_arguments(struct task_struct *task, struct pt_regs *regs,
+ unsigned int i, unsigned int n, unsigned long *args)
+{
+   switch (i) {
+   case 0:
+   if (!n--)
+   break;
+   *args++ = regs->areg[6];
+   /* fall through */
+   case 1:
+   if (!n--)
+   break;
+   *args++ = regs->areg[3];
+   /* fall through */
+   case 2:
+   if (!n--)
+   break;
+   *args++ = regs->areg[4];
+   /* fall through */
+   case 3:
+   if (!n--)
+   break;
+   *args++ = regs->areg[5];
+   /* fall through */
+   case 4:
+   if (!n--)
+   break;
+   *args++ = regs->areg[8];
+   /* fall through */
+   case 5:
+   if (!n--)
+   break;
+   *args++ = regs->areg[9];
+   /* fall through */
+   case 6:
+   if (!n--)
+   break;
+   /* fall through */
+   default:
+   BUG();
+   }
+}
+
+static inline long
+syscall_get_error(struct task_struct *task, struct pt_regs *regs)
+{
+   return IS_ERR_VALUE(regs->areg[2]) ? regs->areg[2] : 0;
+
+static inline long
+syscall_get_return_value(struct task_struct *task, struct pt_regs *regs)
+{
+   return regs->areg[2];
+}
+
+static inline int
+syscall_get_arch(void)
+{
+   return AUDIT_ARCH_XTENSA;
+}
+
 struct pt_regs;
 asmlinkage long xtensa_ptrace(long, long, long, long);
 asmlinkage long xtensa_sigreturn(struct pt_regs*);
diff --git a/include/uapi/linux/audit.h b/include/uapi/linux/audit.h
index 1e9808f3a240..bcc0619b046f 100644
--- a/include/uapi/linux/audit.h
+++ b/include/uapi/linux/audit.h
@@ -425,6 +425,7 @@ enum {
 #define AUDIT_ARCH_TILEGX32(EM_TILEGX|__AUDIT_ARCH_LE)
 #define AUDIT_ARCH_TILEPRO (EM_TILEPRO|__AUDIT_ARCH_LE)
 #define AUDIT_ARCH_X86_64  (EM_X86_64|__AUDIT_ARCH_64BIT|__AUDIT_ARCH_LE)
+#define AUDIT_ARCH_XTENSA  (EM_XTENSA)
 
 #define AUDIT_PERM_EXEC1
 #define AUDIT_PERM_WRITE   2
-- 
ldv


[PATCH v5 23/25] powerpc/ptrace: replace ptrace_report_syscall() with a tracehook call

2018-12-09 Thread Dmitry V. Levin
From: Elvira Khabirova 

Arch code should use tracehook_*() helpers, as documented
in include/linux/tracehook.h,
ptrace_report_syscall() is not expected to be used outside that file.

The patch does not look very nice, but at least it is correct
and opens the way for PTRACE_GET_SYSCALL_INFO API.

Co-authored-by: Dmitry V. Levin 
Fixes: 5521eb4bca2d ("powerpc/ptrace: Add support for PTRACE_SYSEMU")
Cc: Michael Ellerman 
Cc: Oleg Nesterov 
Cc: Eugene Syromyatnikov 
Cc: Benjamin Herrenschmidt 
Cc: Paul Mackerras 
Cc: Breno Leitao 
Cc: Andy Lutomirski 
Cc: linuxppc-...@lists.ozlabs.org
Signed-off-by: Elvira Khabirova 
Signed-off-by: Dmitry V. Levin 
---

Notes:
v5: reverted to a simple approach, compile- and run-tested
v4: rewritten to call tracehook_report_syscall_entry() once, compile-tested
v3: add a descriptive comment
v2: explicitly ignore tracehook_report_syscall_entry() return code

 arch/powerpc/kernel/ptrace.c | 7 ++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/arch/powerpc/kernel/ptrace.c b/arch/powerpc/kernel/ptrace.c
index afb819f4ca68..714c3480c52d 100644
--- a/arch/powerpc/kernel/ptrace.c
+++ b/arch/powerpc/kernel/ptrace.c
@@ -3266,12 +3266,17 @@ long do_syscall_trace_enter(struct pt_regs *regs)
user_exit();
 
if (test_thread_flag(TIF_SYSCALL_EMU)) {
-   ptrace_report_syscall(regs);
/*
+* A nonzero return code from tracehook_report_syscall_entry()
+* tells us to prevent the syscall execution, but we are not
+* going to execute it anyway.
+*
 * Returning -1 will skip the syscall execution. We want to
 * avoid clobbering any register also, thus, not 'gotoing'
 * skip label.
 */
+   if (tracehook_report_syscall_entry(regs))
+   ;
return -1;
}
 
-- 
ldv


[PATCH v5 20/25] Move EM_UNICORE to uapi/linux/elf-em.h

2018-12-09 Thread Dmitry V. Levin
This should never have been defined in the arch tree to begin with,
and now uapi/linux/audit.h header is going to use EM_UNICORE
in order to define AUDIT_ARCH_UNICORE which is needed to implement
syscall_get_arch() which in turn is required to extend
the generic ptrace API with PTRACE_GET_SYSCALL_INFO request.

Cc: Guan Xuetao 
Cc: Oleg Nesterov 
Cc: Andy Lutomirski 
Cc: Elvira Khabirova 
Cc: Eugene Syromyatnikov 
Signed-off-by: Dmitry V. Levin 
---

Notes:
v5: added Cc

 arch/unicore32/include/asm/elf.h | 3 +--
 include/uapi/linux/elf-em.h  | 1 +
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/arch/unicore32/include/asm/elf.h b/arch/unicore32/include/asm/elf.h
index 829042d07722..ae66dc1be49e 100644
--- a/arch/unicore32/include/asm/elf.h
+++ b/arch/unicore32/include/asm/elf.h
@@ -19,6 +19,7 @@
  * ELF register definitions..
  */
 #include 
+#include 
 
 typedef unsigned long elf_greg_t;
 typedef unsigned long elf_freg_t[3];
@@ -28,8 +29,6 @@ typedef elf_greg_t elf_gregset_t[ELF_NGREG];
 
 typedef struct fp_state elf_fpregset_t;
 
-#define EM_UNICORE 110
-
 #define R_UNICORE_NONE 0
 #define R_UNICORE_PC24 1
 #define R_UNICORE_ABS322
diff --git a/include/uapi/linux/elf-em.h b/include/uapi/linux/elf-em.h
index f392dd9d07f7..2533bb1db3ef 100644
--- a/include/uapi/linux/elf-em.h
+++ b/include/uapi/linux/elf-em.h
@@ -37,6 +37,7 @@
 #define EM_ARCOMPACT   93  /* ARCompact processor */
 #define EM_XTENSA  94  /* Tensilica Xtensa Architecture */
 #define EM_BLACKFIN 106 /* ADI Blackfin Processor */
+#define EM_UNICORE 110 /* UniCore-32 */
 #define EM_ALTERA_NIOS2113 /* Altera Nios II soft-core processor */
 #define EM_TI_C6000140 /* TI C6X DSPs */
 #define EM_HEXAGON 164 /* QUALCOMM Hexagon */
-- 
ldv


[PATCH v5 21/25] unicore32: add asm/syscall.h

2018-12-09 Thread Dmitry V. Levin
syscall_get_* functions are required to be implemented on all
architectures in order to extend the generic ptrace API with
PTRACE_GET_SYSCALL_INFO request.

This introduces asm/syscall.h on unicore32 implementing all 5
syscall_get_* functions as documented in asm-generic/syscall.h:
syscall_get_nr, syscall_get_arguments, syscall_get_error,
syscall_get_return_value, and syscall_get_arch.

A note for the unicore32 architecture maintainer: I have no idea about
the syscall semantics on this architecture, and the code is of little
help here.  All I could infer from the code is that it looks very
similar to ARM, so the implementation of syscall_get_* functions
is also similar to ARM.

Cc: Guan Xuetao 
Cc: Paul Moore 
Cc: Eric Paris 
Cc: Oleg Nesterov 
Cc: Andy Lutomirski 
Cc: Elvira Khabirova 
Cc: Eugene Syromyatnikov 
Cc: linux-au...@redhat.com
Signed-off-by: Dmitry V. Levin 
---

Notes:
v5: added syscall_get_nr, syscall_get_arguments, syscall_get_error,
and syscall_get_return_value
v1: added syscall_get_arch

 arch/unicore32/include/asm/syscall.h | 45 
 include/uapi/linux/audit.h   |  1 +
 2 files changed, 46 insertions(+)
 create mode 100644 arch/unicore32/include/asm/syscall.h

diff --git a/arch/unicore32/include/asm/syscall.h 
b/arch/unicore32/include/asm/syscall.h
new file mode 100644
index ..e30d08acf359
--- /dev/null
+++ b/arch/unicore32/include/asm/syscall.h
@@ -0,0 +1,45 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _ASM_UNICORE_SYSCALL_H
+#define _ASM_UNICORE_SYSCALL_H
+
+#include 
+
+static inline int
+syscall_get_nr(struct task_struct *task, struct pt_regs *regs)
+{
+   return task_thread_info(task)->syscall;
+}
+
+static inline void
+syscall_get_arguments(struct task_struct *task, struct pt_regs *regs,
+ unsigned int i, unsigned int n, unsigned long *args)
+{
+   BUG_ON(i + n > 6);
+   if (i == 0) {
+   args[0] = regs->UCreg_ORIG_00;
+   args++;
+   i++;
+   n--;
+   }
+   memcpy(args, ®s->UCreg_00 + i, n * sizeof(args[0]));
+}
+
+static inline long
+syscall_get_error(struct task_struct *task, struct pt_regs *regs)
+{
+   return IS_ERR_VALUE(regs->UCreg_00) ? regs->UCreg_00 : 0;
+}
+
+static inline long
+syscall_get_return_value(struct task_struct *task, struct pt_regs *regs)
+{
+   return regs->UCreg_00;
+}
+
+static inline int
+syscall_get_arch(void)
+{
+   return AUDIT_ARCH_UNICORE;
+}
+
+#endif /* _ASM_UNICORE_SYSCALL_H */
diff --git a/include/uapi/linux/audit.h b/include/uapi/linux/audit.h
index bcc0619b046f..3901c51c0b93 100644
--- a/include/uapi/linux/audit.h
+++ b/include/uapi/linux/audit.h
@@ -424,6 +424,7 @@ enum {
 #define AUDIT_ARCH_TILEGX  (EM_TILEGX|__AUDIT_ARCH_64BIT|__AUDIT_ARCH_LE)
 #define AUDIT_ARCH_TILEGX32(EM_TILEGX|__AUDIT_ARCH_LE)
 #define AUDIT_ARCH_TILEPRO (EM_TILEPRO|__AUDIT_ARCH_LE)
+#define AUDIT_ARCH_UNICORE (EM_UNICORE|__AUDIT_ARCH_LE)
 #define AUDIT_ARCH_X86_64  (EM_X86_64|__AUDIT_ARCH_64BIT|__AUDIT_ARCH_LE)
 #define AUDIT_ARCH_XTENSA  (EM_XTENSA)
 
-- 
ldv


[PATCH v5 24/25] ptrace: add PTRACE_GET_SYSCALL_INFO request

2018-12-09 Thread Dmitry V. Levin
From: Elvira Khabirova 

PTRACE_GET_SYSCALL_INFO is a generic ptrace API that lets ptracer obtain
details of the syscall the tracee is blocked in.

There are two reasons for a special syscall-related ptrace request.

Firstly, with the current ptrace API there are cases when ptracer cannot
retrieve necessary information about syscalls.  Some examples include:
* The notorious int-0x80-from-64-bit-task issue.  See [1] for details.
In short, if a 64-bit task performs a syscall through int 0x80, its tracer
has no reliable means to find out that the syscall was, in fact,
a compat syscall, and misidentifies it.
* Syscall-enter-stop and syscall-exit-stop look the same for the tracer.
Common practice is to keep track of the sequence of ptrace-stops in order
not to mix the two syscall-stops up.  But it is not as simple as it looks;
for example, strace had a (just recently fixed) long-standing bug where
attaching strace to a tracee that is performing the execve system call
led to the tracer identifying the following syscall-exit-stop as
syscall-enter-stop, which messed up all the state tracking.
* Since the introduction of commit 84d77d3f06e7e8dea057d10e8ec77ad71f721be3
("ptrace: Don't allow accessing an undumpable mm"), both PTRACE_PEEKDATA
and process_vm_readv become unavailable when the process dumpable flag
is cleared.  On such architectures as ia64 this results in all syscall
arguments being unavailable for the tracer.

Secondly, ptracers also have to support a lot of arch-specific code for
obtaining information about the tracee.  For some architectures, this
requires a ptrace(PTRACE_PEEKUSER, ...) invocation for every syscall
argument and return value.

ptrace(2) man page:

long ptrace(enum __ptrace_request request, pid_t pid,
void *addr, void *data);
...
PTRACE_GET_SYSCALL_INFO
   Retrieve information about the syscall that caused the stop.
   The information is placed into the buffer pointed by "data"
   argument, which should be a pointer to a buffer of type
   "struct ptrace_syscall_info".
   The "addr" argument contains the size of the buffer pointed to
   by "data" argument (i.e., sizeof(struct ptrace_syscall_info)).
   The return value contains the number of bytes available
   to be written by the kernel.
   If the size of data to be written by the kernel exceeds the size
   specified by "addr" argument, the output is truncated.

Co-authored-by: Dmitry V. Levin 
Cc: Oleg Nesterov 
Cc: Andy Lutomirski 
Cc: Eugene Syromyatnikov 
Cc: Kees Cook 
Cc: Jann Horn 
Cc: linux-...@vger.kernel.org
Cc: strace-de...@lists.strace.io
Signed-off-by: Elvira Khabirova 
Signed-off-by: Dmitry V. Levin 
---

Notes:
v5:
* Change PTRACE_EVENTMSG_SYSCALL_{ENTRY,EXIT} values as requested by Oleg.
* Change struct ptrace_syscall_info: generalize instruction_pointer,
  stack_pointer, and frame_pointer fields by moving them from
  ptrace_syscall_info.{entry,seccomp} substructures to ptrace_syscall_info
  and initializing them for all stops.
* Add PTRACE_SYSCALL_INFO_NONE, set it when not in a syscall stop,
  so e.g. "strace -i" could use PTRACE_SYSCALL_INFO_SECCOMP to obtain
  instruction_pointer when the tracee is in a signal stop.
* Make available for all architectures: do not conditionalize on
  CONFIG_HAVE_ARCH_TRACEHOOK since all syscall_get_* functions
  are implemented on all architectures.

v4:
* Do not introduce task_struct.ptrace_event,
  use child->last_siginfo->si_code instead.
* Implement PTRACE_SYSCALL_INFO_SECCOMP and ptrace_syscall_info.seccomp
  support along with PTRACE_SYSCALL_INFO_{ENTRY,EXIT} and
  ptrace_syscall_info.{entry,exit}.

v3:
* Change struct ptrace_syscall_info.
* Support PTRACE_EVENT_SECCOMP by adding ptrace_event to task_struct.
* Add proper defines for ptrace_syscall_info.op values.
* Rename PT_SYSCALL_IS_ENTERING and PT_SYSCALL_IS_EXITING to
  PTRACE_EVENTMSG_SYSCALL_ENTRY and PTRACE_EVENTMSG_SYSCALL_EXIT
* and move them to uapi.

v2:
* Do not use task->ptrace.
* Replace entry_info.is_compat with entry_info.arch, use syscall_get_arch().
* Use addr argument of sys_ptrace to get expected size of the struct;
  return full size of the struct.

 include/linux/tracehook.h   |  9 ++--
 include/uapi/linux/ptrace.h | 39 +++
 kernel/ptrace.c | 99 -
 3 files changed, 143 insertions(+), 4 deletions(-)

diff --git a/include/linux/tracehook.h b/include/linux/tracehook.h
index df20f8bdbfa3..6bc7a3d58e2f 100644
--- a/include/linux/tracehook.h
+++ b/include/linux/tracehook.h
@@ -57,13 +57,15 @@ struct linux_binprm;
 /*
  * ptrace report for syscall entry and exit looks identical.
  */
-static inline int ptrace_report_syscall(struct pt_regs *regs)
+static inline int ptrace_report_syscall

[PATCH v5 25/25] selftests/ptrace: add a test case for PTRACE_GET_SYSCALL_INFO

2018-12-09 Thread Dmitry V. Levin
Check whether PTRACE_GET_SYSCALL_INFO semantics implemented in the kernel
matches userspace expectations.

Cc: Oleg Nesterov 
Cc: Andy Lutomirski 
Cc: Shuah Khan 
Cc: Elvira Khabirova 
Cc: Eugene Syromyatnikov 
Cc: linux-kselft...@vger.kernel.org
Signed-off-by: Dmitry V. Levin 
---
 tools/testing/selftests/ptrace/.gitignore |   1 +
 tools/testing/selftests/ptrace/Makefile   |   2 +-
 .../selftests/ptrace/get_syscall_info.c   | 272 ++
 3 files changed, 274 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/ptrace/get_syscall_info.c

diff --git a/tools/testing/selftests/ptrace/.gitignore 
b/tools/testing/selftests/ptrace/.gitignore
index b3e59d41fd82..cfcc49a7def7 100644
--- a/tools/testing/selftests/ptrace/.gitignore
+++ b/tools/testing/selftests/ptrace/.gitignore
@@ -1 +1,2 @@
+get_syscall_info
 peeksiginfo
diff --git a/tools/testing/selftests/ptrace/Makefile 
b/tools/testing/selftests/ptrace/Makefile
index 8a2bc5562179..4bc550b6b845 100644
--- a/tools/testing/selftests/ptrace/Makefile
+++ b/tools/testing/selftests/ptrace/Makefile
@@ -1,5 +1,5 @@
 CFLAGS += -iquote../../../../include/uapi -Wall
 
-TEST_GEN_PROGS := peeksiginfo
+TEST_GEN_PROGS := get_syscall_info peeksiginfo
 
 include ../lib.mk
diff --git a/tools/testing/selftests/ptrace/get_syscall_info.c 
b/tools/testing/selftests/ptrace/get_syscall_info.c
new file mode 100644
index ..21d1180d117e
--- /dev/null
+++ b/tools/testing/selftests/ptrace/get_syscall_info.c
@@ -0,0 +1,272 @@
+/*
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ *
+ * Copyright (c) 2018 Dmitry V. Levin 
+ * All rights reserved.
+ *
+ * Check whether PTRACE_GET_SYSCALL_INFO semantics implemented in the kernel
+ * matches userspace expectations.
+ */
+
+#include "../kselftest_harness.h"
+#include 
+#include 
+#include 
+#include "linux/ptrace.h"
+
+static int
+kill_tracee(pid_t pid)
+{
+   if (!pid)
+   return 0;
+
+   int saved_errno = errno;
+
+   int rc = kill(pid, SIGKILL);
+
+   errno = saved_errno;
+   return rc;
+}
+
+static long
+sys_ptrace(int request, pid_t pid, unsigned long addr, unsigned long data)
+{
+   return syscall(__NR_ptrace, request, pid, addr, data);
+}
+
+#define LOG_KILL_TRACEE(fmt, ...)  \
+   do {\
+   kill_tracee(pid);   \
+   TH_LOG("wait #%d: " fmt,\
+  ptrace_stop, ##__VA_ARGS__); \
+   } while (0)
+
+TEST(get_syscall_info)
+{
+   static const unsigned long args[][7] = {
+   /* a sequence of architecture-agnostic syscalls */
+   {
+   __NR_chdir,
+   (unsigned long) "",
+   0xbad1fed1,
+   0xbad2fed2,
+   0xbad3fed3,
+   0xbad4fed4,
+   0xbad5fed5
+   },
+   {
+   __NR_gettid,
+   0xcaf0bea0,
+   0xcaf1bea1,
+   0xcaf2bea2,
+   0xcaf3bea3,
+   0xcaf4bea4,
+   0xcaf5bea5
+   },
+   {
+   __NR_exit_group,
+   0,
+   0xfac1c0d1,
+   0xfac2c0d2,
+   0xfac3c0d3,
+   0xfac4c0d4,
+   0xfac5c0d5
+   }
+   };
+   const unsigned long *exp_args;
+
+   pid_t pid = fork();
+
+   ASSERT_LE(0, pid) {
+   TH_LOG("fork: %m");
+   }
+
+   if (pid == 0) {
+   /* get the pid before PTRACE_TRACEME */
+   pid = getpid();
+   ASSERT_EQ(0, sys_ptrace(PTRACE_TRACEME, 0, 0, 0)) {
+   TH_LOG("PTRACE_TRACEME: %m");
+   }
+   ASSERT_EQ(0, kill(pid, SIGSTOP)) {
+   /* cannot happen */
+   TH_LOG("kill SIGSTOP: %m");
+   }
+   for (unsigned int i = 0; i < ARRAY_SIZE(args); ++i) {
+   syscall(args[i][0],
+   args[i][1], args[i][2], args[i][3],
+   args[i][4], args[i][5], args[i][6]);
+   }
+   /* unreachable */
+   _exit(1);
+   }
+
+   const struct {
+   unsigned int is_error;
+   int rval;
+   } *exp_param, exit_param[] = {
+   { 1, -ENOENT }, /* chdir */
+   { 0, pid }  /* gettid */
+   };
+
+   unsigned int ptrace_stop;
+
+   for (ptrace_stop = 0; ; ++ptrace_stop) {
+   struct ptrace_syscall_info info = {
+ 

[PATCH v5 22/25] syscall_get_arch: add "struct task_struct *" argument

2018-12-09 Thread Dmitry V. Levin
This argument is required to extend the generic ptrace API with
PTRACE_GET_SYSCALL_INFO request: syscall_get_arch() is going
to be called from ptrace_request() along with syscall_get_nr(),
syscall_get_arguments(), syscall_get_error(), and
syscall_get_return_value() functions with a tracee as their argument.

Reverts: 5e937a9ae913 ("syscall_get_arch: remove useless function arguments")
Reverts: 1002d94d3076 ("syscall.h: fix doc text for syscall_get_arch()")
Reviewed-by: Andy Lutomirski  # for x86
Reviewed-by: Palmer Dabbelt 
Acked-by: Paul Burton  # MIPS parts
Acked-by: Michael Ellerman  (powerpc)
Cc: Eric Paris 
Cc: Paul Moore 
Cc: Richard Henderson 
Cc: Ivan Kokshaysky 
Cc: Matt Turner 
Cc: Vineet Gupta 
Cc: Russell King 
Cc: Catalin Marinas 
Cc: Will Deacon 
Cc: Mark Salter 
Cc: Aurelien Jacquiot 
Cc: Yoshinori Sato 
Cc: Richard Kuo 
Cc: Tony Luck 
Cc: Fenghua Yu 
Cc: Geert Uytterhoeven 
Cc: Michal Simek 
Cc: Greentime Hu 
Cc: Vincent Chen 
Cc: Ley Foon Tan 
Cc: Jonas Bonn 
Cc: Stefan Kristiansson 
Cc: Stafford Horne 
Cc: James E.J. Bottomley 
Cc: Helge Deller 
Cc: Albert Ou 
Cc: Martin Schwidefsky 
Cc: Heiko Carstens 
Cc: Rich Felker 
Cc: David S. Miller 
Cc: Guan Xuetao 
Cc: Jeff Dike 
Cc: Richard Weinberger 
Cc: Chris Zankel 
Cc: Max Filippov 
Cc: Arnd Bergmann 
Cc: Kees Cook 
Cc: Will Drewry 
Cc: Oleg Nesterov 
Cc: Elvira Khabirova 
Cc: Eugene Syromyatnikov 
Cc: Ralf Baechle 
Cc: James Hogan 
Cc: Benjamin Herrenschmidt 
Cc: Paul Mackerras 
Cc: Thomas Gleixner 
Cc: Ingo Molnar 
Cc: Borislav Petkov 
Cc: H. Peter Anvin 
Cc: x...@kernel.org
Cc: linux-al...@vger.kernel.org
Cc: linux-snps-...@lists.infradead.org
Cc: linux-arm-ker...@lists.infradead.org
Cc: linux-c6x-...@linux-c6x.org
Cc: uclinux-h8-de...@lists.sourceforge.jp
Cc: linux-hexa...@vger.kernel.org
Cc: linux-i...@vger.kernel.org
Cc: linux-m...@lists.linux-m68k.org
Cc: linux-m...@vger.kernel.org
Cc: nios2-...@lists.rocketboards.org
Cc: openr...@lists.librecores.org
Cc: linux-par...@vger.kernel.org
Cc: linuxppc-...@lists.ozlabs.org
Cc: linux-ri...@lists.infradead.org
Cc: linux-s...@vger.kernel.org
Cc: linux...@vger.kernel.org
Cc: sparcli...@vger.kernel.org
Cc: linux...@lists.infradead.org
Cc: linux-xte...@linux-xtensa.org
Cc: linux-a...@vger.kernel.org
Cc: linux-au...@redhat.com
Signed-off-by: Dmitry V. Levin 
---

Notes:
v5: fixed asm-generic docs by reverting 1002d94d3076, added Cc
v2: cleaned up mips part, added Reviewed-by

 arch/alpha/include/asm/syscall.h  |  2 +-
 arch/arc/include/asm/syscall.h|  2 +-
 arch/arm/include/asm/syscall.h|  2 +-
 arch/arm64/include/asm/syscall.h  |  4 ++--
 arch/c6x/include/asm/syscall.h|  2 +-
 arch/csky/include/asm/syscall.h   |  2 +-
 arch/h8300/include/asm/syscall.h  |  2 +-
 arch/hexagon/include/asm/syscall.h|  2 +-
 arch/ia64/include/asm/syscall.h   |  2 +-
 arch/m68k/include/asm/syscall.h   |  2 +-
 arch/microblaze/include/asm/syscall.h |  2 +-
 arch/mips/include/asm/syscall.h   |  6 +++---
 arch/mips/kernel/ptrace.c |  2 +-
 arch/nds32/include/asm/syscall.h  |  2 +-
 arch/nios2/include/asm/syscall.h  |  2 +-
 arch/openrisc/include/asm/syscall.h   |  2 +-
 arch/parisc/include/asm/syscall.h |  4 ++--
 arch/powerpc/include/asm/syscall.h| 10 --
 arch/riscv/include/asm/syscall.h  |  2 +-
 arch/s390/include/asm/syscall.h   |  4 ++--
 arch/sh/include/asm/syscall_32.h  |  2 +-
 arch/sh/include/asm/syscall_64.h  |  2 +-
 arch/sparc/include/asm/syscall.h  |  5 +++--
 arch/unicore32/include/asm/syscall.h  |  2 +-
 arch/x86/include/asm/syscall.h|  8 +---
 arch/x86/um/asm/syscall.h |  2 +-
 arch/xtensa/include/asm/syscall.h |  2 +-
 include/asm-generic/syscall.h |  5 +++--
 kernel/auditsc.c  |  4 ++--
 kernel/seccomp.c  |  4 ++--
 30 files changed, 52 insertions(+), 42 deletions(-)

diff --git a/arch/alpha/include/asm/syscall.h b/arch/alpha/include/asm/syscall.h
index 437758bdc49f..288779aa9847 100644
--- a/arch/alpha/include/asm/syscall.h
+++ b/arch/alpha/include/asm/syscall.h
@@ -31,7 +31,7 @@ syscall_get_return_value(struct task_struct *task, struct 
pt_regs *regs)
 }
 
 static inline int
-syscall_get_arch(void)
+syscall_get_arch(struct task_struct *task)
 {
return AUDIT_ARCH_ALPHA;
 }
diff --git a/arch/arc/include/asm/syscall.h b/arch/arc/include/asm/syscall.h
index c7fc4c0c3bcb..caf2697ef5b7 100644
--- a/arch/arc/include/asm/syscall.h
+++ b/arch/arc/include/asm/syscall.h
@@ -70,7 +70,7 @@ syscall_get_arguments(struct task_struct *task, struct 
pt_regs *regs,
 }
 
 static inline int
-syscall_get_arch(void)
+syscall_get_arch(struct task_struct *task)
 {
return IS_ENABLED(CONFIG_ISA_ARCOMPACT)
? (IS_ENABLED(CONFIG_CPU_BIG_ENDIAN)
diff --git a/arch/arm/include/asm/syscall.h b/arch/arm/include/asm/syscall.h
index 06dea6bce293..3940ceac0bdc 100644
--- a/arch/arm/include/asm/

Re: [PATCH v4 1/2] ptrace: save the type of syscall-stop in ptrace_message

2018-11-30 Thread Dmitry V. Levin
On Fri, Nov 30, 2018 at 12:29:21PM +0100, Oleg Nesterov wrote:
> On 11/30, Dmitry V. Levin wrote:
> > On Thu, Nov 29, 2018 at 03:47:43PM +0100, Oleg Nesterov wrote:
> >
> > > > so that PTRACE_GETEVENTMSG users can easily tell
> > > > whether this new semantics is supported by the kernel or not.
> > >
> > > Yes. And how much this can help? Again, an application can trivially 
> > > detect
> > > if this feature implemented or not, and it should do this anyway if it 
> > > wants
> > > to (try to) use PTRACE_EVENTMSG_SYSCALL_ENTRY/EXIT ?
> >
> > How an application can easily detect whether this feature is implemented?
> 
> As I already said, it can just do ptrace(PTRACE_GET_SYSCALL_INFO, NULL) ?
> If it returns -EIO then this feature is not implemented. Any other error
> code (actually EINVAL or EFAULT) means it is implemented.

Fair enough.
We can change PTRACE_EVENTMSG_SYSCALL_ENTRY/EXIT to 1/2 if you like,
and document this trick somewhere.


-- 
ldv


signature.asc
Description: PGP signature


Re: [PATCH v2 06/15] m68k: define syscall_get_arch()

2018-12-02 Thread Dmitry V. Levin
Hi Geert,

On Sun, Dec 02, 2018 at 11:29:10AM +0100, Geert Uytterhoeven wrote:
> Hi Dmitry,
> 
> On Tue, Nov 20, 2018 at 1:15 AM Dmitry V. Levin  wrote:
> > syscall_get_arch() is required to be implemented on all architectures
> > in order to extend the generic ptrace API with PTRACE_GET_SYSCALL_INFO
> > request.
> >
> > Signed-off-by: Dmitry V. Levin 
> 
> Reviewed-by: Geert Uytterhoeven 
> 
> What's your plan w.r.t. the upstreaming strategy?
> Do you plan to get this series in as a whole, or through individual 
> architecture
> maintainers?

Given that the last patch in this series adds an argument
to syscall_get_arch(), my plan is to get this series in
as a whole along with PTRACE_GET_SYSCALL_INFO series.


-- 
ldv


signature.asc
Description: PGP signature


[PATCH] microblaze: fix syscall_set_return_value()

2018-12-02 Thread Dmitry V. Levin
According to documentation in include/asm-generic/syscall.h,
if error argument of syscall_set_return_value() is nonzero,
it is a negated errno.

This change fixes syscall_set_return_value() implementation on
microblaze to match its own syscall_get_error(), the documentation,
and other architectures where error argument of
syscall_set_return_value() is non-positive.

Fixes: d5b37092aae1e ("microblaze: Implement include/asm/syscall.h.")
Cc: sta...@vger.kernel.org # v2.6.32+
Signed-off-by: Dmitry V. Levin 
---
 arch/microblaze/include/asm/syscall.h | 5 +
 1 file changed, 1 insertion(+), 4 deletions(-)

diff --git a/arch/microblaze/include/asm/syscall.h 
b/arch/microblaze/include/asm/syscall.h
index 220decd605a4..c2489a591d6b 100644
--- a/arch/microblaze/include/asm/syscall.h
+++ b/arch/microblaze/include/asm/syscall.h
@@ -36,10 +36,7 @@ static inline void syscall_set_return_value(struct 
task_struct *task,
struct pt_regs *regs,
int error, long val)
 {
-   if (error)
-   regs->r3 = -error;
-   else
-   regs->r3 = val;
+   regs->r3 = error ?: val;
 }
 
 static inline microblaze_reg_t microblaze_get_syscall_arg(struct pt_regs *regs,
-- 
ldv


[PATCH] ia64: fix syscall_get_error()

2018-12-02 Thread Dmitry V. Levin
According to documentation in include/asm-generic/syscall.h,
syscall_get_error() should return -ERRORCODE if the system call failed.

This change fixes syscall_get_error() implementation on ia64 to match
its own syscall_set_return_value(), the documentation, and other
architectures where syscall_get_error() returns a non-positive value.

Fixes: cfb361f13c81 ("[IA64] utrace syscall.h support for ia64")
Cc: sta...@vger.kernel.org # v2.6.27+
Signed-off-by: Dmitry V. Levin 
---
 arch/ia64/include/asm/syscall.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/ia64/include/asm/syscall.h b/arch/ia64/include/asm/syscall.h
index 1d0b875fec44..4ccf888c083d 100644
--- a/arch/ia64/include/asm/syscall.h
+++ b/arch/ia64/include/asm/syscall.h
@@ -35,7 +35,7 @@ static inline void syscall_rollback(struct task_struct *task,
 static inline long syscall_get_error(struct task_struct *task,
 struct pt_regs *regs)
 {
-   return regs->r10 == -1 ? regs->r8:0;
+   return regs->r10 == -1 ? -regs->r8 : 0;
 }
 
 static inline long syscall_get_return_value(struct task_struct *task,
-- 
ldv


[PATCH] nios2: fix syscall_get_error()

2018-12-02 Thread Dmitry V. Levin
According to documentation in include/asm-generic/syscall.h,
syscall_get_error() should return -ERRORCODE if the system call failed.

This change fixes syscall_get_error() implementation on nios2 to match
its own syscall_set_return_value(), the documentation, and other
architectures where syscall_get_error() returns a non-positive value.

Fixes: 1000197d80132 ("nios2: System calls handling")
Cc: sta...@vger.kernel.org # v3.19+
Signed-off-by: Dmitry V. Levin 
---
 arch/nios2/include/asm/syscall.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/nios2/include/asm/syscall.h b/arch/nios2/include/asm/syscall.h
index 9de220854c4a..abf54addb804 100644
--- a/arch/nios2/include/asm/syscall.h
+++ b/arch/nios2/include/asm/syscall.h
@@ -35,7 +35,7 @@ static inline void syscall_rollback(struct task_struct *task,
 static inline long syscall_get_error(struct task_struct *task,
struct pt_regs *regs)
 {
-   return regs->r7 ? regs->r2 : 0;
+   return regs->r7 ? -regs->r2 : 0;
 }
 
 static inline long syscall_get_return_value(struct task_struct *task,
-- 
ldv


[PATCH] sh: fix syscall_set_return_value()

2018-12-02 Thread Dmitry V. Levin
According to documentation in include/asm-generic/syscall.h,
if error argument of syscall_set_return_value() is nonzero,
it is a negated errno.

This change fixes syscall_set_return_value() implementation on sh
to match its own syscall_get_error(), the documentation, and other
architectures where error argument of syscall_set_return_value()
is non-positive.

Fixes: fb4f87a2f048b ("sh: Provide the asm/syscall.h interface, needed by 
tracehook.")
Fixes: 94e2fb3d3e1f4 ("sh: Provide asm/syscall.h for SH-5.")
Cc: sta...@vger.kernel.org # v2.6.28+
Signed-off-by: Dmitry V. Levin 
---
 arch/sh/include/asm/syscall_32.h | 5 +
 arch/sh/include/asm/syscall_64.h | 5 +
 2 files changed, 2 insertions(+), 8 deletions(-)

diff --git a/arch/sh/include/asm/syscall_32.h b/arch/sh/include/asm/syscall_32.h
index 6e118799831c..5d636a3593d1 100644
--- a/arch/sh/include/asm/syscall_32.h
+++ b/arch/sh/include/asm/syscall_32.h
@@ -40,10 +40,7 @@ static inline void syscall_set_return_value(struct 
task_struct *task,
struct pt_regs *regs,
int error, long val)
 {
-   if (error)
-   regs->regs[0] = -error;
-   else
-   regs->regs[0] = val;
+   regs->regs[0] = error ?: val;
 }
 
 static inline void syscall_get_arguments(struct task_struct *task,
diff --git a/arch/sh/include/asm/syscall_64.h b/arch/sh/include/asm/syscall_64.h
index 43882580c7f9..799fe72316e2 100644
--- a/arch/sh/include/asm/syscall_64.h
+++ b/arch/sh/include/asm/syscall_64.h
@@ -39,10 +39,7 @@ static inline void syscall_set_return_value(struct 
task_struct *task,
struct pt_regs *regs,
int error, long val)
 {
-   if (error)
-   regs->regs[9] = -error;
-   else
-   regs->regs[9] = val;
+   regs->regs[9] = error ?: val;
 }
 
 static inline void syscall_get_arguments(struct task_struct *task,
-- 
ldv


[PATCH 01/13] Move EM_HEXAGON to uapi/linux/elf-em.h

2018-11-08 Thread Dmitry V. Levin
This should never have been defined in the arch tree to begin with,
and now uapi/linux/audit.h header is going to use EM_HEXAGON
in order to define AUDIT_ARCH_HEXAGON which is needed to implement
syscall_get_arch() which in turn is required to extend
the generic ptrace API with PTRACE_GET_SYSCALL_INFO request.

Signed-off-by: Dmitry V. Levin 
---
 arch/hexagon/include/asm/elf.h | 6 +-
 include/uapi/linux/elf-em.h| 1 +
 2 files changed, 2 insertions(+), 5 deletions(-)

diff --git a/arch/hexagon/include/asm/elf.h b/arch/hexagon/include/asm/elf.h
index 80311e7b8ca6..d10fbd54ae51 100644
--- a/arch/hexagon/include/asm/elf.h
+++ b/arch/hexagon/include/asm/elf.h
@@ -23,11 +23,7 @@
 
 #include 
 #include 
-
-/*
- * This should really be in linux/elf-em.h.
- */
-#define EM_HEXAGON 164   /* QUALCOMM Hexagon */
+#include 
 
 struct elf32_hdr;
 
diff --git a/include/uapi/linux/elf-em.h b/include/uapi/linux/elf-em.h
index 93722e60204c..ba3696e3d694 100644
--- a/include/uapi/linux/elf-em.h
+++ b/include/uapi/linux/elf-em.h
@@ -37,6 +37,7 @@
 #define EM_BLACKFIN 106 /* ADI Blackfin Processor */
 #define EM_ALTERA_NIOS2113 /* Altera Nios II soft-core processor */
 #define EM_TI_C6000140 /* TI C6X DSPs */
+#define EM_HEXAGON 164 /* QUALCOMM Hexagon */
 #define EM_AARCH64 183 /* ARM 64 bit */
 #define EM_TILEPRO 188 /* Tilera TILEPro */
 #define EM_MICROBLAZE  189 /* Xilinx MicroBlaze */
-- 
ldv


[PATCH 03/13] elf-em.h: add EM_NDS32

2018-11-08 Thread Dmitry V. Levin
The uapi/linux/audit.h header is going to use EM_NDS32 in order
to define AUDIT_ARCH_NDS32 which is needed to implement
syscall_get_arch() which in turn is required to extend
the generic ptrace API with PTRACE_GET_SYSCALL_INFO request.

The value for EM_NDS32 has been taken from
http://www.sco.com/developers/gabi/2012-12-31/ch4.eheader.html

Signed-off-by: Dmitry V. Levin 
---
 include/uapi/linux/elf-em.h | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/include/uapi/linux/elf-em.h b/include/uapi/linux/elf-em.h
index 56ff3f9d9633..f879c24a7c21 100644
--- a/include/uapi/linux/elf-em.h
+++ b/include/uapi/linux/elf-em.h
@@ -39,6 +39,8 @@
 #define EM_ALTERA_NIOS2113 /* Altera Nios II soft-core processor */
 #define EM_TI_C6000140 /* TI C6X DSPs */
 #define EM_HEXAGON 164 /* QUALCOMM Hexagon */
+#define EM_NDS32   167 /* Andes Technology compact code size
+  embedded RISC processor family */
 #define EM_AARCH64 183 /* ARM 64 bit */
 #define EM_TILEPRO 188 /* Tilera TILEPro */
 #define EM_MICROBLAZE  189 /* Xilinx MicroBlaze */
-- 
ldv


[PATCH 04/13] elf-em.h: add EM_XTENSA

2018-11-08 Thread Dmitry V. Levin
The uapi/linux/audit.h header is going to use EM_XTENSA in order
to define AUDIT_ARCH_XTENSA which is needed to implement
syscall_get_arch() which in turn is required to extend
the generic ptrace API with PTRACE_GET_SYSCALL_INFO request.

The value for EM_XTENSA has been taken from
http://www.sco.com/developers/gabi/2012-12-31/ch4.eheader.html

Signed-off-by: Dmitry V. Levin 
---
 include/uapi/linux/elf-em.h | 1 +
 1 file changed, 1 insertion(+)

diff --git a/include/uapi/linux/elf-em.h b/include/uapi/linux/elf-em.h
index f879c24a7c21..2639119bf459 100644
--- a/include/uapi/linux/elf-em.h
+++ b/include/uapi/linux/elf-em.h
@@ -35,6 +35,7 @@
 #define EM_M32R88  /* Renesas M32R */
 #define EM_MN10300 89  /* Panasonic/MEI MN10300, AM33 */
 #define EM_OPENRISC 92 /* OpenRISC 32-bit embedded processor */
+#define EM_XTENSA  94  /* Tensilica Xtensa Architecture */
 #define EM_BLACKFIN 106 /* ADI Blackfin Processor */
 #define EM_ALTERA_NIOS2113 /* Altera Nios II soft-core processor */
 #define EM_TI_C6000140 /* TI C6X DSPs */
-- 
ldv


[PATCH 05/13] m68k: define syscall_get_arch()

2018-11-08 Thread Dmitry V. Levin
syscall_get_arch() is required to be implemented on all architectures
that use tracehook_report_syscall_entry() in order to extend
the generic ptrace API with PTRACE_GET_SYSCALL_INFO request.

Signed-off-by: Dmitry V. Levin 
---
 arch/m68k/include/asm/syscall.h | 12 
 1 file changed, 12 insertions(+)
 create mode 100644 arch/m68k/include/asm/syscall.h

diff --git a/arch/m68k/include/asm/syscall.h b/arch/m68k/include/asm/syscall.h
new file mode 100644
index ..d4d7deda8d50
--- /dev/null
+++ b/arch/m68k/include/asm/syscall.h
@@ -0,0 +1,12 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _ASM_M68K_SYSCALL_H
+#define _ASM_M68K_SYSCALL_H
+
+#include 
+
+static inline int syscall_get_arch(void)
+{
+   return AUDIT_ARCH_M68K;
+}
+
+#endif /* _ASM_M68K_SYSCALL_H */
-- 
ldv


[PATCH 08/13] h8300: define syscall_get_arch()

2018-11-08 Thread Dmitry V. Levin
syscall_get_arch() is required to be implemented on all architectures
that use tracehook_report_syscall_entry() in order to extend
the generic ptrace API with PTRACE_GET_SYSCALL_INFO request.

Signed-off-by: Dmitry V. Levin 
---
 arch/h8300/include/asm/syscall.h | 5 +
 include/uapi/linux/audit.h   | 1 +
 2 files changed, 6 insertions(+)

diff --git a/arch/h8300/include/asm/syscall.h b/arch/h8300/include/asm/syscall.h
index 924990401237..699664a0b1be 100644
--- a/arch/h8300/include/asm/syscall.h
+++ b/arch/h8300/include/asm/syscall.h
@@ -8,6 +8,7 @@
 #include 
 #include 
 #include 
+#include 
 
 static inline int
 syscall_get_nr(struct task_struct *task, struct pt_regs *regs)
@@ -47,6 +48,10 @@ syscall_get_arguments(struct task_struct *task, struct 
pt_regs *regs,
}
 }
 
+static inline int syscall_get_arch(void)
+{
+   return AUDIT_ARCH_H8300;
+}
 
 
 /* Misc syscall related bits */
diff --git a/include/uapi/linux/audit.h b/include/uapi/linux/audit.h
index 3eb1397c2b8f..2319283f00e5 100644
--- a/include/uapi/linux/audit.h
+++ b/include/uapi/linux/audit.h
@@ -381,6 +381,7 @@ enum {
 #define AUDIT_ARCH_C6X (EM_TI_C6000)
 #define AUDIT_ARCH_CRIS(EM_CRIS|__AUDIT_ARCH_LE)
 #define AUDIT_ARCH_FRV (EM_FRV)
+#define AUDIT_ARCH_H8300   (EM_H8_300)
 #define AUDIT_ARCH_I386(EM_386|__AUDIT_ARCH_LE)
 #define AUDIT_ARCH_IA64
(EM_IA_64|__AUDIT_ARCH_64BIT|__AUDIT_ARCH_LE)
 #define AUDIT_ARCH_M32R(EM_M32R)
-- 
ldv


[PATCH 09/13] hexagon: define syscall_get_arch()

2018-11-08 Thread Dmitry V. Levin
syscall_get_arch() is required to be implemented on all architectures
that use tracehook_report_syscall_entry() in order to extend
the generic ptrace API with PTRACE_GET_SYSCALL_INFO request.

Signed-off-by: Dmitry V. Levin 
---
 arch/hexagon/include/asm/syscall.h | 8 
 include/uapi/linux/audit.h | 1 +
 2 files changed, 9 insertions(+)

diff --git a/arch/hexagon/include/asm/syscall.h 
b/arch/hexagon/include/asm/syscall.h
index 4af9c7b6f13a..de3917aad3fd 100644
--- a/arch/hexagon/include/asm/syscall.h
+++ b/arch/hexagon/include/asm/syscall.h
@@ -21,6 +21,8 @@
 #ifndef _ASM_HEXAGON_SYSCALL_H
 #define _ASM_HEXAGON_SYSCALL_H
 
+#include 
+
 typedef long (*syscall_fn)(unsigned long, unsigned long,
unsigned long, unsigned long,
unsigned long, unsigned long);
@@ -43,4 +45,10 @@ static inline void syscall_get_arguments(struct task_struct 
*task,
BUG_ON(i + n > 6);
memcpy(args, &(®s->r00)[i], n * sizeof(args[0]));
 }
+
+static inline int syscall_get_arch(void)
+{
+   return AUDIT_ARCH_HEXAGON;
+}
+
 #endif
diff --git a/include/uapi/linux/audit.h b/include/uapi/linux/audit.h
index 2319283f00e5..d5a4f623e47e 100644
--- a/include/uapi/linux/audit.h
+++ b/include/uapi/linux/audit.h
@@ -382,6 +382,7 @@ enum {
 #define AUDIT_ARCH_CRIS(EM_CRIS|__AUDIT_ARCH_LE)
 #define AUDIT_ARCH_FRV (EM_FRV)
 #define AUDIT_ARCH_H8300   (EM_H8_300)
+#define AUDIT_ARCH_HEXAGON (EM_HEXAGON)
 #define AUDIT_ARCH_I386(EM_386|__AUDIT_ARCH_LE)
 #define AUDIT_ARCH_IA64
(EM_IA_64|__AUDIT_ARCH_64BIT|__AUDIT_ARCH_LE)
 #define AUDIT_ARCH_M32R(EM_M32R)
-- 
ldv


Re: [PATCH v6] pidns: introduce syscall translate_pid

2018-11-08 Thread Dmitry V. Levin
Hi,

On Mon, Jul 16, 2018 at 10:57:48AM -0700, Nagarathnam Muthusamy wrote:
> On 06/01/2018 12:18 PM, Konstantin Khlebnikov wrote:
> > Each process have different pids, one for each pid namespace it belongs.
> > When interaction happens within single pid-ns translation isn't required.
> > More complicated scenarios needs special handling.
> >
> > For example:
> > - reading pid-files or logs written inside container with pid namespace
> > - writing logs with internal pids outside container for pushing them into
> > - attaching with ptrace to tasks from different pid namespace
> >
> > Generally speaking, any cross pid-ns API with pids needs translation.
> >
> > Currently there are several interfaces that could be used here:
> >
> > Pid namespaces are identified by device and inode of /proc/[pid]/ns/pid.
> >
> > Pids for nested pid namespaces are shown in file /proc/[pid]/status.
> > In some cases pid translation could be easily done using this information.
> > Backward translation requires scanning all tasks and becomes really
> > complicated for deeper namespace nesting.
> >
> > Unix socket automatically translates pid attached to SCM_CREDENTIALS.
> > This requires CAP_SYS_ADMIN for sending arbitrary pids and entering
> > into pid namespace, this expose process and could be insecure.
> >
> > This patch adds new syscall for converting pids between pid namespaces:
> >
> > pid_t translate_pid(pid_t pid, int source, int target);
> >
> > Pid-namespaces are referred file descriptors opened to proc files
> > /proc/[pid]/ns/pid or /proc/[pid]/ns/pid_for_children.
> > Negative argument points to current pid namespace.
> >
> > Syscall returns pid in target pid-ns or zero if task have no pid there.
> >
> > Error codes:
> > EBADF- file descriptor is closed
> > EINVAL   - file descriptor isn't pid namespace
> > ESRCH- task not found in @source namespace
> >
> > Translation could breach pid-ns isolation and return pids from outer pid
> > namespaces iff process already has file descriptor for these namespaces.
> >
> > Examples:
> > translate_pid(pid, ns, -1)  - get pid in our pid namespace
> > translate_pid(pid, -1, ns)  - get pid in other pid namespace
> > translate_pid(1, ns, -1)- get pid of init task for namespace
> > translate_pid(pid, -1, ns) > 0  - is pid is reachable from ns?
> > translate_pid(1, ns1, ns2) > 0  - is ns1 inside ns2?
> > translate_pid(1, ns1, ns2) == 0 - is ns1 outside ns2?
> > translate_pid(1, ns1, ns2) == 1 - is ns1 equal ns2?
> >
> > Signed-off-by: Konstantin Khlebnikov 
> > Reanimated-by: Nagarathnam Muthusamy 
> >
> > ---
> >
> > v1: https://lkml.org/lkml/2015/9/15/411
> > v2: https://lkml.org/lkml/2015/9/24/278
> >   * use namespace-fd as second/third argument
> >   * add -pid for getting parent pid
> >   * move code into kernel/sys.c next to getppid
> >   * drop ifdef CONFIG_PID_NS
> >   * add generic syscall
> > v3: https://lkml.org/lkml/2015/9/28/3
> >   * use proc_ns_fdget()
> >   * update description
> >   * rebase to next-20150925
> >   * fix conflict with mlock2
> > v4: https://lkml.org/lkml/2017/10/13/177
> >   * rename from getvpid() into translate_pid()
> >   * remove syscall if CONFIG_PID_NS=n
> >   * drop -pid for parent task
> >   * drop fget-fdget optimizations
> >   * add helper get_pid_ns_by_fd()
> >   * wire only into x86
> > v5: https://lkml.org/lkml/2018/4/4/677
> >   * rewrite commit message
> >   * resolve pidns by task pid or by pidns fd
> >   * add arguments source_type and target_type
> > v6:
> >   * revert back minimized v4 design
> >   * rebase to next-20180601
> >   * fix COND_SYSCALL stub
> >   * use next syscall number, old used for io_pgetevents
> >
> > --- sample tool ---
> >
> > #define _GNU_SOURCE
> > #include 
> > #include 
> > #include 
> > #include 
> > #include 
> > #include 
> > #include 
> >
> > #ifndef SYS_translate_pid
> > #ifdef __x86_64__
> > #define SYS_translate_pid 334
> > #elif defined __i386__
> > #define SYS_translate_pid 386
> > #endif
> > #endif
> >
> > pid_t translate_pid(pid_t pid, int source, int target) {
> > return syscall(SYS_translate_pid, pid, source, target);
> > }
> >
> > int main(int argc, char **argv) {
> > int pid, source, target;
> > char buf[64];
> >
> > if (argc != 4)
> > errx(1, "usage: %s   ", argv[0]);
> >
> > pid = atoi(argv[1]);
> > source = atoi(argv[2]);
> > target = atoi(argv[3]);
> >
> > if (source > 0) {
> > snprintf(buf, sizeof(buf), "/proc/%d/ns/pid", source);
> > source = open(buf, O_RDONLY);
> > if (source < 0)
> > err(2, "open source %s", buf);
> > }
> >
> > if (target > 0) {
> > snprintf(buf, sizeof(buf), "/proc/%d/ns/pid", target);
> > target = open(buf, O_RDONLY);
> > if (target < 0)
> > err(2, "open target %s", buf);
> > }
> >
> > pid = translate_pid(pid, source, target);
> > if (pid < 0)
> > err(2, "t

Re: [PATCH 12/13] riscv: define syscall_get_arch()

2018-11-09 Thread Dmitry V. Levin
On Fri, Nov 09, 2018 at 10:45:54AM -0800, Palmer Dabbelt wrote:
> On Thu, 08 Nov 2018 19:17:13 PST (-0800), l...@altlinux.org wrote:
> > syscall_get_arch() is required to be implemented on all architectures
> > that use tracehook_report_syscall_entry() in order to extend
> > the generic ptrace API with PTRACE_GET_SYSCALL_INFO request.
> >
> > Signed-off-by: Dmitry V. Levin 
> > ---
> >  arch/riscv/include/asm/syscall.h | 6 ++
> >  include/uapi/linux/audit.h   | 1 +
> >  2 files changed, 7 insertions(+)
> >
> > diff --git a/arch/riscv/include/asm/syscall.h 
> > b/arch/riscv/include/asm/syscall.h
> > index 8d25f8904c00..7e1e26ca7317 100644
> > --- a/arch/riscv/include/asm/syscall.h
> > +++ b/arch/riscv/include/asm/syscall.h
> > @@ -18,6 +18,7 @@
> >  #ifndef _ASM_RISCV_SYSCALL_H
> >  #define _ASM_RISCV_SYSCALL_H
> >
> > +#include 
> >  #include 
> >  #include 
> >
> > @@ -99,4 +100,9 @@ static inline void syscall_set_arguments(struct 
> > task_struct *task,
> > memcpy(®s->a1 + i * sizeof(regs->a1), args, n * sizeof(regs->a0));
> >  }
> >
> > +static inline int syscall_get_arch(void)
> > +{
> > +   return AUDIT_ARCH_RISCV;
> > +}
> > +
> >  #endif /* _ASM_RISCV_SYSCALL_H */
> > diff --git a/include/uapi/linux/audit.h b/include/uapi/linux/audit.h
> > index c4c8b131af48..ad4105c602a1 100644
> > --- a/include/uapi/linux/audit.h
> > +++ b/include/uapi/linux/audit.h
> > @@ -405,6 +405,7 @@ enum {
> >  /* do not define AUDIT_ARCH_PPCLE since it is not supported by audit */
> >  #define AUDIT_ARCH_PPC64   (EM_PPC64|__AUDIT_ARCH_64BIT)
> >  #define AUDIT_ARCH_PPC64LE (EM_PPC64|__AUDIT_ARCH_64BIT|__AUDIT_ARCH_LE)
> > +#define AUDIT_ARCH_RISCV   (EM_RISCV|__AUDIT_ARCH_64BIT)
> >  #define AUDIT_ARCH_S390(EM_S390)
> >  #define AUDIT_ARCH_S390X   (EM_S390|__AUDIT_ARCH_64BIT)
> >  #define AUDIT_ARCH_SH  (EM_SH)
> 
> I think this is incorrect: EM_RISCV has 32-bit and 64-bit variants, and if I 
> understand what's going on here this is marking all RISC-V targets as 64-bit. 
>  
> Since this is a userspace header, I think the right thing to switch on is 
> __riscv_xlen, which will be defined to either 32 or 64 depending on the base 
> ISA.
> We're also little endian.

OK, it means we need to introduce two different AUDIT_ARCH_ constants
for RISC-V.  Do you have any preferences for their names,
e.g. AUDIT_ARCH_RISCV and AUDIT_ARCH_RISCV64, or
AUDIT_ARCH_RISCV and AUDIT_ARCH_RISCV32, or
AUDIT_ARCH_RISCV64 and AUDIT_ARCH_RISCV32,
or anything else?


-- 
ldv


signature.asc
Description: PGP signature


[PATCH 14/13] Move EM_UNICORE to uapi/linux/elf-em.h

2018-11-10 Thread Dmitry V. Levin
This should never have been defined in the arch tree to begin with,
and now uapi/linux/audit.h header is going to use EM_UNICORE
in order to define AUDIT_ARCH_UNICORE which is needed to implement
syscall_get_arch() which in turn is required to extend
the generic ptrace API with PTRACE_GET_SYSCALL_INFO request.

Signed-off-by: Dmitry V. Levin 
---
Apparently, we need to implement syscall_get_arch() on all architectures
where linux/tracehook.h is compiled, not just those that use
tracehook_report_syscall_entry().
This adds one more architecture to the initial list of 9 architectures
where syscall_get_arch() has to be implemented.

 arch/unicore32/include/asm/elf.h | 3 +--
 include/uapi/linux/elf-em.h  | 1 +
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/arch/unicore32/include/asm/elf.h b/arch/unicore32/include/asm/elf.h
index 829042d07722..ae66dc1be49e 100644
--- a/arch/unicore32/include/asm/elf.h
+++ b/arch/unicore32/include/asm/elf.h
@@ -19,6 +19,7 @@
  * ELF register definitions..
  */
 #include 
+#include 
 
 typedef unsigned long elf_greg_t;
 typedef unsigned long elf_freg_t[3];
@@ -28,8 +29,6 @@ typedef elf_greg_t elf_gregset_t[ELF_NGREG];
 
 typedef struct fp_state elf_fpregset_t;
 
-#define EM_UNICORE 110
-
 #define R_UNICORE_NONE 0
 #define R_UNICORE_PC24 1
 #define R_UNICORE_ABS322
diff --git a/include/uapi/linux/elf-em.h b/include/uapi/linux/elf-em.h
index 4b81fc1a949a..7b02cf339d8f 100644
--- a/include/uapi/linux/elf-em.h
+++ b/include/uapi/linux/elf-em.h
@@ -37,6 +37,7 @@
 #define EM_ARCOMPACT   93  /* ARCompact processor */
 #define EM_XTENSA  94  /* Tensilica Xtensa Architecture */
 #define EM_BLACKFIN 106 /* ADI Blackfin Processor */
+#define EM_UNICORE 110 /* UniCore-32 */
 #define EM_ALTERA_NIOS2113 /* Altera Nios II soft-core processor */
 #define EM_TI_C6000140 /* TI C6X DSPs */
 #define EM_HEXAGON 164 /* QUALCOMM Hexagon */
-- 
ldv


Re: [PATCH bpf 1/2] bpf: fix alignment of netns_dev/netns_ino fields in bpf_{map,prog}_info

2018-05-30 Thread Dmitry V. Levin
On Sun, May 27, 2018 at 01:28:42PM +0200, Eugene Syromiatnikov wrote:
> Recent introduction of netns_dev/netns_ino to bpf_map_info/bpf_prog info
> has broken compat, as offsets of these fields are different in 32-bit
> and 64-bit ABIs.  One fix (other than implementing compat support in
> syscall in order to handle this discrepancy) is to use __aligned_u64
> instead of __u64 for these fields.
> 
> Reported-by: Dmitry V. Levin 
> Fixes: 52775b33bb507 ("bpf: offload: report device information about
> offloaded maps")
> Fixes: 675fc275a3a2d ("bpf: offload: report device information for
> offloaded programs")

Reviewed-by: "Dmitry V. Levin" 
Cc:  # v4.16+

Thanks,


-- 
ldv


signature.asc
Description: PGP signature


Re: [PATCH v5 13/25] m68k: add asm/syscall.h

2018-12-12 Thread Dmitry V. Levin
On Mon, Dec 10, 2018 at 04:30:25PM +0300, Dmitry V. Levin wrote:
> On Mon, Dec 10, 2018 at 02:06:28PM +0100, Geert Uytterhoeven wrote:
> > On Mon, Dec 10, 2018 at 1:41 PM Dmitry V. Levin  wrote:
> > > On Mon, Dec 10, 2018 at 09:45:42AM +0100, Geert Uytterhoeven wrote:
> > > > On Mon, Dec 10, 2018 at 5:30 AM Dmitry V. Levin  
> > > > wrote:
> > > > > syscall_get_* functions are required to be implemented on all
> > > > > architectures in order to extend the generic ptrace API with
> > > > > PTRACE_GET_SYSCALL_INFO request.
> > > > >
> > > > > This introduces asm/syscall.h on m68k implementing all 5 syscall_get_*
> > > > > functions as documented in asm-generic/syscall.h: syscall_get_nr,
> > > > > syscall_get_arguments, syscall_get_error, syscall_get_return_value,
> > > > > and syscall_get_arch.
> > > > >
> > > > > Cc: Geert Uytterhoeven 
> > > > > Cc: Oleg Nesterov 
> > > > > Cc: Andy Lutomirski 
> > > > > Cc: Elvira Khabirova 
> > > > > Cc: Eugene Syromyatnikov 
> > > > > Cc: linux-m...@lists.linux-m68k.org
> > > > > Signed-off-by: Dmitry V. Levin 
> > > > > ---
> > > > >
> > > > > Notes:
> > > > > v5: added syscall_get_nr, syscall_get_arguments, 
> > > > > syscall_get_error,
> > > > > and syscall_get_return_value
> > > > > v1: added syscall_get_arch
> > > >
> > > > > --- /dev/null
> > > > > +++ b/arch/m68k/include/asm/syscall.h
> > > > > @@ -0,0 +1,39 @@
> > > >
> > > > > +static inline void
> > > > > +syscall_get_arguments(struct task_struct *task, struct pt_regs *regs,
> > > > > + unsigned int i, unsigned int n, unsigned long 
> > > > > *args)
> > > > > +{
> > > > > +   BUG_ON(i + n > 6);
> > > >
> > > > Does this have to crash the kernel?
> > >
> > > This is what most of other architectures do, but we could choose
> > > a softer approach, e.g. use WARN_ON_ONCE instead.
> > >
> > > > Perhaps you can return an error code instead?
> > >
> > > That would be problematic given the signature of this function
> > > and the nature of the potential bug which would most likely be a usage 
> > > error.
> > 
> > Of course to handle that, the function's signature need to be changed.
> > Changing it has the advantage that the error handling can be done at the
> > caller, in common code, instead of duplicating it for all
> > architectures, possibly
> > leading to different semantics.
> 
> Given that *all* current users of syscall_get_arguments specify i == 0
> (and there is an architecture that has BUG_ON(i)), 
> it should be really a usage error to get into situation where i + n > 6,
> I wish a BUILD_BUG_ON could be used here instead.
> 
> I don't think it worths pushing the change of API just to convert
> a "cannot happen" assertion into an error that would have to be dealt with
> on the caller side.

I suggest the following BUG_ON replacement for syscall_get_arguments:

#define SYSCALL_MAX_ARGS 6

static inline void
syscall_get_arguments(struct task_struct *task, struct pt_regs *regs,
  unsigned int i, unsigned int n, unsigned long *args)
{
/*
 * Ideally there should have been
 * BUILD_BUG_ON(i + n > SYSCALL_MAX_ARGS);
 * instead of these checks.
 */
if (unlikely(i > SYSCALL_MAX_ARGS)) {
WARN_ONCE(1, "i > SYSCALL_MAX_ARGS");
return;
}
if (unlikely(n > SYSCALL_MAX_ARGS - i)) {
WARN_ONCE(1, "i + n > SYSCALL_MAX_ARGS");
n = SYSCALL_MAX_ARGS - i;
}
BUILD_BUG_ON(sizeof(regs->d1) != sizeof(args[0]));
memcpy(args, ®s->d1 + i, n * sizeof(args[0]));
}


-- 
ldv


signature.asc
Description: PGP signature


Re: [PATCH v5 13/25] m68k: add asm/syscall.h

2018-12-12 Thread Dmitry V. Levin
On Wed, Dec 12, 2018 at 10:01:29AM +0100, Geert Uytterhoeven wrote:
> Hi Dmitry,
> 
> On Wed, Dec 12, 2018 at 9:55 AM Dmitry V. Levin  wrote:
> > On Mon, Dec 10, 2018 at 04:30:25PM +0300, Dmitry V. Levin wrote:
> > > On Mon, Dec 10, 2018 at 02:06:28PM +0100, Geert Uytterhoeven wrote:
> > > > On Mon, Dec 10, 2018 at 1:41 PM Dmitry V. Levin  
> > > > wrote:
> > > > > On Mon, Dec 10, 2018 at 09:45:42AM +0100, Geert Uytterhoeven wrote:
> > > > > > On Mon, Dec 10, 2018 at 5:30 AM Dmitry V. Levin  
> > > > > > wrote:
> > > > > > > syscall_get_* functions are required to be implemented on all
> > > > > > > architectures in order to extend the generic ptrace API with
> > > > > > > PTRACE_GET_SYSCALL_INFO request.
> > > > > > >
> > > > > > > This introduces asm/syscall.h on m68k implementing all 5 
> > > > > > > syscall_get_*
> > > > > > > functions as documented in asm-generic/syscall.h: syscall_get_nr,
> > > > > > > syscall_get_arguments, syscall_get_error, 
> > > > > > > syscall_get_return_value,
> > > > > > > and syscall_get_arch.
> > > > > > >
> > > > > > > Cc: Geert Uytterhoeven 
> > > > > > > Cc: Oleg Nesterov 
> > > > > > > Cc: Andy Lutomirski 
> > > > > > > Cc: Elvira Khabirova 
> > > > > > > Cc: Eugene Syromyatnikov 
> > > > > > > Cc: linux-m...@lists.linux-m68k.org
> > > > > > > Signed-off-by: Dmitry V. Levin 
> > > > > > > ---
> > > > > > >
> > > > > > > Notes:
> > > > > > > v5: added syscall_get_nr, syscall_get_arguments, 
> > > > > > > syscall_get_error,
> > > > > > > and syscall_get_return_value
> > > > > > > v1: added syscall_get_arch
> > > > > >
> > > > > > > --- /dev/null
> > > > > > > +++ b/arch/m68k/include/asm/syscall.h
> > > > > > > @@ -0,0 +1,39 @@
> > > > > >
> > > > > > > +static inline void
> > > > > > > +syscall_get_arguments(struct task_struct *task, struct pt_regs 
> > > > > > > *regs,
> > > > > > > + unsigned int i, unsigned int n, unsigned 
> > > > > > > long *args)
> > > > > > > +{
> > > > > > > +   BUG_ON(i + n > 6);
> > > > > >
> > > > > > Does this have to crash the kernel?
> > > > >
> > > > > This is what most of other architectures do, but we could choose
> > > > > a softer approach, e.g. use WARN_ON_ONCE instead.
> > > > >
> > > > > > Perhaps you can return an error code instead?
> > > > >
> > > > > That would be problematic given the signature of this function
> > > > > and the nature of the potential bug which would most likely be a 
> > > > > usage error.
> > > >
> > > > Of course to handle that, the function's signature need to be changed.
> > > > Changing it has the advantage that the error handling can be done at the
> > > > caller, in common code, instead of duplicating it for all
> > > > architectures, possibly
> > > > leading to different semantics.
> > >
> > > Given that *all* current users of syscall_get_arguments specify i == 0
> > > (and there is an architecture that has BUG_ON(i)),
> > > it should be really a usage error to get into situation where i + n > 6,
> > > I wish a BUILD_BUG_ON could be used here instead.
> > >
> > > I don't think it worths pushing the change of API just to convert
> > > a "cannot happen" assertion into an error that would have to be dealt with
> > > on the caller side.
> >
> > I suggest the following BUG_ON replacement for syscall_get_arguments:
> >
> > #define SYSCALL_MAX_ARGS 6
> >
> > static inline void
> > syscall_get_arguments(struct task_struct *task, struct pt_regs *regs,
> >   unsigned int i, unsigned int n, unsigned long *args)
> > {
> > /*
> >  * Ideally there should have been
> >  * BUILD_BUG_ON(i + n > SYSCALL_MAX_ARGS);
> >  * instead of these checks.
> >  */
> > if (unlikely(i > SYSCALL_MAX_ARGS)) {
> > WARN_ONCE(1, "i > SYSCALL_MAX_ARGS");
> > return;
> 
> Does this have security implications, as args is an output parameter?
> I.e. if you don't fill the array, the caller will use whatever is on the 
> stack.
> Can this ever be passed to userspace, leaking data?

In the current kernel code n is always less or equal to 6,
but in theory future changes can potentially break the assertion
and this could lead to leaking data to userspace.

Do you think we should rather be defensive and add some memsets, e.g.

if (unlikely(i > SYSCALL_MAX_ARGS)) {
WARN_ONCE(1, "i > SYSCALL_MAX_ARGS");
memset(args, 0, n * sizeof(args[0]));
return;
}
if (unlikely(n > SYSCALL_MAX_ARGS - i)) {
unsigned int extra = n - (SYSCALL_MAX_ARGS - i);

WARN_ONCE(1, "i + n > SYSCALL_MAX_ARGS");
n = SYSCALL_MAX_ARGS - i;
memset(&args[n], 0, extra * sizeof(args[0]));
}
?


-- 
ldv


signature.asc
Description: PGP signature


Re: [PATCH v5 13/25] m68k: add asm/syscall.h

2018-12-12 Thread Dmitry V. Levin
Hi Geert,

On Wed, Dec 12, 2018 at 10:43:33AM +0100, Geert Uytterhoeven wrote:
> On Wed, Dec 12, 2018 at 10:27 AM Dmitry V. Levin  wrote:
> > On Wed, Dec 12, 2018 at 10:01:29AM +0100, Geert Uytterhoeven wrote:
> > > On Wed, Dec 12, 2018 at 9:55 AM Dmitry V. Levin  wrote:
> > > > On Mon, Dec 10, 2018 at 04:30:25PM +0300, Dmitry V. Levin wrote:
> > > > > On Mon, Dec 10, 2018 at 02:06:28PM +0100, Geert Uytterhoeven wrote:
> > > > > > On Mon, Dec 10, 2018 at 1:41 PM Dmitry V. Levin  
> > > > > > wrote:
> > > > > > > On Mon, Dec 10, 2018 at 09:45:42AM +0100, Geert Uytterhoeven 
> > > > > > > wrote:
> > > > > > > > On Mon, Dec 10, 2018 at 5:30 AM Dmitry V. Levin 
> > > > > > > >  wrote:
> > > > > > > > > syscall_get_* functions are required to be implemented on all
> > > > > > > > > architectures in order to extend the generic ptrace API with
> > > > > > > > > PTRACE_GET_SYSCALL_INFO request.
> > > > > > > > >
> > > > > > > > > This introduces asm/syscall.h on m68k implementing all 5 
> > > > > > > > > syscall_get_*
> > > > > > > > > functions as documented in asm-generic/syscall.h: 
> > > > > > > > > syscall_get_nr,
> > > > > > > > > syscall_get_arguments, syscall_get_error, 
> > > > > > > > > syscall_get_return_value,
> > > > > > > > > and syscall_get_arch.
> > > > > > > > >
> > > > > > > > > Cc: Geert Uytterhoeven 
> > > > > > > > > Cc: Oleg Nesterov 
> > > > > > > > > Cc: Andy Lutomirski 
> > > > > > > > > Cc: Elvira Khabirova 
> > > > > > > > > Cc: Eugene Syromyatnikov 
> > > > > > > > > Cc: linux-m...@lists.linux-m68k.org
> > > > > > > > > Signed-off-by: Dmitry V. Levin 
> > > > > > > > > ---
> > > > > > > > >
> > > > > > > > > Notes:
> > > > > > > > > v5: added syscall_get_nr, syscall_get_arguments, 
> > > > > > > > > syscall_get_error,
> > > > > > > > > and syscall_get_return_value
> > > > > > > > > v1: added syscall_get_arch
> > > > > > > >
> > > > > > > > > --- /dev/null
> > > > > > > > > +++ b/arch/m68k/include/asm/syscall.h
> > > > > > > > > @@ -0,0 +1,39 @@
> > > > > > > >
> > > > > > > > > +static inline void
> > > > > > > > > +syscall_get_arguments(struct task_struct *task, struct 
> > > > > > > > > pt_regs *regs,
> > > > > > > > > + unsigned int i, unsigned int n, 
> > > > > > > > > unsigned long *args)
> > > > > > > > > +{
> > > > > > > > > +   BUG_ON(i + n > 6);
> > > > > > > >
> > > > > > > > Does this have to crash the kernel?
> > > > > > >
> > > > > > > This is what most of other architectures do, but we could choose
> > > > > > > a softer approach, e.g. use WARN_ON_ONCE instead.
> > > > > > >
> > > > > > > > Perhaps you can return an error code instead?
> > > > > > >
> > > > > > > That would be problematic given the signature of this function
> > > > > > > and the nature of the potential bug which would most likely be a 
> > > > > > > usage error.
> > > > > >
> > > > > > Of course to handle that, the function's signature need to be 
> > > > > > changed.
> > > > > > Changing it has the advantage that the error handling can be done 
> > > > > > at the
> > > > > > caller, in common code, instead of duplicating it for all
> > > > > > architectures, possibly
> > > > > > leading to different semantics.
> > > > >
> > > > > Given that *all* current users of syscall_get_arguments specify i == 0
> > > > > (and there is an architecture t

Re: [PATCH v5 13/25] m68k: add asm/syscall.h

2018-12-12 Thread Dmitry V. Levin
Hi Geert,

On Wed, Dec 12, 2018 at 01:27:14PM +0100, Geert Uytterhoeven wrote:
> On Wed, Dec 12, 2018 at 1:04 PM Dmitry V. Levin  wrote:
> > On Wed, Dec 12, 2018 at 10:43:33AM +0100, Geert Uytterhoeven wrote:
> > > On Wed, Dec 12, 2018 at 10:27 AM Dmitry V. Levin  
> > > wrote:
> > > > On Wed, Dec 12, 2018 at 10:01:29AM +0100, Geert Uytterhoeven wrote:
> > > > > On Wed, Dec 12, 2018 at 9:55 AM Dmitry V. Levin  
> > > > > wrote:
> > > > > > On Mon, Dec 10, 2018 at 04:30:25PM +0300, Dmitry V. Levin wrote:
> > > > > > > On Mon, Dec 10, 2018 at 02:06:28PM +0100, Geert Uytterhoeven 
> > > > > > > wrote:
> > > > > > > > On Mon, Dec 10, 2018 at 1:41 PM Dmitry V. Levin 
> > > > > > > >  wrote:
> > > > > > > > > On Mon, Dec 10, 2018 at 09:45:42AM +0100, Geert Uytterhoeven 
> > > > > > > > > wrote:
> > > > > > > > > > On Mon, Dec 10, 2018 at 5:30 AM Dmitry V. Levin 
> > > > > > > > > >  wrote:
> > > > > > > > > > > syscall_get_* functions are required to be implemented on 
> > > > > > > > > > > all
> > > > > > > > > > > architectures in order to extend the generic ptrace API 
> > > > > > > > > > > with
> > > > > > > > > > > PTRACE_GET_SYSCALL_INFO request.
> > > > > > > > > > >
> > > > > > > > > > > This introduces asm/syscall.h on m68k implementing all 5 
> > > > > > > > > > > syscall_get_*
> > > > > > > > > > > functions as documented in asm-generic/syscall.h: 
> > > > > > > > > > > syscall_get_nr,
> > > > > > > > > > > syscall_get_arguments, syscall_get_error, 
> > > > > > > > > > > syscall_get_return_value,
> > > > > > > > > > > and syscall_get_arch.
> > > > > > > > > > >
> > > > > > > > > > > Cc: Geert Uytterhoeven 
> > > > > > > > > > > Cc: Oleg Nesterov 
> > > > > > > > > > > Cc: Andy Lutomirski 
> > > > > > > > > > > Cc: Elvira Khabirova 
> > > > > > > > > > > Cc: Eugene Syromyatnikov 
> > > > > > > > > > > Cc: linux-m...@lists.linux-m68k.org
> > > > > > > > > > > Signed-off-by: Dmitry V. Levin 
> > > > > > > > > > > ---
> > > > > > > > > > >
> > > > > > > > > > > Notes:
> > > > > > > > > > > v5: added syscall_get_nr, syscall_get_arguments, 
> > > > > > > > > > > syscall_get_error,
> > > > > > > > > > > and syscall_get_return_value
> > > > > > > > > > > v1: added syscall_get_arch
> > > > > > > > > >
> > > > > > > > > > > --- /dev/null
> > > > > > > > > > > +++ b/arch/m68k/include/asm/syscall.h
> > > > > > > > > > > @@ -0,0 +1,39 @@
> > > > > > > > > >
> > > > > > > > > > > +static inline void
> > > > > > > > > > > +syscall_get_arguments(struct task_struct *task, struct 
> > > > > > > > > > > pt_regs *regs,
> > > > > > > > > > > + unsigned int i, unsigned int n, 
> > > > > > > > > > > unsigned long *args)
> > > > > > > > > > > +{
> > > > > > > > > > > +   BUG_ON(i + n > 6);
> > > > > > > > > >
> > > > > > > > > > Does this have to crash the kernel?
> > > > > > > > >
> > > > > > > > > This is what most of other architectures do, but we could 
> > > > > > > > > choose
> > > > > > > > > a softer approach, e.g. use WARN_ON_ONCE instead.
> > > > > > > > >
> > > > > > > > > > Perhaps you can return an error code instead?
> > > > &

Re: [PATCH v5 13/25] m68k: add asm/syscall.h

2018-12-12 Thread Dmitry V. Levin
Hi Geert,

On Wed, Dec 12, 2018 at 01:54:05PM +0100, Geert Uytterhoeven wrote:
> On Wed, Dec 12, 2018 at 1:37 PM Dmitry V. Levin  wrote:
> > On Wed, Dec 12, 2018 at 01:27:14PM +0100, Geert Uytterhoeven wrote:
> > > On Wed, Dec 12, 2018 at 1:04 PM Dmitry V. Levin  wrote:
> > > > On Wed, Dec 12, 2018 at 10:43:33AM +0100, Geert Uytterhoeven wrote:
> > > > > On Wed, Dec 12, 2018 at 10:27 AM Dmitry V. Levin  
> > > > > wrote:
> > > > > > On Wed, Dec 12, 2018 at 10:01:29AM +0100, Geert Uytterhoeven wrote:
> > > > > > > On Wed, Dec 12, 2018 at 9:55 AM Dmitry V. Levin 
> > > > > > >  wrote:
> > > > > > > > On Mon, Dec 10, 2018 at 04:30:25PM +0300, Dmitry V. Levin wrote:
> > > > > > > > > On Mon, Dec 10, 2018 at 02:06:28PM +0100, Geert Uytterhoeven 
> > > > > > > > > wrote:
> > > > > > > > > > On Mon, Dec 10, 2018 at 1:41 PM Dmitry V. Levin 
> > > > > > > > > >  wrote:
> > > > > > > > > > > On Mon, Dec 10, 2018 at 09:45:42AM +0100, Geert 
> > > > > > > > > > > Uytterhoeven wrote:
> > > > > > > > > > > > On Mon, Dec 10, 2018 at 5:30 AM Dmitry V. Levin 
> > > > > > > > > > > >  wrote:
> > > > > > > > > > > > > syscall_get_* functions are required to be 
> > > > > > > > > > > > > implemented on all
> > > > > > > > > > > > > architectures in order to extend the generic ptrace 
> > > > > > > > > > > > > API with
> > > > > > > > > > > > > PTRACE_GET_SYSCALL_INFO request.
> > > > > > > > > > > > >
> > > > > > > > > > > > > This introduces asm/syscall.h on m68k implementing 
> > > > > > > > > > > > > all 5 syscall_get_*
> > > > > > > > > > > > > functions as documented in asm-generic/syscall.h: 
> > > > > > > > > > > > > syscall_get_nr,
> > > > > > > > > > > > > syscall_get_arguments, syscall_get_error, 
> > > > > > > > > > > > > syscall_get_return_value,
> > > > > > > > > > > > > and syscall_get_arch.
> > > > > > > > > > > > >
> > > > > > > > > > > > > Cc: Geert Uytterhoeven 
> > > > > > > > > > > > > Cc: Oleg Nesterov 
> > > > > > > > > > > > > Cc: Andy Lutomirski 
> > > > > > > > > > > > > Cc: Elvira Khabirova 
> > > > > > > > > > > > > Cc: Eugene Syromyatnikov 
> > > > > > > > > > > > > Cc: linux-m...@lists.linux-m68k.org
> > > > > > > > > > > > > Signed-off-by: Dmitry V. Levin 
> > > > > > > > > > > > > ---
> > > > > > > > > > > > >
> > > > > > > > > > > > > Notes:
> > > > > > > > > > > > > v5: added syscall_get_nr, syscall_get_arguments, 
> > > > > > > > > > > > > syscall_get_error,
> > > > > > > > > > > > > and syscall_get_return_value
> > > > > > > > > > > > > v1: added syscall_get_arch
> > > > > > > > > > > >
> > > > > > > > > > > > > --- /dev/null
> > > > > > > > > > > > > +++ b/arch/m68k/include/asm/syscall.h
> > > > > > > > > > > > > @@ -0,0 +1,39 @@
> > > > > > > > > > > >
> > > > > > > > > > > > > +static inline void
> > > > > > > > > > > > > +syscall_get_arguments(struct task_struct *task, 
> > > > > > > > > > > > > struct pt_regs *regs,
> > > > > > > > > > > > > + unsigned int i, unsigned int n, 
> > > > > > > > > > > > > unsigned long *args)
> > > > > > > > > > > > > +{
> > > &

Re: [PATCH v5 13/25] m68k: add asm/syscall.h

2018-12-12 Thread Dmitry V. Levin
Hi Geert,

On Wed, Dec 12, 2018 at 04:07:11PM +0300, Dmitry V. Levin wrote:
> On Wed, Dec 12, 2018 at 01:54:05PM +0100, Geert Uytterhoeven wrote:
> > On Wed, Dec 12, 2018 at 1:37 PM Dmitry V. Levin  wrote:
> > > On Wed, Dec 12, 2018 at 01:27:14PM +0100, Geert Uytterhoeven wrote:
> > > > On Wed, Dec 12, 2018 at 1:04 PM Dmitry V. Levin  
> > > > wrote:
> > > > > On Wed, Dec 12, 2018 at 10:43:33AM +0100, Geert Uytterhoeven wrote:
> > > > > > On Wed, Dec 12, 2018 at 10:27 AM Dmitry V. Levin 
> > > > > >  wrote:
> > > > > > > On Wed, Dec 12, 2018 at 10:01:29AM +0100, Geert Uytterhoeven 
> > > > > > > wrote:
> > > > > > > > On Wed, Dec 12, 2018 at 9:55 AM Dmitry V. Levin 
> > > > > > > >  wrote:
> > > > > > > > > On Mon, Dec 10, 2018 at 04:30:25PM +0300, Dmitry V. Levin 
> > > > > > > > > wrote:
> > > > > > > > > > On Mon, Dec 10, 2018 at 02:06:28PM +0100, Geert 
> > > > > > > > > > Uytterhoeven wrote:
> > > > > > > > > > > On Mon, Dec 10, 2018 at 1:41 PM Dmitry V. Levin 
> > > > > > > > > > >  wrote:
> > > > > > > > > > > > On Mon, Dec 10, 2018 at 09:45:42AM +0100, Geert 
> > > > > > > > > > > > Uytterhoeven wrote:
> > > > > > > > > > > > > On Mon, Dec 10, 2018 at 5:30 AM Dmitry V. Levin 
> > > > > > > > > > > > >  wrote:
> > > > > > > > > > > > > > syscall_get_* functions are required to be 
> > > > > > > > > > > > > > implemented on all
> > > > > > > > > > > > > > architectures in order to extend the generic ptrace 
> > > > > > > > > > > > > > API with
> > > > > > > > > > > > > > PTRACE_GET_SYSCALL_INFO request.
> > > > > > > > > > > > > >
> > > > > > > > > > > > > > This introduces asm/syscall.h on m68k implementing 
> > > > > > > > > > > > > > all 5 syscall_get_*
> > > > > > > > > > > > > > functions as documented in asm-generic/syscall.h: 
> > > > > > > > > > > > > > syscall_get_nr,
> > > > > > > > > > > > > > syscall_get_arguments, syscall_get_error, 
> > > > > > > > > > > > > > syscall_get_return_value,
> > > > > > > > > > > > > > and syscall_get_arch.
> > > > > > > > > > > > > >
> > > > > > > > > > > > > > Cc: Geert Uytterhoeven 
> > > > > > > > > > > > > > Cc: Oleg Nesterov 
> > > > > > > > > > > > > > Cc: Andy Lutomirski 
> > > > > > > > > > > > > > Cc: Elvira Khabirova 
> > > > > > > > > > > > > > Cc: Eugene Syromyatnikov 
> > > > > > > > > > > > > > Cc: linux-m...@lists.linux-m68k.org
> > > > > > > > > > > > > > Signed-off-by: Dmitry V. Levin 
> > > > > > > > > > > > > > ---
> > > > > > > > > > > > > >
> > > > > > > > > > > > > > Notes:
> > > > > > > > > > > > > > v5: added syscall_get_nr, 
> > > > > > > > > > > > > > syscall_get_arguments, syscall_get_error,
> > > > > > > > > > > > > > and syscall_get_return_value
> > > > > > > > > > > > > > v1: added syscall_get_arch
> > > > > > > > > > > > >
> > > > > > > > > > > > > > --- /dev/null
> > > > > > > > > > > > > > +++ b/arch/m68k/include/asm/syscall.h
> > > > > > > > > > > > > > @@ -0,0 +1,39 @@
> > > > > > > > > > > > >
> > > > > > > > > > > > > > +static inline void
> > > > > > > > > > > > > &g

Re: [PATCH 1/8] perf: Allow to block process in syscall tracepoints

2018-12-12 Thread Dmitry V. Levin
Hi Peter,

On Mon, Dec 10, 2018 at 11:18:18AM +0100, Peter Zijlstra wrote:
> On Sat, Dec 08, 2018 at 12:38:05PM -0500, Steven Rostedt wrote:
> > On Sat, 8 Dec 2018 11:44:23 +0100, Peter Zijlstra wrote:
> 
> > > > Why do we care about lost events? Because strace records *all* events,
> > > > as that's what it does and that's what it always has done. It would be
> > > > a break in functionality (a regression) if it were to start losing
> > > > events. I use strace to see everything that an application is doing.  
> > > 
> > > So make a new tool; break the expectation of all events. See if there's
> > > anybody that really cares.
> > 
> > Basically you are saying, break strace and see if anyone notices?
> 
> Nah, give it a new name. Clearly mark this is a new tool.
> 
> > > > When we discussed this at plumbers, Oracle people came to me and said
> > > > how awesome it would be to run strace against their database accesses.
> > > > The problem today is that strace causes such a large overhead that it
> > > > isn't feasible to trace any high speed applications, especially if
> > > > there are time restraints involved.  
> > > 
> > > So have them run that perf thing acme pointed to.
> > > 
> > > So far nobody's made a good argument for why we cannot have LOST events.
> > 
> > If you don't see the use case, I'm not sure anyone can convince you.
> > Again, I like the fact that when I do a strace of an application I know
> > that all system calls that the application I'm tracing is recorded. I
> > don't need to worry about what happened in the "lost events" space.
> 
> You're the one pushing for this crap without _any_ justification. Why
> are you getting upset if I ask for some?
> 
> If people care so much, it shouldn't be hard to write up a coherent
> story on this, so far all I seem to get is: because it's always been
> like that.
> 
> Which really isn't much of an argument.

As you rightly pointed out, strace users are expecting that no events are
lost because it's always been like that, and it would require some efforts
to imagine what kind of things are going to break if this is no longer the
case.

Last FOSDEM I attended a talk [1] by Philippe Ombredanne, he was speaking
about a strace-based tool called TraceCode that constructs build graphs
that are used to find out "exactly which files were built, by what and how
they were transformed in multiple steps from sources to your final binaries".

Imagine you told Philippe that strace now works faster but it's no longer
reliable because some events may be lost, and he would have to repeat
builds under strace again and again until he is lucky.  I can imagine his
reaction to this piece of news, and I certainly wouldn't like to be the
messenger.

btw, I didn't ask for the implementation to be ugly.
You don't have to introduce polling into the kernel if you don't want to,
userspace is perfectly capable of invoking wait4(2) in a loop.
Just block the tracee, notify the tracer, and let it pick up the pieces.

[1] 
https://archive.fosdem.org/2018/schedule/event/debugging_tools_stracing_build/


-- 
ldv


signature.asc
Description: PGP signature


Re: [PATCH 1/8] perf: Allow to block process in syscall tracepoints

2018-12-12 Thread Dmitry V. Levin
On Wed, Dec 12, 2018 at 08:26:39PM -0500, Steven Rostedt wrote:
> On Thu, 13 Dec 2018 03:39:38 +0300, wrote:
> 
> > btw, I didn't ask for the implementation to be ugly.
> > You don't have to introduce polling into the kernel if you don't want to,
> > userspace is perfectly capable of invoking wait4(2) in a loop.
> > Just block the tracee, notify the tracer, and let it pick up the pieces.
> 
> Note, there's been some discussion offlist to only have perf set a flag
> when it dropped an event and have the ptrace code do the heavy lifting
> of blocking the task and waking it back up. I think that would be a
> cleaner solution and wont muck with perf as badly.

Yes, if perf could be instructed to invoke something like
tracehook_report_syscall_entry/exit when it drops the event
of entering/exiting syscall, that should probably be enough
for the ptracer to do the recovery.


-- 
ldv


signature.asc
Description: PGP signature


  1   2   3   4   5   >