Coverage Report

Created: 2025-07-23 06:42

/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
/*
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
30
#include <sudoers.h>
31
32
/*
33
 * Free memory allocated for struct sudoers_context.
34
 */
35
void
36
sudoers_ctx_free(struct sudoers_context *ctx)
37
27.3k
{
38
27.3k
    debug_decl(sudoers_ctx_free, SUDOERS_DEBUG_PLUGIN);
39
40
    /* Free remaining references to password and group entries. */
41
27.3k
    if (ctx->user.pw != NULL)
42
22.1k
  sudo_pw_delref(ctx->user.pw);
43
27.3k
    if (ctx->user.gid_list != NULL)
44
22.0k
  sudo_gidlist_delref(ctx->user.gid_list);
45
46
    /* Free dynamic contents of user_ctx. */
47
27.3k
    free(ctx->user.cwd);
48
27.3k
    free(ctx->user.name);
49
27.3k
    if (ctx->user.ttypath != NULL)
50
503
  free(ctx->user.ttypath);
51
26.8k
    else
52
26.8k
  free(ctx->user.tty);
53
27.3k
    if (ctx->user.shost != ctx->user.host)
54
117
      free(ctx->user.shost);
55
27.3k
    free(ctx->user.host);
56
27.3k
    free(ctx->user.cmnd);
57
27.3k
    canon_path_free(ctx->user.cmnd_dir);
58
27.3k
    free(ctx->user.cmnd_args);
59
27.3k
    free(ctx->user.cmnd_list);
60
27.3k
    free(ctx->user.cmnd_stat);
61
62
    /* Free remaining references to password and group entries. */
63
27.3k
    if (ctx->runas.pw != NULL)
64
21.6k
  sudo_pw_delref(ctx->runas.pw);
65
27.3k
    if (ctx->runas.gr != NULL)
66
573
  sudo_gr_delref(ctx->runas.gr);
67
27.3k
    if (ctx->runas.list_pw != NULL)
68
1.77k
  sudo_pw_delref(ctx->runas.list_pw);
69
70
    /* Free dynamic contents of ctx->runas. */
71
27.3k
    free(ctx->runas.cmnd);
72
27.3k
    free(ctx->runas.cmnd_saved);
73
27.3k
    if (ctx->runas.shost != ctx->runas.host)
74
117
  free(ctx->runas.shost);
75
27.3k
    free(ctx->runas.host);
76
27.3k
    free(ctx->runas.role);
77
27.3k
    free(ctx->runas.type);
78
27.3k
    free(ctx->runas.apparmor_profile);
79
27.3k
    free(ctx->runas.privs);
80
27.3k
    free(ctx->runas.limitprivs);
81
82
    /* Free dynamic contents of ctx. */
83
27.3k
    free(ctx->source);
84
85
27.3k
    memset(ctx, 0, sizeof(*ctx));
86
87
27.3k
    debug_return;
88
27.3k
}