aboutsummaryrefslogtreecommitdiff
path: root/src/tlsdate-setter.c
blob: 138be59edb2d5b889e456e111c24b9ea6832028a (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
/*
 * tlsdate-setter.c - privileged time setter for tlsdated
 * Copyright (c) 2013 The Chromium Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "config.h"

#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/prctl.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

#include <event2/event.h>

#include "src/conf.h"
#include "src/dbus.h"
#include "src/seccomp.h"
#include "src/tlsdate.h"
#include "src/util.h"

/* Atomically writes the timestamp to the specified fd. */
int
save_timestamp_to_fd (int fd, time_t t)
{
  return platform->file_write(fd, &t, sizeof (t));
}

void
report_setter_error (siginfo_t *info)
{
  const char *code;
  int killit = 0;
  switch (info->si_code)
    {
    case CLD_EXITED:
      code = "EXITED";
      break;
    case CLD_KILLED:
      code = "KILLED";
      break;
    case CLD_DUMPED:
      code = "DUMPED";
      break;
    case CLD_STOPPED:
      code = "STOPPED";
      killit = 1;
      break;
    case CLD_TRAPPED:
      code = "TRAPPED";
      killit = 1;
      break;
    case CLD_CONTINUED:
      code = "CONTINUED";
      killit = 1;
      break;
    default:
      code = "???";
      killit = 1;
    }
  info ("tlsdate-setter exitting: code:%s status:%d pid:%d uid:%d",
        code, info->si_status, info->si_pid, info->si_uid);
  if (killit)
    kill (info->si_pid, SIGKILL);
}

void
time_setter_coprocess (int time_fd, int notify_fd, struct state *state)
{
  int save_fd = -1;
  int status;
  prctl (PR_SET_NAME, "tlsdated-setter");
  if (state->opts.should_save_disk && !state->opts.dry_run)
    {
      const mode_t perms = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
      /* TODO(wad) platform->file_open */
      if ( (save_fd = open (state->timestamp_path,
                            O_WRONLY | O_CREAT | O_NOFOLLOW | O_CLOEXEC,
                            perms)) < 0 ||
           fchmod (save_fd, perms) != 0)
        {
          /* Attempt to unlink the path on the way out. */
          unlink (state->timestamp_path);
          status = SETTER_NO_SAVE;
          goto notify_and_die;
        }
    }
  /* XXX: Drop all privs but CAP_SYS_TIME */
#ifdef HAVE_SECCOMP_FILTER
  if (enable_setter_seccomp())
    {
      status = SETTER_NO_SBOX;
      goto notify_and_die;
    }
#endif
  while (1)
    {
      struct timeval tv = { 0, 0 };
      /* The wire protocol is a time_t, but the caller should
       * always be the unprivileged tlsdated process which spawned this
       * helper.
       * There are two special messages:
       * (time_t)   0: requests a clean shutdown
       * (time_t) < 0: indicates not to write to disk
       * On Linux, time_t is a signed long.  Expanding the protocol
       * is easy, but writing one long only is ideal.
       */
      ssize_t bytes = read (time_fd, &tv.tv_sec, sizeof (tv.tv_sec));
      int save = 1;
      if (bytes == -1)
        {
          if (errno == EINTR)
            continue;
          status = SETTER_READ_ERR;
          goto notify_and_die;
        }
      if (bytes == 0)
        {
          /* End of pipe */
          status = SETTER_READ_ERR;
          goto notify_and_die;
        }
      if (bytes != sizeof (tv.tv_sec))
        continue;
      if (tv.tv_sec < 0)
        {
          /* Don't write to disk */
          tv.tv_sec = -tv.tv_sec;
          save = 0;
        }
      if (tv.tv_sec == 0)
        {
          status = SETTER_EXIT;
          goto notify_and_die;
        }
      if (is_sane_time (tv.tv_sec))
        {
          /* It would be nice if time was only allowed to move forward, but
           * if a single time source is wrong, then it could make it impossible
           * to recover from once the time is written to disk.
           */
          status = SETTER_BAD_TIME;
          if (!state->opts.dry_run)
            {
              if (settimeofday (&tv, NULL) < 0)
                {
                  status = SETTER_SET_ERR;
                  goto notify_and_die;
                }
              if (state->opts.should_sync_hwclock &&
                  platform->rtc_write(&state->hwclock, &tv))
                {
                  status = SETTER_NO_RTC;
                  goto notify_and_die;
                }
              if (save && save_fd != -1 &&
                  save_timestamp_to_fd (save_fd, tv.tv_sec))
                {
                  status = SETTER_NO_SAVE;
                  goto notify_and_die;
                }
            }
          status = SETTER_TIME_SET;
        }
      /* TODO(wad) platform->file_write */
      IGNORE_EINTR (write (notify_fd, &status, sizeof(status)));
    }
notify_and_die:
  IGNORE_EINTR (write (notify_fd, &status, sizeof(status)));
  close (notify_fd);
  close (save_fd);
  _exit (status);
}