RPC.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <sys/un.h>
  4. #include <pthread.h>
  5. #include <errno.h>
  6. #include <sys/syscall.h>
  7. #include <fcntl.h>
  8. #include <dlfcn.h>
  9. #include <stdint.h>
  10. #include <sys/socket.h>
  11. #include <strings.h>
  12. #include "RPC.h"
  13. #define SERVICE_CONNECT_ATTEMPTS 30
  14. #define CONNECT_SIG int __fd, const struct sockaddr * __addr, socklen_t __len
  15. #define SOCKET_SIG int socket_family, int socket_type, int protocol
  16. static int (*realconnect)(CONNECT_SIG) = 0;
  17. static int (*realsocket)(SOCKET_SIG) = 0;
  18. #ifdef NETCON_INTERCEPT
  19. static int rpc_count;
  20. #endif
  21. static pthread_mutex_t lock;
  22. void rpc_mutex_init() {
  23. if(pthread_mutex_init(&lock, NULL) != 0) {
  24. fprintf(stderr, "error while initializing service call mutex\n");
  25. }
  26. }
  27. void rpc_mutex_destroy() {
  28. pthread_mutex_destroy(&lock);
  29. }
  30. /*
  31. * Reads a new file descriptor from the service
  32. */
  33. int get_new_fd(int sock)
  34. {
  35. char buf[BUF_SZ];
  36. int newfd;
  37. ssize_t size = sock_fd_read(sock, buf, sizeof(buf), &newfd);
  38. if(size > 0)
  39. return newfd;
  40. return -1;
  41. }
  42. /*
  43. * Reads a return value from the service and sets errno (if applicable)
  44. */
  45. int get_retval(int rpc_sock)
  46. {
  47. if(rpc_sock >= 0) {
  48. int retval;
  49. int sz = sizeof(char) + sizeof(retval) + sizeof(errno);
  50. char retbuf[BUF_SZ];
  51. memset(&retbuf, 0, sz);
  52. int n_read = read(rpc_sock, &retbuf, sz);
  53. if(n_read > 0) {
  54. memcpy(&retval, &retbuf[1], sizeof(retval));
  55. memcpy(&errno, &retbuf[1+sizeof(retval)], sizeof(errno));
  56. return retval;
  57. }
  58. }
  59. return -1;
  60. }
  61. int load_symbols_rpc()
  62. {
  63. #ifdef NETCON_INTERCEPT
  64. realsocket = dlsym(RTLD_NEXT, "socket");
  65. realconnect = dlsym(RTLD_NEXT, "connect");
  66. if(!realconnect || !realsocket)
  67. return -1;
  68. #endif
  69. return 1;
  70. }
  71. int rpc_join(const char * sockname)
  72. {
  73. if(!load_symbols_rpc())
  74. return -1;
  75. struct sockaddr_un addr;
  76. int conn_err = -1, attempts = 0;
  77. memset(&addr, 0, sizeof(addr));
  78. addr.sun_family = AF_UNIX;
  79. strncpy(addr.sun_path, sockname, sizeof(addr.sun_path)-1);
  80. int sock;
  81. if((sock = realsocket(AF_UNIX, SOCK_STREAM, 0)) < 0){
  82. fprintf(stderr, "Error while creating RPC socket\n");
  83. return -1;
  84. }
  85. while((conn_err != 0) && (attempts < SERVICE_CONNECT_ATTEMPTS)){
  86. if((conn_err = realconnect(sock, (struct sockaddr*)&addr, sizeof(addr))) != 0) {
  87. fprintf(stderr, "Error while connecting to RPC socket. Re-attempting...\n");
  88. sleep(1);
  89. }
  90. else
  91. return sock;
  92. attempts++;
  93. }
  94. return -1;
  95. }
  96. /*
  97. * Send a command to the service
  98. */
  99. int rpc_send_command(char *path, int cmd, int forfd, void *data, int len)
  100. {
  101. pthread_mutex_lock(&lock);
  102. char c, padding[] = {PADDING};
  103. char cmdbuf[BUF_SZ], CANARY[CANARY_SZ+PADDING_SZ], metabuf[BUF_SZ];
  104. memcpy(CANARY+CANARY_SZ, padding, sizeof(padding));
  105. uint64_t canary_num;
  106. // ephemeral RPC socket used only for this command
  107. int rpc_sock = rpc_join(path);
  108. // Generate token
  109. int fdrand = open("/dev/urandom", O_RDONLY);
  110. if(read(fdrand, &CANARY, CANARY_SZ) < 0) {
  111. fprintf(stderr,"unable to read from /dev/urandom for RPC canary data\n");
  112. return -1;
  113. }
  114. memcpy(&canary_num, CANARY, CANARY_SZ);
  115. cmdbuf[CMD_ID_IDX] = cmd;
  116. memcpy(&cmdbuf[CANARY_IDX], &canary_num, CANARY_SZ);
  117. memcpy(&cmdbuf[STRUCT_IDX], data, len);
  118. #ifdef VERBOSE
  119. memset(metabuf, 0, BUF_SZ);
  120. pid_t pid = syscall(SYS_getpid);
  121. pid_t tid = syscall(SYS_gettid);
  122. rpc_count++;
  123. char timestring[20];
  124. time_t timestamp;
  125. timestamp = time(NULL);
  126. strftime(timestring, sizeof(timestring), "%H:%M:%S", localtime(&timestamp));
  127. memcpy(metabuf, RPC_PHRASE, RPC_PHRASE_SZ); // Write signal phrase
  128. memcpy(&metabuf[IDX_PID], &pid, sizeof(pid_t) ); /* pid */
  129. memcpy(&metabuf[IDX_TID], &tid, sizeof(pid_t) ); /* tid */
  130. memcpy(&metabuf[IDX_COUNT], &rpc_count, sizeof(rpc_count) ); /* rpc_count */
  131. memcpy(&metabuf[IDX_TIME], &timestring, 20 ); /* timestamp */
  132. #endif
  133. /* Combine command flag+payload with RPC metadata */
  134. memcpy(&metabuf[IDX_PAYLOAD], cmdbuf, len + 1 + CANARY_SZ);
  135. // Write RPC
  136. int n_write = write(rpc_sock, &metabuf, BUF_SZ);
  137. if(n_write < 0) {
  138. fprintf(stderr, "Error writing command to service (CMD = %d)\n", cmdbuf[CMD_ID_IDX]);
  139. errno = 0;
  140. }
  141. // Write token to corresponding data stream
  142. if(read(rpc_sock, &c, 1) < 0) {
  143. fprintf(stderr, "unable to read RPC ACK byte from service.\n");
  144. return -1;
  145. }
  146. if(c == 'z' && n_write > 0 && forfd > -1){
  147. if(send(forfd, &CANARY, CANARY_SZ+PADDING_SZ, 0) < 0) {
  148. fprintf(stderr,"unable to write canary to stream\n");
  149. return -1;
  150. }
  151. }
  152. // Process response from service
  153. int ret = ERR_OK;
  154. if(n_write > 0) {
  155. if(cmdbuf[CMD_ID_IDX]==RPC_SOCKET) {
  156. pthread_mutex_unlock(&lock);
  157. return rpc_sock; // Used as new socket
  158. }
  159. if(cmdbuf[CMD_ID_IDX]==RPC_CONNECT
  160. || cmdbuf[CMD_ID_IDX]==RPC_BIND
  161. || cmdbuf[CMD_ID_IDX]==RPC_LISTEN) {
  162. ret = get_retval(rpc_sock);
  163. }
  164. if(cmdbuf[CMD_ID_IDX]==RPC_GETSOCKNAME) {
  165. pthread_mutex_unlock(&lock);
  166. return rpc_sock; // Don't close rpc here, we'll use it to read getsockopt_st
  167. }
  168. }
  169. else
  170. ret = -1;
  171. close(rpc_sock); // We're done with this RPC socket, close it (if type-R)
  172. pthread_mutex_unlock(&lock);
  173. return ret;
  174. }
  175. /*
  176. * Send file descriptor
  177. */
  178. ssize_t sock_fd_write(int sock, int fd)
  179. {
  180. ssize_t size;
  181. struct msghdr msg;
  182. struct iovec iov;
  183. char buf = '\0';
  184. int buflen = 1;
  185. union {
  186. struct cmsghdr cmsghdr;
  187. char control[CMSG_SPACE(sizeof (int))];
  188. } cmsgu;
  189. struct cmsghdr *cmsg;
  190. iov.iov_base = &buf;
  191. iov.iov_len = buflen;
  192. msg.msg_name = NULL;
  193. msg.msg_namelen = 0;
  194. msg.msg_iov = &iov;
  195. msg.msg_iovlen = 1;
  196. if (fd != -1) {
  197. msg.msg_control = cmsgu.control;
  198. msg.msg_controllen = sizeof(cmsgu.control);
  199. cmsg = CMSG_FIRSTHDR(&msg);
  200. cmsg->cmsg_len = CMSG_LEN(sizeof (int));
  201. cmsg->cmsg_level = SOL_SOCKET;
  202. cmsg->cmsg_type = SCM_RIGHTS;
  203. *((int *) CMSG_DATA(cmsg)) = fd;
  204. } else {
  205. msg.msg_control = NULL;
  206. msg.msg_controllen = 0;
  207. }
  208. size = sendmsg(sock, &msg, 0);
  209. if (size < 0)
  210. perror ("sendmsg");
  211. return size;
  212. }
  213. /*
  214. * Read a file descriptor
  215. */
  216. ssize_t sock_fd_read(int sock, void *buf, ssize_t bufsize, int *fd)
  217. {
  218. ssize_t size;
  219. if (fd) {
  220. struct msghdr msg;
  221. struct iovec iov;
  222. union {
  223. struct cmsghdr cmsghdr;
  224. char control[CMSG_SPACE(sizeof (int))];
  225. } cmsgu;
  226. struct cmsghdr *cmsg;
  227. iov.iov_base = buf;
  228. iov.iov_len = bufsize;
  229. msg.msg_name = NULL;
  230. msg.msg_namelen = 0;
  231. msg.msg_iov = &iov;
  232. msg.msg_iovlen = 1;
  233. msg.msg_control = cmsgu.control;
  234. msg.msg_controllen = sizeof(cmsgu.control);
  235. size = recvmsg (sock, &msg, 0);
  236. if (size < 0)
  237. return -1;
  238. cmsg = CMSG_FIRSTHDR(&msg);
  239. if (cmsg && cmsg->cmsg_len == CMSG_LEN(sizeof(int))) {
  240. if (cmsg->cmsg_level != SOL_SOCKET) {
  241. fprintf (stderr, "invalid cmsg_level %d\n",cmsg->cmsg_level);
  242. return -1;
  243. }
  244. if (cmsg->cmsg_type != SCM_RIGHTS) {
  245. fprintf (stderr, "invalid cmsg_type %d\n",cmsg->cmsg_type);
  246. return -1;
  247. }
  248. *fd = *((int *) CMSG_DATA(cmsg));
  249. } else *fd = -1;
  250. } else {
  251. size = read (sock, buf, bufsize);
  252. if (size < 0) {
  253. fprintf(stderr, "sock_fd_read(): read: Error\n");
  254. return -1;
  255. }
  256. }
  257. return size;
  258. }