From 09eeb8c958c15232b6255d135b3161ec74a7939c Mon Sep 17 00:00:00 2001 From: Toni Uhlig Date: Fri, 11 May 2018 23:43:28 +0200 Subject: suidcmd: additional checks, error printing Signed-off-by: Toni Uhlig --- config.h | 2 +- dummyshell.c | 2 +- suidcmd.c | 35 +++++++++++++++++++++++++++++------ 3 files changed, 31 insertions(+), 8 deletions(-) diff --git a/config.h b/config.h index 5ebf43b..624f578 100644 --- a/config.h +++ b/config.h @@ -40,4 +40,4 @@ ***********/ /* suid commands (e.g.: "first-cmd", "second-cmd", "nth-cmd") */ -#define SUIDCMD_CMDS "/usr/sbin/ether-wake", "/bin/ping" +#define SUIDCMD_CMDS "/usr/sbin/etherwake", "/usr/sbin/ether-wake", "/bin/ping" diff --git a/dummyshell.c b/dummyshell.c index 115b317..db69fb1 100644 --- a/dummyshell.c +++ b/dummyshell.c @@ -123,7 +123,7 @@ static char readInput(char* buf, size_t* siz, size_t szMax, char key, int flags) default: if (isprint(key) && *siz < szMax) buf[(*siz)++] = key; - break; + break; } return key; } diff --git a/suidcmd.c b/suidcmd.c index 7c2487d..04b2645 100644 --- a/suidcmd.c +++ b/suidcmd.c @@ -11,6 +11,7 @@ #include #include #include +#include #include /* memset(...), strstr(...) */ #include #include /* basename(...) */ @@ -120,30 +121,52 @@ int main(int argc, char** argv) } struct stat buf; + if (lstat(argv[0], &buf) != 0) { + perror("lstat"); + return 1; + } + if (!S_ISLNK(buf.st_mode)) { + printCmds(); + return 0; + } + + static char *real_arg0 = NULL; + real_arg0 = realpath(argv[0], NULL); + if (!real_arg0) { + perror("realpath"); + return 1; + } + if (stat(argv[0], &buf) != 0) { perror("stat"); + return 1; } if ((buf.st_mode & S_ISUID) == 0) { - fprintf(stderr, "%s: not set suid\n", argv[0]); + fprintf(stderr, "%s: not suid\n", real_arg0); return 1; } const char* runpath = getCmd(argv[0]); if (!runpath) { - fprintf(stderr, "%s not runnable cmd\n", argv[0]); + fprintf(stderr, "%s: %s not runnable cmd\n", real_arg0, argv[0]); printCmds(); return 1; } + if (stat(runpath, &buf) != 0) { + fprintf(stderr, "%s: %s error: %s\n", real_arg0, runpath, strerror(errno)); + return 1; + } + if (getresuid(&ruid, &euid, &suid) != 0) { - perror("getresuid()"); + perror("getresuid"); } else { printf("%s: RUID:%u , EUID:%u , SUID:%u\n", argv[0], ruid, euid, suid); } - if (setuid(0) != 0) { - perror("setuid(0)"); - } else printf("%s: setuid(0)\n", argv[0]); + if (setresuid(0,0,0) != 0) { + perror("setresuid"); + } char* cmd = NULL; if (asprintf(&cmd, "%s", runpath) <= 0) { -- cgit v1.2.3