@@ -57,6 +57,8 @@ static struct su_initiator su_from = {
57
57
.uid = 0 ,
58
58
.bin = "" ,
59
59
.args = "" ,
60
+ .env = "" ,
61
+ .envp = { NULL , },
60
62
};
61
63
62
64
static struct su_request su_to = {
@@ -72,6 +74,7 @@ static int from_init(struct su_initiator *from)
72
74
ssize_t len ;
73
75
int i ;
74
76
int err ;
77
+ size_t j ;
75
78
76
79
from -> uid = getuid ();
77
80
from -> pid = getppid ();
@@ -126,6 +129,30 @@ static int from_init(struct su_initiator *from)
126
129
strncpy (from -> bin , argv0 , sizeof (from -> bin ));
127
130
from -> bin [sizeof (from -> bin )- 1 ] = '\0' ;
128
131
132
+ /* Get the environment of the calling process */
133
+ snprintf (path , sizeof (path ), "/proc/%u/environ" , from -> pid );
134
+ fd = open (path , O_RDONLY );
135
+ if (fd < 0 ) {
136
+ PLOGE ("Opening environment" );
137
+ goto out ;
138
+ }
139
+ len = read (fd , from -> env , sizeof (from -> env ));
140
+ close (fd );
141
+ if (len < 0 || len == sizeof (from -> env )) {
142
+ PLOGE ("Reading environment" );
143
+ goto out ;
144
+ }
145
+ from -> env [len ] = '\0' ;
146
+
147
+ from -> envp [0 ] = & from -> env [0 ];
148
+ for (i = 0 , j = 0 ; i < len && j < ARRAY_SIZE (from -> envp ); i ++ ) {
149
+ if (from -> env [i ] == '\0' ) {
150
+ from -> envp [++ j ] = & from -> env [i + 1 ];
151
+ }
152
+ }
153
+ from -> envp [j ] = NULL ;
154
+
155
+ out :
129
156
return 0 ;
130
157
}
131
158
@@ -269,6 +296,7 @@ static void allow(char *shell, mode_t mask)
269
296
struct su_initiator * from = & su_from ;
270
297
struct su_request * to = & su_to ;
271
298
char * exe = NULL ;
299
+ char * * envp = environ ;
272
300
273
301
umask (mask );
274
302
send_intent (& su_from , & su_to , "" , 1 , 1 );
@@ -277,14 +305,17 @@ static void allow(char *shell, mode_t mask)
277
305
strcpy (shell , "/system/bin/sh" );
278
306
}
279
307
exe = strrchr (shell , '/' ) + 1 ;
308
+ if (from -> envp [0 ]) {
309
+ envp = from -> envp ;
310
+ }
280
311
setresgid (to -> uid , to -> uid , to -> uid );
281
312
setresuid (to -> uid , to -> uid , to -> uid );
282
313
LOGD ("%u %s executing %u %s using shell %s : %s" , from -> uid , from -> bin ,
283
314
to -> uid , to -> command , shell , exe );
284
315
if (strcmp (to -> command , DEFAULT_COMMAND )) {
285
- execl (shell , exe , "-c" , to -> command , (char * )NULL );
316
+ execle (shell , exe , "-c" , to -> command , (char * )NULL , envp );
286
317
} else {
287
- execl (shell , exe , "-" , (char * )NULL );
318
+ execle (shell , exe , "-" , (char * )NULL , envp );
288
319
}
289
320
PLOGE ("exec" );
290
321
exit (EXIT_SUCCESS );
0 commit comments