Coverage Report

Created: 2025-10-10 07:09

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
 */
44
bool
45
iolog_open(struct iolog_file *iol, int dfd, int iofd, const char *mode)
46
825
{
47
825
    int flags;
48
825
    const char *file;
49
825
    unsigned char magic[2];
50
825
    const uid_t iolog_uid = iolog_get_uid();
51
825
    const gid_t iolog_gid = iolog_get_gid();
52
825
    debug_decl(iolog_open, SUDO_DEBUG_UTIL);
53
54
825
    if (mode[0] == 'r') {
55
825
  flags = mode[1] == '+' ? O_RDWR : O_RDONLY;
56
825
    } else if (mode[0] == 'w') {
57
0
  flags = O_CREAT|O_TRUNC;
58
0
  flags |= mode[1] == '+' ? O_RDWR : O_WRONLY;
59
0
    } else {
60
0
  sudo_debug_printf(SUDO_DEBUG_ERROR,
61
0
      "%s: invalid I/O mode %s", __func__, mode);
62
0
  debug_return_bool(false);
63
0
    }
64
825
    if ((file = iolog_fd_to_name(iofd)) == NULL) {
65
0
  sudo_debug_printf(SUDO_DEBUG_ERROR,
66
0
      "%s: invalid iofd %d", __func__, iofd);
67
0
  debug_return_bool(false);
68
0
    }
69
70
825
    iol->writable = false;
71
825
    iol->compressed = false;
72
825
    if (iol->enabled) {
73
825
  int fd = iolog_openat(dfd, file, flags);
74
825
  if (fd != -1) {
75
825
      if (*mode == 'w') {
76
0
    if (fchown(fd, iolog_uid, iolog_gid) != 0) {
77
0
        sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_ERRNO,
78
0
      "%s: unable to fchown %d:%d %s", __func__,
79
0
      (int)iolog_uid, (int)iolog_gid, file);
80
0
    }
81
0
    iol->compressed = iolog_get_compress();
82
825
      } else {
83
    /* check for gzip magic number */
84
825
    if (pread(fd, magic, sizeof(magic), 0) == ssizeof(magic)) {
85
810
        if (magic[0] == gzip_magic[0] && magic[1] == gzip_magic[1])
86
1
      iol->compressed = true;
87
810
    }
88
825
      }
89
825
      if (fcntl(fd, F_SETFD, FD_CLOEXEC) != -1) {
90
#ifdef HAVE_ZLIB_H
91
    if (iol->compressed)
92
        iol->fd.g = gzdopen(fd, mode);
93
    else
94
#endif
95
825
        iol->fd.f = fdopen(fd, mode);
96
825
      }
97
825
      if (iol->fd.v != NULL) {
98
825
    switch ((flags & O_ACCMODE)) {
99
0
    case O_WRONLY:
100
0
    case O_RDWR:
101
0
        iol->writable = true;
102
0
        break;
103
825
    }
104
825
      } else {
105
0
    int save_errno = errno;
106
0
    close(fd);
107
0
    errno = save_errno;
108
0
    fd = -1;
109
0
      }
110
825
  }
111
825
  if (fd == -1) {
112
0
      iol->enabled = false;
113
0
      debug_return_bool(false);
114
0
  }
115
825
    } else {
116
0
  if (*mode == 'w') {
117
      /* Remove old log file in case we recycled sequence numbers. */
118
0
      (void)unlinkat(dfd, file, 0);
119
0
  }
120
0
    }
121
825
    debug_return_bool(true);
122
825
}