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
hidden_def(getpeercon_raw)46 hidden_def(getpeercon_raw)
47
48 int getpeercon(int fd, char ** context)
49 {
50 int ret;
51 char * rcontext;
52
53 ret = getpeercon_raw(fd, &rcontext);
54
55 if (!ret) {
56 ret = selinux_raw_to_trans_context(rcontext, context);
57 freecon(rcontext);
58 }
59
60 return ret;
61 }
62