calibre-mount-helper: Validate dev arg as well as mount

This commit is contained in:
Kovid Goyal 2011-11-03 07:22:44 +05:30
parent 8b21b4a4ad
commit eb2a40ffef

View File

@ -33,6 +33,25 @@ void ensure_root() {
}
}
int check_args(const char *dev, const char *mp) {
if (dev == NULL || strlen(dev) < strlen("/dev/") || mp == NULL || strlen(mp) < strlen("/media/")) {
fprintf(stderr, "Invalid arguments\n");
return False;
}
if (strncmp("/media/", mp, 6) != 0) {
fprintf(stderr, "Trying to operate on a mount point not under /media is not allowed\n");
return False;
}
if (strncmp("/dev/", dev, 5) != 0) {
fprintf(stderr, "Trying to operate on a dev node not under /dev\n");
return False;
}
return True;
}
int do_mount(const char *dev, const char *mp) {
char options[1000], marker[2000];
#ifdef __NetBSD__
@ -45,11 +64,6 @@ int do_mount(const char *dev, const char *mp) {
return EXIT_FAILURE;
}
if (strncmp("/usr", mp, 4) == 0 || strncmp("/bin", mp, 4) == 0 || strncmp("/sbin", mp, 5) == 0) {
fprintf(stderr, "Trying to mount to a mount point under /usr, /bin, /sbin is not allowed\n");
return EXIT_FAILURE;
}
if (!exists(mp)) {
if (mkdir(mp, S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH) != 0) {
errsv = errno;
@ -226,6 +240,8 @@ int main(int argc, char** argv)
exit(EXIT_FAILURE);
}
if (!check_args(dev, mp)) exit(EXIT_FAILURE);
if (strncmp(action, "mount", 5) == 0) {
status = do_mount(dev, mp);
} else if (strncmp(action, "eject", 5) == 0) {