Coverage Report

Created: 2026-12-01 07:07

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/sudo/plugins/sudoers/sudoers_ctx_free.c
Line
Count
Source
1
/*
2
 * SPDX-License-Identifier: ISC
3
 *
4
 * Copyright (c) 2023-2024 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
25
#include <sudoers.h>
26
27
/*
28
 * Free memory allocated for struct sudoers_context.
29
 */
30
void
31
sudoers_ctx_free(struct sudoers_context *ctx)
32
42.2k
{
33
42.2k
    debug_decl(sudoers_ctx_free, SUDOERS_DEBUG_PLUGIN);
34
35
    /* Free remaining references to password and group entries. */
36
42.2k
    if (ctx->user.pw != NULL)
37
32.9k
  sudo_pw_delref(ctx->user.pw);
38
42.2k
    if (ctx->user.gid_list != NULL)
39
24.0k
  sudo_gidlist_delref(ctx->user.gid_list);
40
41
    /* Free dynamic contents of user_ctx. */
42
42.2k
    free(ctx->user.cwd);
43
42.2k
    free(ctx->user.name);
44
42.2k
    if (ctx->user.ttypath != NULL)
45
640
  free(ctx->user.ttypath);
46
41.6k
    else
47
41.6k
  free(ctx->user.tty);
48
42.2k
    if (ctx->user.shost != ctx->user.host)
49
2
      free(ctx->user.shost);
50
42.2k
    free(ctx->user.host);
51
42.2k
    free(ctx->user.cmnd);
52
42.2k
    canon_path_free(ctx->user.cmnd_dir);
53
42.2k
    free(ctx->user.cmnd_args);
54
42.2k
    free(ctx->user.cmnd_list);
55
42.2k
    free(ctx->user.cmnd_stat);
56
57
    /* Free remaining references to password and group entries. */
58
42.2k
    if (ctx->runas.pw != NULL)
59
32.7k
  sudo_pw_delref(ctx->runas.pw);
60
42.2k
    if (ctx->runas.gr != NULL)
61
654
  sudo_gr_delref(ctx->runas.gr);
62
42.2k
    if (ctx->runas.list_pw != NULL)
63
2.06k
  sudo_pw_delref(ctx->runas.list_pw);
64
65
    /* Free dynamic contents of ctx->runas. */
66
42.2k
    free(ctx->runas.cmnd);
67
42.2k
    free(ctx->runas.cmnd_saved);
68
42.2k
    if (ctx->runas.shost != ctx->runas.host)
69
2
  free(ctx->runas.shost);
70
42.2k
    free(ctx->runas.host);
71
42.2k
    free(ctx->runas.role);
72
42.2k
    free(ctx->runas.type);
73
42.2k
    free(ctx->runas.apparmor_profile);
74
42.2k
    free(ctx->runas.privs);
75
42.2k
    free(ctx->runas.limitprivs);
76
77
    /* Free dynamic contents of ctx. */
78
42.2k
    free(ctx->source);
79
80
42.2k
    memset(ctx, 0, sizeof(*ctx));
81
82
42.2k
    debug_return;
83
42.2k
}