-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathspawn-ssh-suid.c
69 lines (59 loc) · 1.39 KB
/
spawn-ssh-suid.c
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
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/un.h>
#include <errno.h>
#include <pwd.h>
#define USER "www-data"
int main(int argc, char *argv[]) {
int fd, r;
socklen_t l;
union {
struct sockaddr sa;
struct sockaddr_un un;
struct sockaddr_in in;
struct sockaddr_in6 in6;
struct sockaddr_storage storage;
} local, remote;
struct passwd *pw;
errno = 0;
pw = getpwnam(USER);
if (errno > 0 || !pw)
goto fail;
if (pw->pw_uid != getuid())
goto fail;
if (clearenv() < 0)
goto fail;
fd = 3;
l = sizeof(local);
if (getsockname(fd, &local.sa, &l) < 0)
goto fail;
l = sizeof(remote);
if (getpeername(fd, &remote.sa, &l) < 0)
goto fail;
switch (local.sa.sa_family) {
case AF_INET:
case AF_INET6:
case AF_UNIX:
break;
default:
goto fail;
}
if (dup2(fd, STDOUT_FILENO) < 0)
goto fail;
if (dup2(fd, STDIN_FILENO) < 0)
goto fail;
if (dup2(fd, STDERR_FILENO) < 0)
goto fail;
r = close(fd);
if (r < 0 && errno != EINTR)
goto fail;
setuid(0);
execve("/usr/sbin/sshd", (char *[]){"sshd", "-i", NULL}, (char *[]){NULL});
fail:
return EXIT_FAILURE;
}