1 #include <unistd.h>
2 #include <fcntl.h>
3 #include <string.h>
4 #include <stdlib.h>
5 #include <errno.h>
6 #include <sys/socket.h>
7 #include "selinux_internal.h"
8 #include "policy.h"
9
10 #ifndef SO_PEERSEC
11 #define SO_PEERSEC 31
12 #endif
13
getpeercon_raw(int fd,char ** context)14 int getpeercon_raw(int fd, char ** context)
15 {
16 char *buf;
17 socklen_t size;
18 ssize_t ret;
19
20 size = INITCONTEXTLEN + 1;
21 buf = malloc(size);
22 if (!buf)
23 return -1;
24 memset(buf, 0, size);
25
26 ret = getsockopt(fd, SOL_SOCKET, SO_PEERSEC, buf, &size);
27 if (ret < 0 && errno == ERANGE) {
28 char *newbuf;
29
30 newbuf = realloc(buf, size);
31 if (!newbuf)
32 goto out;
33
34 buf = newbuf;
35 memset(buf, 0, size);
36 ret = getsockopt(fd, SOL_SOCKET, SO_PEERSEC, buf, &size);
37 }
38 out:
39 if (ret < 0)
40 free(buf);
41 else
42 *context = buf;
43 return ret;
44 }
45
46
getpeercon(int fd,char ** context)47 int getpeercon(int fd, char ** context)
48 {
49 int ret;
50 char * rcontext;
51
52 ret = getpeercon_raw(fd, &rcontext);
53
54 if (!ret) {
55 ret = selinux_raw_to_trans_context(rcontext, context);
56 freecon(rcontext);
57 }
58
59 return ret;
60 }
61