Skip to contentSkip to conversation

linux-user: fix readlinkat handling with magic exe symlink

PATCH Applied Jameson Nash 2 participants 5 messages Aug 9, 2022 – Sep 23, 2022

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 linux-user: fix readlinkat handling with magic exe symlink

Exactly the same as f17f4989fa193fa8279474c5462289a3cfe69aea before was for readlink. I suppose this was simply missed at the time.

---
 linux-user/syscall.c | 15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)
linux-user/syscall.c +13 -2
index ef53feb5ab..6ef4e42b21 100644
@@ -9894,11 +9894,22 @@ static abi_long do_syscall1(CPUArchState *cpu_env, int num, abi_long arg1,
             p2 = lock_user(VERIFY_WRITE, arg3, arg4, 0);
             if (!p || !p2) {
                 ret = -TARGET_EFAULT;
+            } else if (!arg4) {
+                /* Short circuit this for the magic exe check. */
+                ret = -TARGET_EINVAL;
             } else if (is_proc_myself((const char *)p, "exe")) {
                 char real[PATH_MAX], *temp;
                 temp = realpath(exec_path, real);
-                ret = temp == NULL ? get_errno(-1) : strlen(real) ;
-                snprintf((char *)p2, arg4, "%s", real);
+                /* Return value is # of bytes that we wrote to the buffer. */
+                if (temp == NULL) {
+                    ret = get_errno(-1);
+                } else {
+                    /* Don't worry about sign mismatch as earlier mapping
+                     * logic would have thrown a bad address error. */
+                    ret = MIN(strlen(real), arg4);
+                    /* We cannot NUL terminate the string. */
+                    memcpy(p2, real, ret);
+                }
             } else {
                 ret = get_errno(readlinkat(arg1, path(p), p2, arg4));
             }
Signature · 1 line
2.25.1
#1 lore raw

bump? This helps fix one of the libuv tests when run under qemu
https://github.com/libuv/libuv/pull/2941#issuecomment-1207145306

Quoted 43 lines · Jameson Nash

Exactly the same as f17f4989fa193fa8279474c5462289a3cfe69aea before was for readlink. I suppose this was simply missed at the time.

---
 linux-user/syscall.c | 15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)
linux-user/syscall.c +4 -2
index ef53feb5ab..6ef4e42b21 100644
@@ -9894,11 +9894,22 @@ static abi_long do_syscall1(CPUArchState *cpu_env,
 int num, abi_long arg1,
             p2 = lock_user(VERIFY_WRITE, arg3, arg4, 0);
             if (!p || !p2) {
                 ret = -TARGET_EFAULT;
+            } else if (!arg4) {
+                /* Short circuit this for the magic exe check. */
+                ret = -TARGET_EINVAL;
             } else if (is_proc_myself((const char *)p, "exe")) {
                 char real[PATH_MAX], *temp;
                 temp = realpath(exec_path, real);
-                ret = temp == NULL ? get_errno(-1) : strlen(real) ;
-                snprintf((char *)p2, arg4, "%s", real);
+                /* Return value is # of bytes that we wrote to the

buffer. */

linux-user/syscall.c +9 -1
+                if (temp == NULL) {
+                    ret = get_errno(-1);
+                } else {
+                    /* Don't worry about sign mismatch as earlier mapping
+                     * logic would have thrown a bad address error. */
+                    ret = MIN(strlen(real), arg4);
+                    /* We cannot NUL terminate the string. */
+                    memcpy(p2, real, ret);
+                }
             } else {
                 ret = get_errno(readlinkat(arg1, path(p), p2, arg4));
             }
--

2.25.1

#2 lore raw

bump?? This is just copying the existing f17f4989fa193fa8279474c5462289a3cfe69aea commit to cover all code paths, so I was hoping it would be trivial to get an ack for this. Thanks!

Quoted 48 lines · Jameson Nash

bump? This helps fix one of the libuv tests when run under qemu
https://github.com/libuv/libuv/pull/2941#issuecomment-1207145306

Quoted 43 lines · Jameson Nash

Exactly the same as f17f4989fa193fa8279474c5462289a3cfe69aea before was for readlink. I suppose this was simply missed at the time.

---
 linux-user/syscall.c | 15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)
linux-user/syscall.c +4 -2
index ef53feb5ab..6ef4e42b21 100644
@@ -9894,11 +9894,22 @@ static abi_long do_syscall1(CPUArchState
 *cpu_env, int num, abi_long arg1,
             p2 = lock_user(VERIFY_WRITE, arg3, arg4, 0);
             if (!p || !p2) {
                 ret = -TARGET_EFAULT;
+            } else if (!arg4) {
+                /* Short circuit this for the magic exe check. */
+                ret = -TARGET_EINVAL;
             } else if (is_proc_myself((const char *)p, "exe")) {
                 char real[PATH_MAX], *temp;
                 temp = realpath(exec_path, real);
-                ret = temp == NULL ? get_errno(-1) : strlen(real) ;
-                snprintf((char *)p2, arg4, "%s", real);
+                /* Return value is # of bytes that we wrote to the

buffer. */

linux-user/syscall.c +9 -1
+                if (temp == NULL) {
+                    ret = get_errno(-1);
+                } else {
+                    /* Don't worry about sign mismatch as earlier mapping
+                     * logic would have thrown a bad address error. */
+                    ret = MIN(strlen(real), arg4);
+                    /* We cannot NUL terminate the string. */
+                    memcpy(p2, real, ret);
+                }
             } else {
                 ret = get_errno(readlinkat(arg1, path(p), p2, arg4));
             }
--

2.25.1

Reviewed-by on v1 1/1
#3 lore raw
In reply to Jameson Nash

Le 08/08/2022 à 21:07, Jameson Nash a écrit :

Quoted 27 lines

Exactly the same as f17f4989fa193fa8279474c5462289a3cfe69aea before was for readlink. I suppose this was simply missed at the time.

---
  linux-user/syscall.c | 15 +++++++++++++--
  1 file changed, 13 insertions(+), 2 deletions(-)
linux-user/syscall.c +6 -2
index ef53feb5ab..6ef4e42b21 100644
@@ -9894,11 +9894,22 @@ static abi_long do_syscall1(CPUArchState *cpu_env, int num, abi_long arg1,
              p2 = lock_user(VERIFY_WRITE, arg3, arg4, 0);
              if (!p || !p2) {
                  ret = -TARGET_EFAULT;
+            } else if (!arg4) {
+                /* Short circuit this for the magic exe check. */
+                ret = -TARGET_EINVAL;
              } else if (is_proc_myself((const char *)p, "exe")) {
                  char real[PATH_MAX], *temp;
                  temp = realpath(exec_path, real);
-                ret = temp == NULL ? get_errno(-1) : strlen(real) ;
-                snprintf((char *)p2, arg4, "%s", real);
+                /* Return value is # of bytes that we wrote to the buffer. */
+                if (temp == NULL) {
+                    ret = get_errno(-1);

linux-user/syscall.c static abi_long do_syscall1(CPUArchState *cpu_env, int num, abi_long arg1, · line 9915

Quoted 10 lines
linux-user/syscall.c +7 -0
+                } else {
+                    /* Don't worry about sign mismatch as earlier mapping
+                     * logic would have thrown a bad address error. */
+                    ret = MIN(strlen(real), arg4);
+                    /* We cannot NUL terminate the string. */
+                    memcpy(p2, real, ret);
+                }
              } else {
                  ret = get_errno(readlinkat(arg1, path(p), p2, arg4));
              }
#4 lore raw
In reply to Jameson Nash

Le 08/08/2022 à 21:07, Jameson Nash a écrit :

Quoted 27 lines

Exactly the same as f17f4989fa193fa8279474c5462289a3cfe69aea before was for readlink. I suppose this was simply missed at the time.

---
  linux-user/syscall.c | 15 +++++++++++++--
  1 file changed, 13 insertions(+), 2 deletions(-)
linux-user/syscall.c +6 -2
index ef53feb5ab..6ef4e42b21 100644
@@ -9894,11 +9894,22 @@ static abi_long do_syscall1(CPUArchState *cpu_env, int num, abi_long arg1,
              p2 = lock_user(VERIFY_WRITE, arg3, arg4, 0);
              if (!p || !p2) {
                  ret = -TARGET_EFAULT;
+            } else if (!arg4) {
+                /* Short circuit this for the magic exe check. */
+                ret = -TARGET_EINVAL;
              } else if (is_proc_myself((const char *)p, "exe")) {
                  char real[PATH_MAX], *temp;
                  temp = realpath(exec_path, real);
-                ret = temp == NULL ? get_errno(-1) : strlen(real) ;
-                snprintf((char *)p2, arg4, "%s", real);
+                /* Return value is # of bytes that we wrote to the buffer. */
+                if (temp == NULL) {
+                    ret = get_errno(-1);

linux-user/syscall.c static abi_long do_syscall1(CPUArchState *cpu_env, int num, abi_long arg1, · line 9915

Quoted 10 lines
linux-user/syscall.c +7 -0
+                } else {
+                    /* Don't worry about sign mismatch as earlier mapping
+                     * logic would have thrown a bad address error. */
+                    ret = MIN(strlen(real), arg4);
+                    /* We cannot NUL terminate the string. */
+                    memcpy(p2, real, ret);
+                }
              } else {
                  ret = get_errno(readlinkat(arg1, path(p), p2, arg4));
              }

Applied to my linux-user-for-7.2 branch.

Thanks,
Laurent