summaryrefslogtreecommitdiff
blob: df580563458c73033de26f6c7bfde70bd6720e76 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
commit 520fa3601c36dd0a3c84e310bd2a1189259000bd
Author: Cédric VINCENT <cedric.vincent@st.com>
Date:   Thu Aug 7 14:29:37 2014 +0200

    Don't dereference argv[0] when launching a script through a symlink.
    
    Reported-by: Joakim Tjernlund <Joakim.Tjernlund@transmode.se>
    Ref: https://bugs.gentoo.org/show_bug.cgi?id=517496
    
    Also, don't complain about non-regular or non-executable files that
    are not explicitely candidates.

diff --git a/src/path/path.c b/src/path/path.c
index 4225876..ecdef70 100644
--- a/src/path/path.c
+++ b/src/path/path.c
@@ -219,17 +219,21 @@ int which(Tracee *tracee, const char *paths, char host_path[PATH_MAX], char *con
 	/* Is the command available without any $PATH look-up?  */
 	status = realpath2(tracee, host_path, command, true);
 	if (status == 0 && stat(host_path, &statr) == 0) {
-		if (!S_ISREG(statr.st_mode)) {
+		if (is_explicit && !S_ISREG(statr.st_mode)) {
 			notice(tracee, ERROR, USER, "'%s' is not a regular file", command);
 			return -EACCES;
 		}
 
-		if ((statr.st_mode & S_IXUSR) == 0) {
+		if (is_explicit && (statr.st_mode & S_IXUSR) == 0) {
 			notice(tracee, ERROR, USER, "'%s' is not executable", command);
 			return -EACCES;
 		}
 
 		found = true;
+
+		/* Don't dereference the final component to preserve
+		 * argv0 in case it is a symlink to script.  */
+		(void) realpath2(tracee, host_path, command, false);
 	}
 	else
 		found = false;
@@ -274,8 +278,12 @@ int which(Tracee *tracee, const char *paths, char host_path[PATH_MAX], char *con
 		if (status == 0
 		    && stat(host_path, &statr) == 0
 		    && S_ISREG(statr.st_mode)
-		    && (statr.st_mode & S_IXUSR) != 0)
-				return 0;
+		    && (statr.st_mode & S_IXUSR) != 0) {
+			/* Don't dereference the final component to preserve
+			 * argv0 in case it is a symlink to script.  */
+			(void) realpath2(tracee, host_path, path, false);
+			return 0;
+		}
 	} while (*(cursor - 1) != '\0');
 
 not_found:
diff --git a/src/tracee/event.c b/src/tracee/event.c
index 70668d6..5905c43 100644
--- a/src/tracee/event.c
+++ b/src/tracee/event.c
@@ -92,7 +92,7 @@ int launch_process(Tracee *tracee)
 		 * guest rootfs.  Note: Valgrind can't handle execve(2) on
 		 * "foreign" binaries (ENOEXEC) but can handle execvp(3) on such
 		 * binaries.  */
-		execvp(tracee->exe, tracee->cmdline);
+		execv(tracee->exe, tracee->cmdline);
 		return -errno;
 
 	default: /* parent */
diff --git a/tests/test-713b6910.sh b/tests/test-713b6910.sh
new file mode 100644
index 0000000..82e01fd
--- /dev/null
+++ b/tests/test-713b6910.sh
@@ -0,0 +1,51 @@
+if [ -z `which mcookie` ] || [ -z `which rm` ] || [ -z `which cat` ] || [ -z `which chmod` ] || [ -z `which ln` ] || [ -z `which grep` ] || [ -z `which mkdir` ] || [ ! -x  ${ROOTFS}/bin/readlink ]; then
+    exit 125;
+fi
+
+######################################################################
+
+TMP1=/tmp/$(mcookie)
+TMP2=/tmp/$(mcookie)
+TMP3=/tmp/$(mcookie)
+TMP4=/tmp/$(mcookie)
+
+rm -fr ${TMP1} ${TMP2} ${TMP3} ${TMP4}
+
+######################################################################
+
+cat > ${TMP1} <<'EOF'
+#!/bin/sh
+echo $0
+EOF
+
+chmod +x ${TMP1}
+ln -s ${TMP1} ${TMP2}
+
+${PROOT} ${TMP2} | grep -v ${TMP1}
+${PROOT} ${TMP2} | grep ${TMP2}
+
+######################################################################
+
+mkdir -p ${TMP3}
+cd ${TMP3}
+
+ln -s $(which true) false
+! ${PROOT} false
+
+echo "#!$(which false)" > true
+chmod a-x true
+${PROOT} true
+
+######################################################################
+
+ln -s ${ROOTFS}/bin/readlink ${TMP4}
+
+TEST1=$(${PROOT} ${ROOTFS}/bin/readlink /proc/self/exe)
+TEST2=$(${PROOT} ${TMP4} /proc/self/exe)
+
+test "${TEST1}" = "${TEST2}"
+
+######################################################################
+
+cd /
+rm -fr ${TMP1} ${TMP2} ${TMP3} ${TMP4}