Coverage Report

Created: 2023-06-07 06:47

/src/sudo/lib/iolog/iolog_openat.c
Line
Count
Source (jump to first uncovered line)
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
/*
20
 * This is an open source non-commercial project. Dear PVS-Studio, please check it.
21
 * PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
22
 */
23
24
#include <config.h>
25
26
#include <sys/stat.h>
27
#include <stdio.h>
28
#include <stdlib.h>
29
#ifdef HAVE_STDBOOL_H
30
# include <stdbool.h>
31
#else
32
# include "compat/stdbool.h"
33
#endif
34
#include <errno.h>
35
#include <fcntl.h>
36
#include <unistd.h>
37
38
#include "sudo_compat.h"
39
#include "sudo_debug.h"
40
#include "sudo_fatal.h"
41
#include "sudo_gettext.h"
42
#include "sudo_iolog.h"
43
#include "sudo_util.h"
44
45
/*
46
 * Wrapper for openat(2) that sets umask and retries as iolog_uid/iolog_gid
47
 * if openat(2) returns EACCES.
48
 */
49
int
50
iolog_openat(int dfd, const char *path, int flags)
51
843
{
52
843
    const mode_t iolog_filemode = iolog_get_file_mode();
53
843
    const mode_t iolog_dirmode = iolog_get_dir_mode();
54
843
    mode_t omask = S_IRWXG|S_IRWXO;
55
843
    int fd;
56
843
    debug_decl(iolog_openat, SUDO_DEBUG_UTIL);
57
58
843
    if (ISSET(flags, O_CREAT)) {
59
  /* umask must not be more restrictive than the file modes. */
60
0
  omask = umask(ACCESSPERMS & ~(iolog_filemode|iolog_dirmode));
61
0
    }
62
843
    fd = openat(dfd, path, flags, iolog_filemode);
63
843
    if (fd == -1 && errno == EACCES) {
64
  /* Enable write bit if it is missing. */
65
0
  struct stat sb;
66
0
  if (fstatat(dfd, path, &sb, 0) == 0) {
67
0
      mode_t write_bits = iolog_filemode & (S_IWUSR|S_IWGRP|S_IWOTH);
68
0
      if ((sb.st_mode & write_bits) != write_bits) {
69
0
    if (fchmodat(dfd, path, iolog_filemode, 0) == 0)
70
0
        fd = openat(dfd, path, flags, iolog_filemode);
71
0
      }
72
0
  }
73
0
    }
74
843
    if (fd == -1 && errno == EACCES) {
75
  /* Try again as the I/O log owner (for NFS). */
76
0
  if (iolog_swapids(false)) {
77
0
      fd = openat(dfd, path, flags, iolog_filemode);
78
0
      if (!iolog_swapids(true)) {
79
    /* iolog_swapids() warns on error. */
80
0
    if (fd != -1) {
81
0
        close(fd);
82
0
        fd = -1;
83
0
    }
84
0
      }
85
0
  }
86
0
    }
87
843
    if (ISSET(flags, O_CREAT))
88
0
  umask(omask);
89
843
    debug_return_int(fd);
90
843
}