summaryrefslogtreecommitdiffstats
path: root/src/havegecmd.c
blob: 9ced105e61945ea72eac28f702cbf4b02a83ba47 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
/**
 ** Provide HAVEGE socket communication API
 **
 ** Copyright 2018-2021 Jirka Hladky hladky DOT jiri AT gmail DOT com
 ** Copyright 2018 Werner Fink <werner@suse.de>
 **
 ** This program is free software: you can redistribute it and/or modify
 ** it under the terms of the GNU General Public License as published by
 ** the Free Software Foundation, either version 3 of the License, or
 ** (at your option) any later version.
 **
 ** This program is distributed in the hope that it will be useful,
 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 ** GNU General Public License for more details.
 **
 ** You should have received a copy of the GNU General Public License
 ** along with this program.  If not, see <http://www.gnu.org/licenses/>.
 **
 */

#include "config.h"

#include <stddef.h>
#include <stdint.h>

#ifndef NO_COMMAND_MODE
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/un.h>
#include <unistd.h>

#ifndef HAVE_STRUCT_UCRED
struct ucred
{
   pid_t pid;                      /* PID of sending process.  */
   uid_t uid;                      /* UID of sending process.  */
   gid_t gid;                      /* GID of sending process.  */
};
#endif

#include "havegecmd.h"

int first_byte;
int socket_fd;
static char errmsg[1024];

static int new_root(               /* RETURN: status                        */
   const char *root,               /* IN: path of the new root file system  */
   const volatile char *path,      /* IN: path of the haveged executable    */
   char *const argv[],             /* IN: arguments for the haveged process */
   struct pparams *params)         /* IN: input params                      */
{
   int ret;
   struct stat st;
   const char *realtive = (const char*)&path[0];

   ret = chdir(root);
   if (ret < 0) {
      snprintf(&errmsg[0], sizeof(errmsg)-1,
               "can't change to working directory %s: %s\n",
               root, strerror(errno));
      goto err;
      }
   if (path[0] == '/')
      realtive++;
   ret = fstatat(AT_FDCWD, realtive, &st, 0);
   if (ret < 0) {
      snprintf(&errmsg[0], sizeof(errmsg)-1,
               "can't restart %s: %s\n",
               path, strerror(errno));
      goto err;
      }
   ret = chroot(".");
   if (ret < 0) {
      snprintf(&errmsg[0], sizeof(errmsg)-1,
               "can't change root directory %s\n",
               strerror(errno));
      goto err;
      }
   ret = chdir("/");
   if (ret < 0) {
      snprintf(&errmsg[0], sizeof(errmsg)-1,
               "can't change to working directory / : %s\n",
               strerror(errno));
      goto err;
      }
   ret = execv((const char *)path, argv);
   if (ret < 0) {
      snprintf(&errmsg[0], sizeof(errmsg)-1,
               "can't restart %s: %s\n",
               path, strerror(errno));
   }   
err:
   if (ret < 0)
      print_msg("%s", errmsg);
   return ret;
}

/**
 * Open and listen on a UNIX socket to get command from there
 */
int cmd_listen(                    /* RETURN: UNIX socket file descriptor */
   struct pparams *params)         /* IN: input params                    */
{
   struct sockaddr_un su = {       /* The abstract UNIX socket of haveged */
      .sun_family = AF_UNIX,
      .sun_path = HAVEGED_SOCKET_PATH,
   };
   const int one = 1;
   int fd, ret;

   fd = socket(PF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
   if (fd < 0) {
      print_msg("%s: can not open UNIX socket\n", params->daemon);
      goto err;
      }

   ret = setsockopt(fd, SOL_SOCKET, SO_PASSCRED, &one, (socklen_t)sizeof(one));
   if (ret < 0) {
      close(fd);
      fd = -1;
      print_msg("%s: can not set option for UNIX socket\n", params->daemon);
      goto err;
      }

   ret = bind(fd, (struct sockaddr *)&su, offsetof(struct sockaddr_un, sun_path) + 1 + strlen(su.sun_path+1));
   if (ret < 0) {
      close(fd);
      fd = -1;
      if (errno != EADDRINUSE)
         print_msg("%s: can not bind a name to UNIX socket\n", params->daemon);
      else
         fd = -2;
      goto err;
      }

   ret = listen(fd, SOMAXCONN);
   if (ret < 0) {
      close(fd);
      fd = -1;
      print_msg("%s: can not listen on UNIX socket\n", params->daemon);
      goto err;
      }
err:
   return fd;
}

/**
 * Open and connect on a UNIX socket to send command over this
 */
int cmd_connect(                   /* RETURN: UNIX socket file descriptor */
   struct pparams *params)         /* IN: input params                    */
{
   struct sockaddr_un su = {       /* The abstract UNIX socket of haveged */
      .sun_family = AF_UNIX,
      .sun_path = HAVEGED_SOCKET_PATH,
      };
   const int one = 1;
   int fd, ret;

   fd = socket(PF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
   if (fd < 0) {
      print_msg("%s: can not open UNIX socket\n", params->daemon);
      goto err;
      }

   ret = setsockopt(fd, SOL_SOCKET, SO_PASSCRED, &one, (socklen_t)sizeof(one));
   if (ret < 0) {
      print_msg("%s: can not set option for UNIX socket\n", params->daemon);
      close(fd);
      fd = -1;
      goto err;
      }

   ret = connect(fd, (struct sockaddr *)&su, offsetof(struct sockaddr_un, sun_path) + 1 + strlen(su.sun_path+1));
   if (ret < 0) {
      if (errno != ECONNREFUSED)
         print_msg("%s: can not connect on UNIX socket\n", params->daemon);
      close(fd);
      fd = -1;
      goto err;
      }
err:
   return fd;
}

/**
 * Handle arguments in command mode
 */
int getcmd(                        /* RETURN: success or error      */
   char *arg)                      /* handle current known commands */
{
   static const struct {
      const char* cmd;
      const int req;
      const int arg;
      const char* opt;
   } cmds[] = {
      { "root=",      MAGIC_CHROOT, 1, NULL },      /* New root */
      { "close",      MAGIC_CLOSE,  0, NULL },      /* Close socket */
      {0}
   }, *cmd = cmds;
   int ret = -1;

   if (!arg || !*arg)
      goto err;

   optarg = NULL;
   for (; cmd->cmd; cmd++)
      if (cmd->arg) {
         if (strncmp(cmd->cmd, arg, strlen(cmd->cmd)) == 0) {
            optarg = strchr(arg, '=');
            optarg++;
            ret = cmd->req;
            break;
            }
         }
      else {
         if (strcmp(cmd->cmd, arg) == 0) {
            ret = cmd->req;
            break;
            }
         }
err:
   return ret;
}

/**
 * Handle incomming messages from socket
 */
int socket_handler(                /* RETURN: closed file descriptor        */
   int fd,                         /* IN: connected socket file descriptor  */
   const volatile char *path,      /* IN: path of the haveged executable    */
   char *const argv[],             /* IN: arguments for the haveged process */
   struct pparams *params)         /* IN: input params                      */
{
   struct ucred cred = {0};
   unsigned char magic[2], *ptr;
   char *enqry;
   char *optarg = NULL;
   socklen_t clen;
   int ret = -1, len;

   if (fd < 0) {
      print_msg("%s: no connection jet\n", params->daemon);
      }

   ptr = &magic[0];
   len = sizeof(magic);
   ret = safein(fd, ptr, len);
   if (ret < 0) {
      print_msg("%s: can not read from UNIX socket\n", params->daemon);
      goto out;
      }

   if (magic[1] == '\002') {       /* ASCII start of text: read argument provided */
      uint32_t alen;

      ret = receive_uinteger(fd, &alen);
      if (ret < 0) {
         print_msg("%s: can not read from UNIX socket\n", params->daemon);
         goto out;
         }

      optarg = calloc(alen, sizeof(char));
      if (!optarg) {
          print_msg("can not allocate memory for message from UNIX socket");
          goto out;
          }
      ptr = (unsigned char*)optarg;

      ret = safein(fd, ptr, alen);
      if (ret < 0) {
         print_msg("%s: can not read from UNIX socket\n", params->daemon);
         goto out;
         }
      }

   clen = sizeof(struct ucred);
   ret = getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &cred, &clen);
   if (ret < 0) {
      print_msg("%s: can not get credentials from UNIX socket part1\n", params->daemon);
      goto out;
      }
   if (clen != sizeof(struct ucred)) {
      print_msg("%s: can not get credentials from UNIX socket part2\n", params->daemon);
      goto out;
      }
   if (cred.uid != 0) {
      enqry = ASCII_NAK;

      ptr = (unsigned char *)enqry;
      len = (int)strlen(enqry)+1;
      safeout(fd, ptr, len);
      }

   switch (magic[0]) {
      case MAGIC_CHROOT:
         enqry = ASCII_ACK;

         ret = new_root(optarg, path, argv, params);
         if (ret < 0) {
            uint32_t size = strlen(errmsg);
            safeout(fd, ASCII_STX, strlen(ASCII_STX));
            send_uinteger(fd, size);
            safeout(fd, errmsg, size+1);
            break;
         }

         ptr = (unsigned char *)enqry;
         len = (int)strlen(enqry);
         safeout(fd, ptr, len);

         break;
      case MAGIC_CLOSE:
         enqry = ASCII_ACK;

         close(socket_fd);
         socket_fd = -1;

         ptr = (unsigned char *)enqry;
         len = (int)strlen(enqry);
         safeout(fd, ptr, len);
         argv[0][0] = first_byte;

         break;
      default:
         enqry = ASCII_NAK;

         ptr = (unsigned char *)enqry;
         len = (int)strlen(enqry);
         safeout(fd, ptr, len);
         break;
      }
out:
   if (optarg)
      free(optarg);
   if (fd > 0) {
      close(fd);
      fd = -1;
      }
   return fd;
}

/**
 * Receive incomming messages from socket
 */
ssize_t safein(                    /* RETURN: read bytes                    */
   int fd,                         /* IN: file descriptor                   */
   void *ptr,                      /* OUT: pointer to buffer                */
   size_t len)                      /* IN: size of buffer                    */
{
   int saveerr = errno, avail;
   ssize_t ret = 0;

   if (len > SSIZE_MAX)
      len = SSIZE_MAX;

   ret = ioctl(fd, FIONREAD, &avail);
   if (ret < 0 || avail <=0)
      goto out;

   if (len > (unsigned int) avail)
      len = avail; 

   do {
      errno = saveerr;
      ssize_t p = recv(fd, ptr, len, 0 /* MSG_DONTWAIT */);
      if (p < 0) {
         if (errno == EINTR)
            continue;
         if (errno == EAGAIN || errno == EWOULDBLOCK)
            break;
         print_msg("Unable to read from socket %d: %s", socket_fd, strerror(errno));
         }
      ptr = (char *) ptr + p;
      ret += p;
      len -= p;
      }
   while (len > 0);
out:
   return ret;
}

/**
 * Send outgoing messages to socket
 */
void safeout(                      /* RETURN: nothing                       */
   int fd,                         /* IN: file descriptor                   */
   const void *ptr,                /* IN: pointer to buffer                 */
   size_t len)                     /* IN: size of buffer                    */
{
   int saveerr = errno;

   do {
      ssize_t p = send(fd, ptr, len, MSG_NOSIGNAL);
      if (p < 0) {
         if (errno == EINTR)
                     continue;
         if (errno == EPIPE || errno == EAGAIN || errno == EWOULDBLOCK)
                     break;
         print_msg("Unable to write to socket %d: %s", fd, strerror(errno));
         }
       ptr = (char *) ptr + p;
       len -= p;
       }
   while (len > 0);

   errno = saveerr;
}

/**
 * Send outgoing unsigned integer to socket
 */
void send_uinteger(                /* RETURN: nothing                       */
   int fd,                         /* IN: file descriptor                   */
   uint32_t value)                 /* IN: 32 bit unsigned integer           */
{
   uint8_t buffer[4];

   buffer[0] = (uint8_t)((value >> 24) & 0xFF);
   buffer[1] = (uint8_t)((value >> 16) & 0xFF);
   buffer[2] = (uint8_t)((value >>  8) & 0xFF);
   buffer[3] = (uint8_t)((value >>  0) & 0xFF);

   safeout(fd, buffer, 4 * sizeof(uint8_t));
}

/**
 * Receive incomming unsigned integer from socket
 */
int receive_uinteger(              /* RETURN: status                        */
   int fd,                         /* IN: file descriptor                   */
   uint32_t *value)                /* OUT: 32 bit unsigned integer          */
{
   uint8_t buffer[4];

   if (safein(fd, buffer, 4 * sizeof(uint8_t)) < 0)
      return -1;

   *value = (((uint32_t)buffer[0]) << 24) |
            (((uint32_t)buffer[1]) << 16) |
            (((uint32_t)buffer[2]) <<  8) |
            (((uint32_t)buffer[3]) <<  0);

   return 0;
}

#endif