| 1 | /* |
| 2 | * Copyright (C) 2011 David Goulet <dgoulet@efficios.com> |
| 3 | * |
| 4 | * SPDX-License-Identifier: GPL-2.0-only |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #ifndef _COMPAT_SOCKET_H |
| 9 | #define _COMPAT_SOCKET_H |
| 10 | |
| 11 | #include <sys/socket.h> |
| 12 | #include <sys/un.h> |
| 13 | |
| 14 | #include <common/macros.h> |
| 15 | |
| 16 | #ifndef MSG_NOSIGNAL |
| 17 | # ifdef SO_NOSIGPIPE |
| 18 | # define MSG_NOSIGNAL SO_NOSIGPIPE |
| 19 | # endif |
| 20 | #endif |
| 21 | |
| 22 | #if defined(MSG_NOSIGNAL) |
| 23 | static inline |
| 24 | ssize_t lttng_recvmsg_nosigpipe(int sockfd, struct msghdr *msg) |
| 25 | { |
| 26 | return recvmsg(sockfd, msg, MSG_NOSIGNAL); |
| 27 | } |
| 28 | #else |
| 29 | |
| 30 | #include <signal.h> |
| 31 | #include <common/compat/errno.h> |
| 32 | |
| 33 | static inline |
| 34 | ssize_t lttng_recvmsg_nosigpipe(int sockfd, struct msghdr *msg) |
| 35 | { |
| 36 | ssize_t received; |
| 37 | int saved_err; |
| 38 | sigset_t sigpipe_set, pending_set, old_set; |
| 39 | int sigpipe_was_pending; |
| 40 | |
| 41 | /* |
| 42 | * Discard the SIGPIPE from send(), not disturbing any SIGPIPE |
| 43 | * that might be already pending. If a bogus SIGPIPE is sent to |
| 44 | * the entire process concurrently by a malicious user, it may |
| 45 | * be simply discarded. |
| 46 | */ |
| 47 | if (sigemptyset(&pending_set)) { |
| 48 | return -1; |
| 49 | } |
| 50 | /* |
| 51 | * sigpending returns the mask of signals that are _both_ |
| 52 | * blocked for the thread _and_ pending for either the thread or |
| 53 | * the entire process. |
| 54 | */ |
| 55 | if (sigpending(&pending_set)) { |
| 56 | return -1; |
| 57 | } |
| 58 | sigpipe_was_pending = sigismember(&pending_set, SIGPIPE); |
| 59 | /* |
| 60 | * If sigpipe was pending, it means it was already blocked, so |
| 61 | * no need to block it. |
| 62 | */ |
| 63 | if (!sigpipe_was_pending) { |
| 64 | if (sigemptyset(&sigpipe_set)) { |
| 65 | return -1; |
| 66 | } |
| 67 | if (sigaddset(&sigpipe_set, SIGPIPE)) { |
| 68 | return -1; |
| 69 | } |
| 70 | if (pthread_sigmask(SIG_BLOCK, &sigpipe_set, &old_set)) { |
| 71 | return -1; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | /* Send and save errno. */ |
| 76 | received = recvmsg(sockfd, msg, 0); |
| 77 | saved_err = errno; |
| 78 | |
| 79 | if (received == -1 && errno == EPIPE && !sigpipe_was_pending) { |
| 80 | struct timespec timeout = { 0, 0 }; |
| 81 | int ret; |
| 82 | |
| 83 | do { |
| 84 | ret = sigtimedwait(&sigpipe_set, NULL, |
| 85 | &timeout); |
| 86 | } while (ret == -1 && errno == EINTR); |
| 87 | } |
| 88 | if (!sigpipe_was_pending) { |
| 89 | if (pthread_sigmask(SIG_SETMASK, &old_set, NULL)) { |
| 90 | return -1; |
| 91 | } |
| 92 | } |
| 93 | /* Restore send() errno */ |
| 94 | errno = saved_err; |
| 95 | |
| 96 | return received; |
| 97 | } |
| 98 | #endif |
| 99 | |
| 100 | |
| 101 | #ifdef __linux__ |
| 102 | |
| 103 | #define LTTNG_SOCK_CREDS SCM_CREDENTIALS |
| 104 | |
| 105 | typedef struct ucred lttng_sock_cred; |
| 106 | |
| 107 | #define LTTNG_SOCK_SET_UID_CRED(c, u) LTTNG_REF(c)->uid = u |
| 108 | #define LTTNG_SOCK_SET_GID_CRED(c, g) LTTNG_REF(c)->gid = g |
| 109 | #define LTTNG_SOCK_SET_PID_CRED(c, p) LTTNG_REF(c)->pid = p |
| 110 | |
| 111 | #define LTTNG_SOCK_GET_UID_CRED(c) LTTNG_REF(c)->uid |
| 112 | #define LTTNG_SOCK_GET_GID_CRED(c) LTTNG_REF(c)->gid |
| 113 | #define LTTNG_SOCK_GET_PID_CRED(c) LTTNG_REF(c)->pid |
| 114 | |
| 115 | #elif (defined(__FreeBSD__) || defined(__CYGWIN__) || defined(__sun__) || defined(__APPLE__)) |
| 116 | |
| 117 | struct lttng_sock_cred { |
| 118 | uid_t uid; |
| 119 | gid_t gid; |
| 120 | }; |
| 121 | |
| 122 | typedef struct lttng_sock_cred lttng_sock_cred; |
| 123 | |
| 124 | #define LTTNG_SOCK_SET_UID_CRED(c, u) LTTNG_REF(c)->uid = u |
| 125 | #define LTTNG_SOCK_SET_GID_CRED(c, g) LTTNG_REF(c)->gid = g |
| 126 | #define LTTNG_SOCK_SET_PID_CRED(c, p) |
| 127 | |
| 128 | #define LTTNG_SOCK_GET_UID_CRED(c) LTTNG_REF(c)->uid |
| 129 | #define LTTNG_SOCK_GET_GID_CRED(c) LTTNG_REF(c)->gid |
| 130 | #define LTTNG_SOCK_GET_PID_CRED(c) -1 |
| 131 | |
| 132 | #else |
| 133 | #error "Please add support for your OS." |
| 134 | #endif /* __linux__ , __FreeBSD__ */ |
| 135 | |
| 136 | |
| 137 | #ifdef __sun__ |
| 138 | |
| 139 | # ifndef CMSG_ALIGN |
| 140 | # ifdef _CMSG_DATA_ALIGN |
| 141 | # define CMSG_ALIGN(len) _CMSG_DATA_ALIGN(len) |
| 142 | # else |
| 143 | /* aligning to sizeof (long) is assumed to be portable (fd.o#40235) */ |
| 144 | # define CMSG_ALIGN(len) (((len) + sizeof (long) - 1) & ~(sizeof (long) - 1)) |
| 145 | # endif |
| 146 | # ifndef CMSG_SPACE |
| 147 | # define CMSG_SPACE(len) (CMSG_ALIGN (sizeof (struct cmsghdr)) + CMSG_ALIGN (len)) |
| 148 | # endif |
| 149 | # ifndef CMSG_LEN |
| 150 | # define CMSG_LEN(len) (CMSG_ALIGN (sizeof (struct cmsghdr)) + (len)) |
| 151 | # endif |
| 152 | # endif |
| 153 | |
| 154 | |
| 155 | #include <ucred.h> |
| 156 | |
| 157 | static inline |
| 158 | int getpeereid(int s, uid_t *euid, gid_t *gid) |
| 159 | { |
| 160 | int ret = 0; |
| 161 | ucred_t *ucred = NULL; |
| 162 | |
| 163 | ret = getpeerucred(s, &ucred); |
| 164 | if (ret == -1) { |
| 165 | goto end; |
| 166 | } |
| 167 | |
| 168 | ret = ucred_geteuid(ucred); |
| 169 | if (ret == -1) { |
| 170 | goto free; |
| 171 | } |
| 172 | *euid = ret; |
| 173 | |
| 174 | ret = ucred_getrgid(ucred); |
| 175 | if (ret == -1) { |
| 176 | goto free; |
| 177 | } |
| 178 | *gid = ret; |
| 179 | ret = 0; |
| 180 | free: |
| 181 | ucred_free(ucred); |
| 182 | end: |
| 183 | return ret; |
| 184 | } |
| 185 | |
| 186 | #endif /* __sun__ */ |
| 187 | |
| 188 | #endif /* _COMPAT_SOCKET_H */ |