Bug Summary

File:daemons/ipa-kdb/ipa_kdb_mkey.c
Warning:line 224, column 5
Potential leak of memory pointed to by 'mods'

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -triple x86_64-unknown-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name ipa_kdb_mkey.c -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model pic -pic-level 2 -mthread-model posix -mframe-pointer=all -fmath-errno -fno-rounding-math -masm-verbose -mconstructor-aliases -munwind-tables -target-cpu x86-64 -dwarf-column-info -fno-split-dwarf-inlining -debugger-tuning=gdb -resource-dir /usr/lib64/clang/10.0.0 -D HAVE_CONFIG_H -I . -I ../.. -I . -I ../../util -D PREFIX="/usr/local" -D BINDIR="/usr/local/bin" -D LIBDIR="/usr/local/lib" -D LIBEXECDIR="/usr/local/libexec" -D DATADIR="/usr/local/share" -D LDAPIDIR="/run" -I /usr/include/samba-4.0 -D _GNU_SOURCE=1 -D HAVE_IMMEDIATE_STRUCTURES=1 -I /usr/include/nss3 -I /usr/include/nspr4 -I /usr/include/nspr4 -I /usr/include/nss3 -I /usr/include/nspr4 -D __STDC_WANT_LIB_EXT1__=1 -D _DEFAULT_SOURCE=1 -D _POSIX_C_SOURCE=200809L -D PIC -internal-isystem /usr/local/include -internal-isystem /usr/lib64/clang/10.0.0/include -internal-externc-isystem /include -internal-externc-isystem /usr/include -fdebug-compilation-dir /home/heimes/redhat/freeipa/daemons/ipa-kdb -ferror-limit 19 -fmessage-length 0 -fgnuc-version=4.2.1 -fobjc-runtime=gcc -fdiagnostics-show-option -analyzer-output=html -faddrsig -o /home/heimes/redhat/freeipa/report/2020-06-05-101548-295465-1 -x c ipa_kdb_mkey.c
1/*
2 * MIT Kerberos KDC database backend for FreeIPA
3 *
4 * Authors: Simo Sorce <ssorce@redhat.com>
5 *
6 * Copyright (C) 2011 Simo Sorce, Red Hat
7 * see file 'COPYING' for use and warranty information
8 *
9 * This program is free software you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 */
22
23#include "ipa_kdb.h"
24
25static char *krbmkey_attrs[] = {
26 "krbMKey",
27 NULL((void*)0)
28};
29
30krb5_error_code ipadb_fetch_master_key(krb5_context kcontext,
31 krb5_principal mname,
32 krb5_keyblock *key,
33 krb5_kvno *kvno,
34 char *db_args)
35{
36 struct ipadb_context *ipactx;
37 LDAPMessage *res = NULL((void*)0);
38 LDAPMessage *first;
39 struct berval **vals = NULL((void*)0);
40 BerElement *be = NULL((void*)0);
41 krb5_error_code kerr;
42 krb5_keyblock k;
43 int mkvno;
44 int ret;
45 int i;
46
47 ipactx = ipadb_get_context(kcontext);
48 if (!ipactx) {
49 return KRB5_KDB_DBNOTINITED(-1780008435L);
50 }
51
52 if (!ipactx->lcontext) {
53 ret = ipadb_get_connection(ipactx);
54 if (ret != 0) {
55 kerr = KRB5_KDB_SERVER_INTERNAL_ERR(-1780008413L);
56 goto done;
57 }
58 }
59
60 be = ber_alloc_t(LBER_USE_DER0x01);
61 if (!be) {
62 kerr = ENOMEM12;
63 goto done;
64 }
65
66 kerr = ipadb_simple_search(ipactx, ipactx->realm_base, LDAP_SCOPE_BASE((ber_int_t) 0x0000),
67 "(krbMKey=*)", krbmkey_attrs, &res);
68 if (kerr) {
69 goto done;
70 }
71
72 first = ldap_first_entry(ipactx->lcontext, res);
73 if (!first) {
74 kerr = KRB5_KDB_NOENTRY(-1780008443L);
75 goto done;
76 }
77
78 mkvno = 0;
79 k.contents = NULL((void*)0);
80 vals = ldap_get_values_len(ipactx->lcontext, first, "krbmkey");
81 for (i = 0; vals[i]; i++) {
82 struct berval *mkey;
83 ber_tag_t tag;
84 ber_int_t tvno;
85 ber_int_t ttype;
86
87 ber_init2(be, vals[i], LBER_USE_DER0x01);
88
89 tag = ber_scanf(be, "{i{iO}}", &tvno, &ttype, &mkey);
90 if (tag == LBER_ERROR((ber_tag_t) -1)) {
91 kerr = KRB5_KDB_SERVER_INTERNAL_ERR(-1780008413L);
92 goto done;
93 }
94
95 if (tvno > mkvno) {
96 mkvno = tvno;
97 k.enctype = ttype;
98 k.length = mkey->bv_len;
99 if (k.contents) {
100 free(k.contents);
101 }
102 k.contents = malloc(k.length);
103 if (!k.contents) {
104 kerr = ENOMEM12;
105 goto done;
106 }
107 memcpy(k.contents, mkey->bv_val, k.length);
108 }
109 ber_bvfree(mkey);
110 }
111
112 if (mkvno == 0) {
113 kerr = KRB5_KDB_NOENTRY(-1780008443L);
114 goto done;
115 }
116
117 *kvno = mkvno;
118 key->magic = KV5M_KEYBLOCK(-1760647421L);
119 key->enctype = k.enctype;
120 key->length = k.length;
121 key->contents = k.contents;
122
123 kerr = 0;
124
125done:
126 if (be) {
127 ber_free(be, 0);
128 }
129 ldap_value_free_len(vals);
130 ldap_msgfree(res);
131 return kerr;
132}
133
134krb5_error_code ipadb_store_master_key_list(krb5_context kcontext,
135 char *db_arg,
136 krb5_principal mname,
137 krb5_keylist_node *keylist,
138 char *master_pwd)
139{
140 struct ipadb_context *ipactx;
141 BerElement *be = NULL((void*)0);
142 krb5_keyblock k = { 0, 0, 0, NULL((void*)0) };
143 struct berval mkey;
144 ber_int_t tvno;
145 ber_int_t ttype;
146 LDAPMod **mods = NULL((void*)0);
147 krb5_error_code kerr;
148 int ret;
149
150 ipactx = ipadb_get_context(kcontext);
151 if (!ipactx) {
1
Assuming 'ipactx' is non-null
2
Taking false branch
152 return KRB5_KDB_DBNOTINITED(-1780008435L);
153 }
154
155 /* we support storing only one key for now */
156 if (!keylist || keylist->next) {
3
Assuming 'keylist' is non-null
4
Assuming field 'next' is null
5
Taking false branch
157 return EINVAL22;
158 }
159
160 if (!ipactx->lcontext) {
6
Assuming field 'lcontext' is non-null
7
Taking false branch
161 ret = ipadb_get_connection(ipactx);
162 if (ret != 0) {
163 kerr = KRB5_KDB_SERVER_INTERNAL_ERR(-1780008413L);
164 goto done;
165 }
166 }
167
168 be = ber_alloc_t(LBER_USE_DER0x01);
169 if (!be) {
8
Assuming 'be' is non-null
9
Taking false branch
170 kerr = ENOMEM12;
171 goto done;
172 }
173
174
175 tvno = keylist->kvno;
176 ttype = keylist->keyblock.enctype;
177 mkey.bv_len = keylist->keyblock.length;
178 mkey.bv_val = (void *)keylist->keyblock.contents;
179
180 ret = ber_printf(be, "{i{iO}}", tvno, ttype, &mkey);
181 if (ret == -1) {
10
Assuming the condition is false
11
Taking false branch
182 kerr = KRB5_KDB_INTERNAL_ERROR(-1780008411L);
183 goto done;
184 }
185
186 mods = calloc(2, sizeof(LDAPMod *));
12
Memory is allocated
187 if (!mods) {
13
Assuming 'mods' is non-null
14
Taking false branch
188 kerr = ENOMEM12;
189 goto done;
190 }
191 mods[0] = calloc(1, sizeof(LDAPMod));
192 if (!mods[0]) {
15
Assuming the condition is true
16
Taking true branch
193 kerr = ENOMEM12;
194 goto done;
17
Control jumps to line 219
195 }
196 mods[0]->mod_op = LDAP_MOD_ADD(0x0000) | LDAP_MOD_BVALUES(0x0080);
197 mods[0]->mod_type = strdup("krbMKey");
198 if (!mods[0]->mod_type) {
199 kerr = ENOMEM12;
200 goto done;
201 }
202 mods[0]->mod_bvaluesmod_vals.modv_bvals = calloc(2, sizeof(struct berval *));
203 if (!mods[0]->mod_bvaluesmod_vals.modv_bvals) {
204 kerr = ENOMEM12;
205 goto done;
206 }
207
208 ret = ber_flatten(be, &mods[0]->mod_bvaluesmod_vals.modv_bvals[0]);
209 if (ret == -1) {
210 kerr = KRB5_KDB_INTERNAL_ERROR(-1780008411L);
211 goto done;
212 }
213
214 kerr = ipadb_simple_modify(ipactx, ipactx->realm_base, mods);
215
216 kerr = 0;
217
218done:
219 if (be
17.1
'be' is non-null
) {
18
Taking true branch
220 ber_free(be, 1);
221 }
222 krb5_free_keyblock_contents(kcontext, &k);
223 ldap_mods_free(mods, 1);
224 return kerr;
19
Potential leak of memory pointed to by 'mods'
225}