Coverage Report

Created: 2025-10-28 06:14

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/sudo/lib/iolog/iolog_open.c
Line
Count
Source
1
/*
2
 * SPDX-License-Identifier: ISC
3
 *
4
 * Copyright (c) 2009-2021 Todd C. Miller <Todd.Miller@sudo.ws>
5
 *
6
 * Permission to use, copy, modify, and distribute this software for any
7
 * purpose with or without fee is hereby granted, provided that the above
8
 * copyright notice and this permission notice appear in all copies.
9
 *
10
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17
 */
18
19
#include <config.h>
20
21
#include <stdio.h>
22
#include <stdlib.h>
23
#ifdef HAVE_STDBOOL_H
24
# include <stdbool.h>
25
#else
26
# include <compat/stdbool.h>
27
#endif
28
#include <errno.h>
29
#include <fcntl.h>
30
#include <time.h>
31
#include <unistd.h>
32
33
#include <sudo_compat.h>
34
#include <sudo_debug.h>
35
#include <sudo_iolog.h>
36
#include <sudo_util.h>
37
38
static unsigned char const gzip_magic[2] = {0x1f, 0x8b};
39
40
/*
41
 * Open the specified I/O log file and store in iol.
42
 * Stores the open file handle which has the close-on-exec flag set.
43
 * Also locks the file if iofd is IOFD_TIMING and mode is writable.
44
 * The "r+" and "w+" modes are not supported for compressed logs
45
 * so the "+" will be stripped before calling gzdopen().
46
 */
47
bool
48
iolog_open(struct iolog_file *iol, int dfd, int iofd, const char *mode)
49
821
{
50
821
    int flags;
51
821
    const char *file;
52
821
    bool lockit = false;
53
821
    unsigned char magic[2];
54
821
    const uid_t iolog_uid = iolog_get_uid();
55
821
    const gid_t iolog_gid = iolog_get_gid();
56
821
    debug_decl(iolog_open, SUDO_DEBUG_UTIL);
57
58
821
    if (mode[0] == 'r') {
59
821
  flags = mode[1] == '+' ? O_RDWR : O_RDONLY;
60
821
    } else if (mode[0] == 'w') {
61
0
  flags = O_CREAT|O_TRUNC;
62
0
  flags |= mode[1] == '+' ? O_RDWR : O_WRONLY;
63
0
    } else {
64
0
  sudo_debug_printf(SUDO_DEBUG_ERROR,
65
0
      "%s: invalid I/O mode %s", __func__, mode);
66
0
  debug_return_bool(false);
67
0
    }
68
821
    if ((file = iolog_fd_to_name(iofd)) == NULL) {
69
0
  sudo_debug_printf(SUDO_DEBUG_ERROR,
70
0
      "%s: invalid iofd %d", __func__, iofd);
71
0
  debug_return_bool(false);
72
0
    }
73
821
    if (iofd == IOFD_TIMING && ISSET(flags, O_WRONLY|O_RDWR)) {
74
0
  lockit = true;
75
0
    }
76
77
821
    iol->writable = false;
78
821
    iol->compressed = false;
79
821
    iol->locked = false;
80
821
    if (iol->enabled) {
81
821
  int fd = iolog_openat(dfd, file, flags);
82
821
  if (lockit && fd != -1) {
83
0
      if (sudo_lock_file(fd, SUDO_TLOCK)) {
84
0
    iol->locked = true;
85
0
      } else {
86
0
    sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_ERRNO,
87
0
        "%s: unable to lock %s", __func__, file);
88
0
    close(fd);
89
0
    fd = -1;
90
0
      }
91
0
  }
92
821
  if (fd != -1) {
93
821
      if (*mode == 'w') {
94
0
    if (fchown(fd, iolog_uid, iolog_gid) != 0) {
95
0
        sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_ERRNO,
96
0
      "%s: unable to fchown %d:%d %s", __func__,
97
0
      (int)iolog_uid, (int)iolog_gid, file);
98
0
    }
99
0
    iol->compressed = iolog_get_compress();
100
821
      } else {
101
    /* check for gzip magic number */
102
821
    if (pread(fd, magic, sizeof(magic), 0) == ssizeof(magic)) {
103
805
        if (magic[0] == gzip_magic[0] && magic[1] == gzip_magic[1])
104
1
      iol->compressed = true;
105
805
    }
106
821
      }
107
      /*
108
       * Compressed logs don't support random access, gzdopen() will
109
       * fail for mode "r+".  Caller must check the compressed flag.
110
       */
111
821
      if (iol->compressed)
112
1
    mode = *mode == 'r' ? "r" : "w";
113
821
      if (fcntl(fd, F_SETFD, FD_CLOEXEC) != -1) {
114
#ifdef HAVE_ZLIB_H
115
    if (iol->compressed)
116
        iol->fd.g = gzdopen(fd, mode);
117
    else
118
#endif
119
821
        iol->fd.f = fdopen(fd, mode);
120
821
      }
121
821
      if (iol->fd.v != NULL) {
122
821
    switch ((flags & O_ACCMODE)) {
123
0
    case O_WRONLY:
124
0
    case O_RDWR:
125
0
        iol->writable = true;
126
0
        break;
127
821
    }
128
821
      } else {
129
0
    int save_errno = errno;
130
0
    close(fd);
131
0
    errno = save_errno;
132
0
    fd = -1;
133
0
      }
134
821
  }
135
821
  if (fd == -1) {
136
0
      iol->enabled = false;
137
0
      debug_return_bool(false);
138
0
  }
139
821
    } else {
140
0
  if (*mode == 'w') {
141
      /* Remove old log file in case we recycled sequence numbers. */
142
0
      (void)unlinkat(dfd, file, 0);
143
0
  }
144
0
    }
145
821
    debug_return_bool(true);
146
821
}