Coverage Report

Created: 2026-05-16 07:05

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
23.4k
{
33
23.4k
    debug_decl(sudoers_ctx_free, SUDOERS_DEBUG_PLUGIN);
34
35
    /* Free remaining references to password and group entries. */
36
23.4k
    if (ctx->user.pw != NULL)
37
18.9k
  sudo_pw_delref(ctx->user.pw);
38
23.4k
    if (ctx->user.gid_list != NULL)
39
18.8k
  sudo_gidlist_delref(ctx->user.gid_list);
40
41
    /* Free dynamic contents of user_ctx. */
42
23.4k
    free(ctx->user.cwd);
43
23.4k
    free(ctx->user.name);
44
23.4k
    if (ctx->user.ttypath != NULL)
45
802
  free(ctx->user.ttypath);
46
22.6k
    else
47
22.6k
  free(ctx->user.tty);
48
23.4k
    if (ctx->user.shost != ctx->user.host)
49
17
      free(ctx->user.shost);
50
23.4k
    free(ctx->user.host);
51
23.4k
    free(ctx->user.cmnd);
52
23.4k
    canon_path_free(ctx->user.cmnd_dir);
53
23.4k
    free(ctx->user.cmnd_args);
54
23.4k
    free(ctx->user.cmnd_list);
55
23.4k
    free(ctx->user.cmnd_stat);
56
57
    /* Free remaining references to password and group entries. */
58
23.4k
    if (ctx->runas.pw != NULL)
59
18.5k
  sudo_pw_delref(ctx->runas.pw);
60
23.4k
    if (ctx->runas.gr != NULL)
61
553
  sudo_gr_delref(ctx->runas.gr);
62
23.4k
    if (ctx->runas.list_pw != NULL)
63
1.55k
  sudo_pw_delref(ctx->runas.list_pw);
64
65
    /* Free dynamic contents of ctx->runas. */
66
23.4k
    free(ctx->runas.cmnd);
67
23.4k
    free(ctx->runas.cmnd_saved);
68
23.4k
    if (ctx->runas.shost != ctx->runas.host)
69
17
  free(ctx->runas.shost);
70
23.4k
    free(ctx->runas.host);
71
23.4k
    free(ctx->runas.role);
72
23.4k
    free(ctx->runas.type);
73
23.4k
    free(ctx->runas.apparmor_profile);
74
23.4k
    free(ctx->runas.privs);
75
23.4k
    free(ctx->runas.limitprivs);
76
77
    /* Free dynamic contents of ctx. */
78
23.4k
    free(ctx->source);
79
80
23.4k
    memset(ctx, 0, sizeof(*ctx));
81
82
23.4k
    debug_return;
83
23.4k
}