Coverage Report

Created: 2023-06-07 06:47

/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.6k
#define MKTEMP_FILE 1
51
1.68k
#define MKTEMP_DIR  2
52
53
46.6k
#define TEMPCHARS "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
54
39.9k
#define NUM_CHARS (sizeof(TEMPCHARS) - 1)
55
26.6k
#define MIN_X   6
56
57
6.65k
#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.65k
{
62
6.65k
  char *start, *cp, *ep;
63
6.65k
  const char tempchars[] = TEMPCHARS;
64
6.65k
  unsigned int tries;
65
6.65k
  size_t len;
66
6.65k
  int fd;
67
68
6.65k
  len = strlen(path);
69
6.65k
  if (len < MIN_X || slen < 0 || (size_t)slen > len - MIN_X) {
70
0
    errno = EINVAL;
71
0
    return -1;
72
0
  }
73
6.65k
  ep = path + len - slen;
74
75
46.6k
  for (start = ep; start > path && start[-1] == 'X'; start--)
76
39.9k
    ;
77
6.65k
  if (ep - start < MIN_X) {
78
0
    errno = EINVAL;
79
0
    return -1;
80
0
  }
81
82
6.65k
  if (flags & ~MKOTEMP_FLAGS) {
83
0
    errno = EINVAL;
84
0
    return -1;
85
0
  }
86
6.65k
  flags |= O_CREAT | O_EXCL | O_RDWR;
87
88
6.65k
  tries = INT_MAX;
89
6.65k
  do {
90
6.65k
    cp = start;
91
6.65k
    do {
92
6.65k
      unsigned short rbuf[16];
93
6.65k
      unsigned int 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.65k
      arc4random_buf(rbuf, sizeof(rbuf));
100
46.6k
      for (i = 0; i < nitems(rbuf) && cp != ep; i++)
101
39.9k
        *cp++ = tempchars[rbuf[i] % NUM_CHARS];
102
6.65k
    } while (cp != ep);
103
104
6.65k
    switch (mode) {
105
5.81k
    case MKTEMP_FILE:
106
5.81k
      fd = openat(dfd, path, flags, S_IRUSR|S_IWUSR);
107
5.81k
      if (fd != -1 || errno != EEXIST)
108
5.81k
        return fd;
109
0
      break;
110
843
    case MKTEMP_DIR:
111
843
      if (mkdirat(dfd, path, S_IRWXU) == 0)
112
843
        return 0;
113
0
      if (errno != EEXIST)
114
0
        return -1;
115
0
      break;
116
6.65k
    }
117
6.65k
  } while (--tries);
118
119
0
  errno = EEXIST;
120
0
  return -1;
121
6.65k
}
122
123
char *
124
sudo_mkdtemp(char *path)
125
843
{
126
843
  if (mktemp_internal(AT_FDCWD, path, 0, MKTEMP_DIR, 0) == -1)
127
0
    return NULL;
128
843
  return path;
129
843
}
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.81k
{
156
5.81k
  return mktemp_internal(AT_FDCWD, path, 0, MKTEMP_FILE, 0);
157
5.81k
}
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 */