Coverage Report

Created: 2025-07-11 06:58

/src/sudo/lib/util/mktemp.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * SPDX-License-Identifier: ISC
3
 *
4
 * Copyright (c) 2001, 2003, 2004, 2008-2011, 2013, 2015, 2017, 2018, 2022
5
 *  Todd C. Miller <Todd.Miller@sudo.ws>
6
 *
7
 * Permission to use, copy, modify, and distribute this software for any
8
 * purpose with or without fee is hereby granted, provided that the above
9
 * copyright notice and this permission notice appear in all copies.
10
 *
11
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18
 */
19
20
/*
21
 * This is an open source non-commercial project. Dear PVS-Studio, please check it.
22
 * PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
23
 */
24
25
#include <config.h>
26
27
#if (!defined(HAVE_MKDTEMPAT) && !defined(HAVE_MKDTEMPAT_NP)) || \
28
    (!defined(HAVE_MKOSTEMPSAT) && !defined(HAVE_MKOSTEMPSAT_NP))
29
30
#include <sys/stat.h>
31
32
#include <errno.h>
33
#include <fcntl.h>
34
#include <limits.h>
35
#include <stdio.h>
36
#include <stdlib.h>
37
#if defined(HAVE_STDINT_H)
38
# include <stdint.h>
39
#elif defined(HAVE_INTTYPES_H)
40
# include <inttypes.h>
41
#endif
42
#include <string.h>
43
#include <ctype.h>
44
#include <unistd.h>
45
46
#include <sudo_compat.h>
47
#include <sudo_rand.h>
48
#include <pathnames.h>
49
50
11.5k
#define MKTEMP_FILE 1
51
1.44k
#define MKTEMP_DIR  2
52
53
45.4k
#define TEMPCHARS "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
54
38.9k
#define NUM_CHARS (sizeof(TEMPCHARS) - 1)
55
25.9k
#define MIN_X   6
56
57
6.48k
#define MKOTEMP_FLAGS (O_APPEND | O_CLOEXEC | O_SYNC)
58
59
static int
60
mktemp_internal(int dfd, char *path, int slen, int mode, int flags)
61
6.48k
{
62
6.48k
  char *start, *cp, *ep;
63
6.48k
  const char tempchars[] = TEMPCHARS;
64
6.48k
  unsigned int tries;
65
6.48k
  size_t len;
66
6.48k
  int fd;
67
68
6.48k
  len = strlen(path);
69
6.48k
  if (len < MIN_X || slen < 0 || (size_t)slen > len - MIN_X) {
70
0
    errno = EINVAL;
71
0
    return -1;
72
0
  }
73
6.48k
  ep = path + len - slen;
74
75
45.4k
  for (start = ep; start > path && start[-1] == 'X'; start--)
76
38.9k
    ;
77
6.48k
  if (ep - start < MIN_X) {
78
0
    errno = EINVAL;
79
0
    return -1;
80
0
  }
81
82
6.48k
  if (flags & ~MKOTEMP_FLAGS) {
83
0
    errno = EINVAL;
84
0
    return -1;
85
0
  }
86
6.48k
  flags |= O_CREAT | O_EXCL | O_RDWR;
87
88
6.48k
  tries = INT_MAX;
89
6.48k
  do {
90
6.48k
    cp = start;
91
6.48k
    do {
92
6.48k
      unsigned short rbuf[16];
93
6.48k
      size_t i;
94
95
      /*
96
       * Avoid lots of arc4random() calls by using
97
       * a buffer sized for up to 16 Xs at a time.
98
       */
99
6.48k
      arc4random_buf(rbuf, sizeof(rbuf));
100
45.4k
      for (i = 0; i < nitems(rbuf) && cp != ep; i++)
101
38.9k
        *cp++ = tempchars[rbuf[i] % NUM_CHARS];
102
6.48k
    } while (cp != ep);
103
104
6.48k
    switch (mode) {
105
5.76k
    case MKTEMP_FILE:
106
5.76k
      fd = openat(dfd, path, flags, S_IRUSR|S_IWUSR);
107
5.76k
      if (fd != -1 || errno != EEXIST)
108
5.76k
        return fd;
109
0
      break;
110
724
    case MKTEMP_DIR:
111
724
      if (mkdirat(dfd, path, S_IRWXU) == 0)
112
724
        return 0;
113
0
      if (errno != EEXIST)
114
0
        return -1;
115
0
      break;
116
6.48k
    }
117
6.48k
  } while (--tries);
118
119
0
  errno = EEXIST;
120
0
  return -1;
121
6.48k
}
122
123
char *
124
sudo_mkdtemp(char *path)
125
724
{
126
724
  if (mktemp_internal(AT_FDCWD, path, 0, MKTEMP_DIR, 0) == -1)
127
0
    return NULL;
128
724
  return path;
129
724
}
130
131
char *
132
sudo_mkdtempat(int dfd, char *path)
133
0
{
134
0
  if (mktemp_internal(dfd, path, 0, MKTEMP_DIR, 0) == -1)
135
0
    return NULL;
136
0
  return path;
137
0
}
138
139
int
140
sudo_mkostempsat(int dfd, char *path, int slen, int flags)
141
0
{
142
0
  return mktemp_internal(dfd, path, slen, MKTEMP_FILE, flags);
143
0
}
144
145
#ifdef notyet
146
int
147
sudo_mkostemps(char *path, int slen, int flags)
148
{
149
  return mktemp_internal(AT_FDCWD, path, slen, MKTEMP_FILE, flags);
150
}
151
#endif
152
153
int
154
sudo_mkstemp(char *path)
155
5.76k
{
156
5.76k
  return mktemp_internal(AT_FDCWD, path, 0, MKTEMP_FILE, 0);
157
5.76k
}
158
159
#ifdef notyet
160
int
161
sudo_mkostemp(char *path, int flags)
162
{
163
  return mktemp_internal(AT_FDCWD, path, 0, MKTEMP_FILE, flags);
164
}
165
#endif
166
167
int
168
sudo_mkstemps(char *path, int slen)
169
0
{
170
0
  return mktemp_internal(AT_FDCWD, path, slen, MKTEMP_FILE, 0);
171
0
}
172
#endif /* !HAVE_MKDTEMPAT || !HAVE_MKOSTEMPSAT */