Coverage Report

Created: 2025-11-24 06:37

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