Coverage Report

Created: 2025-12-28 06:49

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