Skip to content

Commit 23c04c6

Browse files
committed
Fix command injection on screencap
There is a potential injection by using screencap in case of user handled parameters. "dumpstate" command launches "screencap", when "-p" is argument is set. At that moment, content of "-o" parameter generates a path with ".png" extension to define "screencap" argument. "dumpstate" is often run as a service with "root" privileged such as defined in "dumpstate.rc". For instance "bugreportz" call "ctl.start" property with "dumpstatez". Launching "dumpstate" with "-p" option and a user input as "-o" would result in a root command execution. SE Linux might protect part of this attack. Cherry-pick from ag/10651695 with fix ag/10700515 Bug: 123230379 Test: please see commands #4 and #5 Change-Id: Icd88cdf4af153e07addb4449cdb117b1a3c881d3
1 parent e6dddc9 commit 23c04c6

1 file changed

Lines changed: 33 additions & 5 deletions

File tree

cmds/screencap/screencap.cpp

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <linux/fb.h>
2525
#include <sys/ioctl.h>
2626
#include <sys/mman.h>
27+
#include <sys/wait.h>
2728

2829
#include <binder/ProcessState.h>
2930

@@ -99,11 +100,38 @@ static uint32_t dataSpaceToInt(ui::Dataspace d)
99100
}
100101

101102
static status_t notifyMediaScanner(const char* fileName) {
102-
String8 cmd("am broadcast -a android.intent.action.MEDIA_SCANNER_SCAN_FILE -d file://");
103-
cmd.append(fileName);
104-
cmd.append(" > /dev/null");
105-
int result = system(cmd.string());
106-
if (result < 0) {
103+
std::string filePath("file://");
104+
filePath.append(fileName);
105+
char *cmd[] = {
106+
(char*) "am",
107+
(char*) "broadcast",
108+
(char*) "am",
109+
(char*) "android.intent.action.MEDIA_SCANNER_SCAN_FILE",
110+
(char*) "-d",
111+
&filePath[0],
112+
nullptr
113+
};
114+
115+
int status;
116+
int pid = fork();
117+
if (pid < 0){
118+
fprintf(stderr, "Unable to fork in order to send intent for media scanner.\n");
119+
return UNKNOWN_ERROR;
120+
}
121+
if (pid == 0){
122+
int fd = open("/dev/null", O_WRONLY);
123+
if (fd < 0){
124+
fprintf(stderr, "Unable to open /dev/null for media scanner stdout redirection.\n");
125+
exit(1);
126+
}
127+
dup2(fd, 1);
128+
int result = execvp(cmd[0], cmd);
129+
close(fd);
130+
exit(result);
131+
}
132+
wait(&status);
133+
134+
if (status < 0) {
107135
fprintf(stderr, "Unable to broadcast intent for media scanner.\n");
108136
return UNKNOWN_ERROR;
109137
}

0 commit comments

Comments
 (0)