Skip to contentSkip to conversation

linux-user: identify running binary in /proc/self/exe

PATCH Applied Riku Voipio 3 participants 5 messages Jan 19, 2009 – Jan 30, 2009

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 linux-user: identify running binary in /proc/self/exe

Some applications like to test /proc/self/exe to find out who they are. Fake the result of readlink() for them.

---
 linux-user/main.c    |    3 +++
 linux-user/qemu.h    |    1 +
 linux-user/syscall.c |    9 +++++++--
 3 files changed, 11 insertions(+), 2 deletions(-)
linux-user/main.c +3 -0
index 3019f33..2ffe244 100644
@@ -34,6 +34,8 @@
 
 #define DEBUG_LOGFILE "/tmp/qemu.log"
 
+char *exec_path;
+
 static const char *interp_prefix = CONFIG_QEMU_PREFIX;
 const char *qemu_uname_release = CONFIG_UNAME_RELEASE;
 
@@ -2314,6 +2316,7 @@ int main(int argc, char **argv, char **envp)
     if (optind >= argc)
         usage();
     filename = argv[optind];
+    exec_path = argv[optind];
 
     /* Zero out regs */
     memset(regs, 0, sizeof(struct target_pt_regs));
linux-user/qemu.h +1 -0
index 9fddd05..4137567 100644
@@ -120,6 +120,7 @@ typedef struct TaskState {
     uint8_t stack[0];
 } __attribute__((aligned(16))) TaskState;
 
+extern char *exec_path;
 void init_task_state(TaskState *ts);
 extern const char *qemu_uname_release;
 
linux-user/syscall.c +7 -2
index adb27de..53167e9 100644
@@ -4720,8 +4720,13 @@ abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
             p2 = lock_user(VERIFY_WRITE, arg2, arg3, 0);
             if (!p || !p2)
                 ret = -TARGET_EFAULT;
-            else
-                ret = get_errno(readlink(path(p), p2, arg3));
+            else {
+                if (strncmp((const char *)p, "/proc/self/exe", 14) == 0)
+                    ret = get_errno(snprintf((char *)p2, arg3, "%s", exec_path));
+                else
+                    ret = get_errno(readlink(path(p), p2, arg3));
+                break;
+            }
             unlock_user(p2, arg2, ret);
             unlock_user(p, arg1, 0);
         }
Signature · 5 lines
1.5.6.5


-- 
"rm -rf" only sounds scary if you don't have backups
#1 lore raw
Quoted 5 lines · Riku Voipio

Some applications like to test /proc/self/exe to find out who they are. Fake the result of readlink() for them.

+    exec_path = argv[optind];

Won't this give the wrong answer when the binary is invoked using a relative path or a symlink? On my system /proc/self/exe is always an absolute path to an actual file.

Paul

#2 lore raw
Quoted 10 lines · Paul Brook
Quoted 5 lines · Riku Voipio

Some applications like to test /proc/self/exe to find out who they are. Fake the result of readlink() for them.

+    exec_path = argv[optind];

Won't this give the wrong answer when the binary is invoked using a relative path or a symlink? On my system /proc/self/exe is always an absolute path to an actual file.

You are right. Fixed version attached.

Signature · 1 line
"rm -rf" only sounds scary if you don't have backups
Quoted 1 line

From f3be7d8337ae3def8f2d3d4e089016fcd9686467 Mon Sep 17 00:00:00 2001

From: Riku Voipio <[email protected]>
Date: Tue, 20 Jan 2009 17:46:18 +0200
Subject: [PATCH] linux-user: identify running binary in /proc/self/exe

Some applications like to test /proc/self/exe to find out who they are. Fake the result of readlink() for them. Use realpath() to return full path to binary (which the links /proc/self/exe are)

---
 linux-user/main.c    |    3 +++
 linux-user/qemu.h    |    1 +
 linux-user/syscall.c |   12 ++++++++++--
 3 files changed, 14 insertions(+), 2 deletions(-)
linux-user/main.c +3 -0
index 59da5fd..b45dcfe 100644
@@ -34,6 +34,8 @@
 
 #define DEBUG_LOGFILE "/tmp/qemu.log"
 
+char *exec_path;
+
 static const char *interp_prefix = CONFIG_QEMU_PREFIX;
 const char *qemu_uname_release = CONFIG_UNAME_RELEASE;
 
@@ -2312,6 +2314,7 @@ int main(int argc, char **argv, char **envp)
     if (optind >= argc)
         usage();
     filename = argv[optind];
+    exec_path = argv[optind];
 
     /* Zero out regs */
     memset(regs, 0, sizeof(struct target_pt_regs));
linux-user/qemu.h +1 -0
index 9fddd05..4137567 100644
@@ -120,6 +120,7 @@ typedef struct TaskState {
     uint8_t stack[0];
 } __attribute__((aligned(16))) TaskState;
 
+extern char *exec_path;
 void init_task_state(TaskState *ts);
 extern const char *qemu_uname_release;
 
linux-user/syscall.c +10 -2
index 5d787bb..21ec17b 100644
@@ -4375,8 +4375,16 @@ abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
             p2 = lock_user(VERIFY_WRITE, arg2, arg3, 0);
             if (!p || !p2)
                 ret = -TARGET_EFAULT;
-            else
-                ret = get_errno(readlink(path(p), p2, arg3));
+            else {
+                if (strncmp((const char *)p, "/proc/self/exe", 14) == 0) {
+                    char real[PATH_MAX];
+                    ret = get_errno(realpath(exec_path,real));
+                    snprintf((char *)p2, arg3, "%s", real);
+                    }
+                else
+                    ret = get_errno(readlink(path(p), p2, arg3));
+                break;
+            }
             unlock_user(p2, arg2, ret);
             unlock_user(p, arg1, 0);
         }
Signature · 1 line
1.5.6.3
#3 lore raw

Some applications like to test /proc/self/exe to find out who they are. Fake the result of readlink() for them. Use realpath() to return full path to binary (which the links /proc/self/exe are)

updated version - fix ret/errno in the /proc/self/exe case.

---
 linux-user/main.c    |    3 +++
 linux-user/qemu.h    |    1 +
 linux-user/syscall.c |   15 ++++++++++++---
 3 files changed, 16 insertions(+), 3 deletions(-)
linux-user/main.c +3 -0
index cb10173..3418ca6 100644
@@ -37,6 +37,8 @@
 
 #define DEBUG_LOGFILE "/tmp/qemu.log"
 
+char *exec_path;
+
 static const char *interp_prefix = CONFIG_QEMU_PREFIX;
 const char *qemu_uname_release = CONFIG_UNAME_RELEASE;
 
@@ -2354,6 +2356,7 @@ int main(int argc, char **argv, char **envp)
     if (optind >= argc)
         usage();
     filename = argv[optind];
+    exec_path = argv[optind];
 
     /* Zero out regs */
     memset(regs, 0, sizeof(struct target_pt_regs));
linux-user/qemu.h +1 -0
index 9fddd05..4137567 100644
@@ -120,6 +120,7 @@ typedef struct TaskState {
     uint8_t stack[0];
 } __attribute__((aligned(16))) TaskState;
 
+extern char *exec_path;
 void init_task_state(TaskState *ts);
 extern const char *qemu_uname_release;
 
linux-user/syscall.c +12 -3
index 690df14..2903bf3 100644
@@ -4877,13 +4877,22 @@ abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
 #endif
     case TARGET_NR_readlink:
         {
-            void *p2;
+            void *p2, *temp;
             p = lock_user_string(arg1);
             p2 = lock_user(VERIFY_WRITE, arg2, arg3, 0);
             if (!p || !p2)
                 ret = -TARGET_EFAULT;
-            else
-                ret = get_errno(readlink(path(p), p2, arg3));
+            else {
+                if (strncmp((const char *)p, "/proc/self/exe", 14) == 0) {
+                    char real[PATH_MAX];
+                    temp = realpath(exec_path,real);
+                    ret = (temp==NULL) ? get_errno(-1) : strlen(real) ;
+                    snprintf((char *)p2, arg3, "%s", real);
+                    }
+                else
+                    ret = get_errno(readlink(path(p), p2, arg3));
+                break;
+            }
             unlock_user(p2, arg2, ret);
             unlock_user(p, arg1, 0);
         }
Signature · 1 line
1.5.6.5
#4 lore raw
Quoted 8 lines · Riku Voipio

Some applications like to test /proc/self/exe to find out who they are. Fake the result of readlink() for them. Use realpath() to return full path to binary (which the links /proc/self/exe are)

updated version - fix ret/errno in the /proc/self/exe case.

Thanks, applied.

Quoted 75 lines
---
 linux-user/main.c    |    3 +++
 linux-user/qemu.h    |    1 +
 linux-user/syscall.c |   15 ++++++++++++---
 3 files changed, 16 insertions(+), 3 deletions(-)
linux-user/main.c +3 -0
index cb10173..3418ca6 100644
@@ -37,6 +37,8 @@
 
 #define DEBUG_LOGFILE "/tmp/qemu.log"
 
+char *exec_path;
+
 static const char *interp_prefix = CONFIG_QEMU_PREFIX;
 const char *qemu_uname_release = CONFIG_UNAME_RELEASE;
 
@@ -2354,6 +2356,7 @@ int main(int argc, char **argv, char **envp)
     if (optind >= argc)
         usage();
     filename = argv[optind];
+    exec_path = argv[optind];
 
     /* Zero out regs */
     memset(regs, 0, sizeof(struct target_pt_regs));
linux-user/qemu.h +1 -0
index 9fddd05..4137567 100644
@@ -120,6 +120,7 @@ typedef struct TaskState {
     uint8_t stack[0];
 } __attribute__((aligned(16))) TaskState;
 
+extern char *exec_path;
 void init_task_state(TaskState *ts);
 extern const char *qemu_uname_release;
 
linux-user/syscall.c +12 -3
index 690df14..2903bf3 100644
@@ -4877,13 +4877,22 @@ abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
 #endif
     case TARGET_NR_readlink:
         {
-            void *p2;
+            void *p2, *temp;
             p = lock_user_string(arg1);
             p2 = lock_user(VERIFY_WRITE, arg2, arg3, 0);
             if (!p || !p2)
                 ret = -TARGET_EFAULT;
-            else
-                ret = get_errno(readlink(path(p), p2, arg3));
+            else {
+                if (strncmp((const char *)p, "/proc/self/exe", 14) == 0) {
+                    char real[PATH_MAX];
+                    temp = realpath(exec_path,real);
+                    ret = (temp==NULL) ? get_errno(-1) : strlen(real) ;
+                    snprintf((char *)p2, arg3, "%s", real);
+                    }
+                else
+                    ret = get_errno(readlink(path(p), p2, arg3));
+                break;
+            }
             unlock_user(p2, arg2, ret);
             unlock_user(p, arg1, 0);
         }
Signature · 1 line
1.5.6.5
Signature · 2 lines
Aurelien Jarno	                        GPG: 1024D/F1BCDB73
[email protected]                 http://www.aurel32.net