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(int fd,security_context_t * context)14 int getpeercon(int fd, security_context_t * 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