Coverage Report

Created: 2025-07-11 06:57

/src/sudo/plugins/sudoers/filedigest.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * SPDX-License-Identifier: ISC
3
 *
4
 * Copyright (c) 2013-2018 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 <stdio.h>
27
#include <stdlib.h>
28
#include <string.h>
29
#include <unistd.h>
30
#include <fcntl.h>
31
#include <errno.h>
32
33
#include <sudoers.h>
34
#include <sudo_digest.h>
35
36
unsigned char *
37
sudo_filedigest(int fd, const char *file, unsigned int digest_type,
38
    size_t *digest_len)
39
0
{
40
0
    unsigned char *file_digest = NULL;
41
0
    unsigned char buf[32 * 1024];
42
0
    struct sudo_digest *dig = NULL;
43
0
    FILE *fp = NULL;
44
0
    size_t nread;
45
0
    int fd2;
46
0
    debug_decl(sudo_filedigest, SUDOERS_DEBUG_UTIL);
47
48
0
    *digest_len = sudo_digest_getlen(digest_type);
49
0
    if (*digest_len == 0) {
50
0
  sudo_warnx(U_("unsupported digest type %u for %s"), digest_type, file);
51
0
  debug_return_ptr(NULL);
52
0
    }
53
54
0
    if ((fd2 = dup(fd)) == -1) {
55
0
  sudo_debug_printf(SUDO_DEBUG_INFO, "unable to dup %s: %s",
56
0
      file, strerror(errno));
57
0
  debug_return_ptr(NULL);
58
0
    }
59
0
    if ((fp = fdopen(fd2, "r")) == NULL) {
60
0
  sudo_debug_printf(SUDO_DEBUG_INFO, "unable to fdopen %s: %s",
61
0
      file, strerror(errno));
62
0
  close(fd2);
63
0
  goto bad;
64
0
    }
65
0
    if ((file_digest = malloc(*digest_len)) == NULL) {
66
0
  sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
67
0
  goto bad;
68
0
    }
69
0
    if ((dig = sudo_digest_alloc(digest_type)) == NULL) {
70
0
  sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
71
0
  goto bad;
72
0
    }
73
0
    while ((nread = fread(buf, 1, sizeof(buf), fp)) != 0) {
74
0
  sudo_digest_update(dig, buf, nread);
75
0
    }
76
0
    if (ferror(fp)) {
77
0
  sudo_warnx(U_("%s: read error"), file);
78
0
  goto bad;
79
0
    }
80
0
    sudo_digest_final(dig, file_digest);
81
0
    sudo_digest_free(dig);
82
0
    fclose(fp);
83
84
0
    debug_return_ptr(file_digest);
85
0
bad:
86
0
    sudo_digest_free(dig);
87
0
    free(file_digest);
88
0
    if (fp != NULL)
89
0
  fclose(fp);
90
0
    debug_return_ptr(NULL);
91
0
}