Debian Codegraph @

src/hashsum.c

raw symbols SCIP
1 /*
2 * AIDE (Advanced Intrusion Detection Environment)
3 *
4 * Copyright (C) 2020,2022,2024-2025 Hannes von Haugwitz
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21 #include <stdbool.h>
22 #include <stdlib.h>
23 #include "config.h"cxx . . $ `<file>/include/config.h`/
24 #include "attributes.h"cxx . . $ `<file>/include/attributes.h`/
25 #include "hashsum.h"cxx . . $ `<file>/include/hashsum.h`/
26 #include "log.h"cxx . . $ `<file>/include/log.h`/
27 #include "errorcodes.h"cxx . . $ `<file>/include/errorcodes.h`/
28
29 #ifdef WITH_GCRYPT
30 #include <gcrypt.h>
31 #define NEED_LIBGCRYPT_VERSION "1.8.0"
32 #endif
33
34 hashsum_tcxx . . $ hashsum_t# hashsumscxx . . $ hashsums.[] = {
35 { attr_md5cxx . . $ attr_md5., 16 },
36 { attr_sha1cxx . . $ attr_sha1., 20 },
37 { attr_sha256cxx . . $ attr_sha256., 32 },
38 { attr_sha512cxx . . $ attr_sha512., 64 },
39 { attr_rmd160cxx . . $ attr_rmd160., 20 },
40 { attr_tigercxx . . $ attr_tiger., 24 },
41 { attr_crc32cxx . . $ attr_crc32., 4 },
42 { attr_crc32bcxx . . $ attr_crc32b., 4 },
43 { attr_havalcxx . . $ attr_haval., 32 },
44 { attr_whirlpoolcxx . . $ attr_whirlpool., 64 },
45 { attr_gostr3411_94cxx . . $ attr_gostr3411_94., 32 },
46 { attr_stribog256cxx . . $ attr_stribog256., 32 },
47 { attr_stribog512cxx . . $ attr_stribog512., 64 },
48 { attr_sha512_256cxx . . $ attr_sha512_256., 32 },
49 { attr_sha3_256cxx . . $ attr_sha3_256., 32 },
50 { attr_sha3_512cxx . . $ attr_sha3_512., 64 },
51 };
52
53 DB_ATTR_TYPEcxx . . $ `include/attributes.h:25:9`! DEPRECATED_HASHEScxx . . $ DEPRECATED_HASHES. = ATTRcxx . . $ `include/attributes.h:28:9`!(attr_md5)|ATTRcxx . . $ `include/attributes.h:28:9`!(attr_sha1)|ATTRcxx . . $ `include/attributes.h:28:9`!(attr_rmd160)|ATTRcxx . . $ `include/attributes.h:28:9`!(attr_gostr3411_94);
54 DB_ATTR_TYPEcxx . . $ `include/attributes.h:25:9`! REMOVED_HASHEScxx . . $ REMOVED_HASHES. = ATTRcxx . . $ `include/attributes.h:28:9`!(attr_crc32)|ATTRcxx . . $ `include/attributes.h:28:9`!(attr_crc32b)|ATTRcxx . . $ `include/attributes.h:28:9`!(attr_haval)|ATTRcxx . . $ `include/attributes.h:28:9`!(attr_tiger)|ATTRcxx . . $ `include/attributes.h:28:9`!(attr_whirlpool);
55
56 #ifdef WITH_NETTLEcxx . . $ `include/config.h:254:9`!
57 int algorithmscxx . . $ algorithms.[] = { /* order must match hashsums array */
58 1, /* md5 */
59 1, /* sha1 */
60 1, /* sha256 */
61 1, /* sha512 */
62 1, /* rmd160 */
63 -1, /* tiger NOT available */
64 -1, /* crc32 NOT available */
65 -1, /* crc32b NOT available */
66 -1, /* haval NOT available */
67 -1, /* whirlpool NOT available */
68 1, /* gost */
69 1, /* stribog256 */
70 1, /* stribog512 */
71 1, /* sha512_256 */
72 1, /* sha3-256 */
73 1, /* sha3-512 */
74 };
75 #endif
76
77 #ifdef WITH_GCRYPT
78 int algorithms[] = { /* order must match hashsums array */
79 GCRY_MD_MD5,
80 GCRY_MD_SHA1,
81 GCRY_MD_SHA256,
82 GCRY_MD_SHA512,
83 GCRY_MD_RMD160,
84 -1, /* tiger is no longer supported by aide */
85 -1, /* crc32 is no longer supported by aide */
86 -1, /* CRC32B is not available */
87 -1, /* GCRY_MD_HAVAL is not (yet) implemented */
88 -1, /* whirlpool is no longer supported by aide */
89 GCRY_MD_GOSTR3411_94,
90 GCRY_MD_STRIBOG256,
91 GCRY_MD_STRIBOG512,
92 GCRY_MD_SHA512_256,
93 GCRY_MD_SHA3_256,
94 GCRY_MD_SHA3_512,
95 };
96 #endif
97
98 void init_hashsum_libcxx . . $ init_hashsum_lib(49f6e7a06ebc5aa8).(void) {
99 #ifdef WITH_GCRYPT
100 if(!gcry_check_version(NEED_LIBGCRYPT_VERSION)) {
101 log_msg(LOG_LEVEL_ERROR, "libgcrypt is too old (need %s, have %s)", NEED_LIBGCRYPT_VERSION, gcry_check_version (NULL));
102 exit(VERSION_MISMATCH_ERROR);
103 }
104 gcry_control(GCRYCTL_DISABLE_SECMEM, 0);
105 gcry_control(GCRYCTL_INITIALIZATION_FINISHED, 0);
106 if (gcry_fips_mode_active()) {
107 char* str;
108 log_msg(LOG_LEVEL_NOTICE, "libgcrypt is running in FIPS mode, the following hash(es) are not available: %s", str = diff_attributes(0, ATTR(attr_md5)));
109 free(str);
110 }
111 #endif
112 }
113
114 DB_ATTR_TYPEcxx . . $ `include/attributes.h:25:9`! get_hashescxx . . $ get_hashes(48b6f722ee11d9bd).(boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! include_unsupported) {
115 DB_ATTR_TYPEcxx . . $ `include/attributes.h:25:9`! attr = 0LLU;
116 for (int i = 0; i < num_hashescxx . . $ num_hashes.; ++i) {
117 if (include_unsupported || (algorithmscxx . . $ algorithms.[i] >= 0
118 #ifdef WITH_GCRYPT
119 && (algorithms[i] != GCRY_MD_MD5 || ! gcry_fips_mode_active())
120 #endif
121 )) {
122 attr |= ATTRcxx . . $ `include/attributes.h:28:9`!(hashsums[i].attribute);
123 }
124 }
125 return attr;
126 }
127
128 DB_ATTR_TYPEcxx . . $ `include/attributes.h:25:9`! validate_hashescxx . . $ validate_hashes(ee42c3dd4b883080).(DB_ATTR_TYPEcxx . . $ `include/attributes.h:25:9`! attrs, int linenumber, char *filename, char* linebuf) {
129 char *attr_str;
130
131 DB_ATTR_TYPEcxx . . $ `include/attributes.h:25:9`! requested_hashes = attrs&get_hashescxx . . $ get_hashes(48b6f722ee11d9bd).(truecxx . . $ `<external>/13015445228558014418/stdbool.h:37:9`!);
132
133 DB_ATTR_TYPEcxx . . $ `include/attributes.h:25:9`! requested_deprecated = requested_hashes&DEPRECATED_HASHEScxx . . $ DEPRECATED_HASHES.;
134 if (requested_deprecated) {
135 attr_str = diff_attributescxx . . $ diff_attributes(39cd1df6f24d7ad6).(0, requested_deprecated);
136 LOG_CONFIG_FORMAT_LINEcxx . . $ `include/log.h:59:9`!(LOG_LEVEL_NOTICE, "hashsum(s) '%s' are DEPRECATED and will be removed in the release after next, please update your config", attr_str)
137 freecxx . . $ free(5d22bdacc48458e8).(attr_str);
138 }
139 DB_ATTR_TYPEcxx . . $ `include/attributes.h:25:9`! requested_unsupported = requested_hashes&REMOVED_HASHEScxx . . $ REMOVED_HASHES.;
140 if (requested_unsupported) {
141 DB_ATTR_TYPEcxx . . $ `include/attributes.h:25:9`! remaining_hashes = requested_hashes & ~requested_unsupported;
142 if (remaining_hashes) {
143 attr_str = diff_attributescxx . . $ diff_attributes(39cd1df6f24d7ad6).(0, requested_unsupported);
144 char *remaining_str = diff_attributescxx . . $ diff_attributes(39cd1df6f24d7ad6).(0, remaining_hashes);
145 LOG_CONFIG_FORMAT_LINEcxx . . $ `include/log.h:59:9`!(LOG_LEVEL_WARNING, "ignore unsupported hashsum(s): %s (remaining hashsum(s): %s), please update your config", attr_str, remaining_str);
146 attrs &= ~requested_unsupported;
147 freecxx . . $ free(5d22bdacc48458e8).(remaining_str);
148 freecxx . . $ free(5d22bdacc48458e8).(attr_str);
149 } else {
150 attr_str = diff_attributescxx . . $ diff_attributes(39cd1df6f24d7ad6).(0, requested_unsupported);
151 LOG_CONFIG_FORMAT_LINEcxx . . $ `include/log.h:59:9`!(LOG_LEVEL_ERROR, "no hashsum(s) left after ignoring unsupported hashsum(s): %s", attr_str);
152 freecxx . . $ free(5d22bdacc48458e8).(attr_str);
153 exitcxx . . $ exit(3ca292c338df9bb3).(INVALID_CONFIGURELINE_ERRORcxx . . $ `include/errorcodes.h:28:9`!);
154 }
155 }
156 return attrs;
157 }
158
159 DB_ATTR_TYPEcxx . . $ `include/attributes.h:25:9`! get_transition_hashsumscxx . . $ get_transition_hashsums(d940f40586ac8d6a).(char *old_filename, DB_ATTR_TYPEcxx . . $ `include/attributes.h:25:9`! old_attr, char *new_filename, DB_ATTR_TYPEcxx . . $ `include/attributes.h:25:9`! new_attr) {
160 DB_ATTR_TYPEcxx . . $ `include/attributes.h:25:9`! transition_hashsums = 0LL;
161
162 DB_ATTR_TYPEcxx . . $ `include/attributes.h:25:9`! all_hashsums = get_hashescxx . . $ get_hashes(48b6f722ee11d9bd).(truecxx . . $ `<external>/13015445228558014418/stdbool.h:37:9`!);
163 DB_ATTR_TYPEcxx . . $ `include/attributes.h:25:9`! available_hashsums = get_hashescxx . . $ get_hashes(48b6f722ee11d9bd).(falsecxx . . $ `<external>/13015445228558014418/stdbool.h:38:9`!);
164
165 DB_ATTR_TYPEcxx . . $ `include/attributes.h:25:9`! added_hashsums = (~old_attr)&new_attr&all_hashsums;
166 DB_ATTR_TYPEcxx . . $ `include/attributes.h:25:9`! removed_hashsums = old_attr&~(new_attr)&all_hashsums;
167 if (added_hashsums && removed_hashsums) {
168 char *diff_str = diff_attributescxx . . $ diff_attributes(39cd1df6f24d7ad6).(old_attr&all_hashsums, new_attr&all_hashsums);
169 DB_ATTR_TYPEcxx . . $ `include/attributes.h:25:9`! common_hashsums = old_attr&new_attr&available_hashsums;
170 if (common_hashsums == 0) {
171 transition_hashsums = removed_hashsums&available_hashsums;
172 if (transition_hashsums) {
173 char *trans_str = diff_attributescxx . . $ diff_attributes(39cd1df6f24d7ad6).(0, transition_hashsums);
174 log_msgcxx . . $ log_msg(e585f2b5603b5bf9).(LOG_LEVEL_COMPAREcxx . . $ LOG_LEVEL_COMPARE., "hashsum transition (%s) for old:'%s' and new:'%s': transition hashsum(s): %s", diff_str, old_filename, new_filename, trans_str);
175 freecxx . . $ free(5d22bdacc48458e8).(trans_str);
176 } else {
177 log_msgcxx . . $ log_msg(e585f2b5603b5bf9).(LOG_LEVEL_WARNINGcxx . . $ LOG_LEVEL_WARNING., "hashsum transition (%s) for old:'%s' and new:'%s': no common or transition hashsum(s) available", diff_str, old_filename, new_filename);
178 }
179 } else {
180 transition_hashsums = common_hashsums;
181 char *common_str = diff_attributescxx . . $ diff_attributes(39cd1df6f24d7ad6).(0, common_hashsums);
182 log_msgcxx . . $ log_msg(e585f2b5603b5bf9).(LOG_LEVEL_COMPAREcxx . . $ LOG_LEVEL_COMPARE., "hashsum transition (%s) for old:'%s' and new:'%s': common hashsum(s): %s", diff_str, old_filename, new_filename, common_str);
183 freecxx . . $ free(5d22bdacc48458e8).(common_str);
184 }
185 freecxx . . $ free(5d22bdacc48458e8).(diff_str);
186 }
187 return transition_hashsums;
188 }
189
190 DB_ATTR_TYPEcxx . . $ `include/attributes.h:25:9`! get_hashsums_to_ignorecxx . . $ get_hashsums_to_ignore(d940f40586ac8d6a).(char *old_filename, DB_ATTR_TYPEcxx . . $ `include/attributes.h:25:9`! old_attr, char *new_filename, DB_ATTR_TYPEcxx . . $ `include/attributes.h:25:9`! new_attr) {
191 DB_ATTR_TYPEcxx . . $ `include/attributes.h:25:9`! hashsums_to_ignore = 0LL;
192
193 DB_ATTR_TYPEcxx . . $ `include/attributes.h:25:9`! all_hashsums = get_hashescxx . . $ get_hashes(48b6f722ee11d9bd).(truecxx . . $ `<external>/13015445228558014418/stdbool.h:37:9`!);
194 DB_ATTR_TYPEcxx . . $ `include/attributes.h:25:9`! available_hashsums = get_hashescxx . . $ get_hashes(48b6f722ee11d9bd).(falsecxx . . $ `<external>/13015445228558014418/stdbool.h:38:9`!);
195
196 DB_ATTR_TYPEcxx . . $ `include/attributes.h:25:9`! added_hashsums = (~old_attr)&new_attr&all_hashsums;
197 DB_ATTR_TYPEcxx . . $ `include/attributes.h:25:9`! removed_hashsums = old_attr&~(new_attr)&all_hashsums;
198 if (added_hashsums && removed_hashsums) {
199 char *diff_str = diff_attributescxx . . $ diff_attributes(39cd1df6f24d7ad6).(old_attr&all_hashsums, new_attr&all_hashsums);
200 DB_ATTR_TYPEcxx . . $ `include/attributes.h:25:9`! common_hashsums = old_attr&new_attr&available_hashsums;
201 if (common_hashsums) {
202 hashsums_to_ignore = added_hashsums^removed_hashsums;
203 char *common_str = diff_attributescxx . . $ diff_attributes(39cd1df6f24d7ad6).(0, common_hashsums);
204 log_msgcxx . . $ log_msg(e585f2b5603b5bf9).(LOG_LEVEL_COMPAREcxx . . $ LOG_LEVEL_COMPARE., "│ hashsum transition (%s) for old:'%s' and new:'%s', common hashsum(s): %s", diff_str, old_filename, new_filename, common_str);
205 freecxx . . $ free(5d22bdacc48458e8).(common_str);
206 } else {
207 log_msgcxx . . $ log_msg(e585f2b5603b5bf9).(LOG_LEVEL_DEBUGcxx . . $ LOG_LEVEL_DEBUG., "│ hashsum transition (%s) for old:'%s' and new:'%s', no common hashsum available", diff_str, old_filename, new_filename);
208 }
209 freecxx . . $ free(5d22bdacc48458e8).(diff_str);
210 }
211 return hashsums_to_ignore;
212 }