Debian Codegraph @

src/OVAL/probes/independent/filehash58_probe.c

raw symbols SCIP
1 /**
2 * @file filehash58_probe.c
3 * @brief filehash58 probe
4 * @author "Daniel Kopecek" <[email protected]>
5 *
6 * 2011/07/01 [email protected]
7 * This probe is able to process a filehash58_object as defined in OVAL 5.8
8 *
9 */
10
11 /*
12 * Copyright 2011 Red Hat Inc., Durham, North Carolina.
13 * All Rights Reserved.
14 *
15 * This library is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU Lesser General Public
17 * License as published by the Free Software Foundation; either
18 * version 2.1 of the License, or (at your option) any later version.
19 *
20 * This library is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 * Lesser General Public License for more details.
24 *
25 * You should have received a copy of the GNU Lesser General Public
26 * License along with this library; if not, write to the Free Software
27 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
28 *
29 * Authors:
30 * "Daniel Kopecek" <[email protected]>
31 * "Petr Lautrbach" <[email protected]>
32 */
33
34 #ifdef HAVE_CONFIG_H
35 #include <config.h>
36 #endif
37
38 #include "_seap.h"
39 #include <probe-api.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <sys/types.h>
43 #include <sys/stat.h>
44 #include <fcntl.h>
45 #include <limits.h>
46 #include <pthread.h>
47 #include <errno.h>
48 #include <crapi/crapi.h>
49 #include <probe/probe.h>
50 #include <probe/option.h>
51
52 #include "common/debug_priv.h"
53 #include "oval_fts.h"
54 #include "util.h"
55 #include "probe/entcmp.h"
56 #include "filehash58_probe.h"
57 #include "oscap_helpers.h"
58
59 #define FILE_SEPARATOR '/'
60
61 /* List of hash types listed in OVAL specification */
62 static const char *OVAL_FILEHASH58_HASH_TYPES[] = {
63 "MD5",
64 "SHA-1",
65 "SHA-224",
66 "SHA-256",
67 "SHA-384",
68 "SHA-512",
69 NULL
70 };
71
72 /* List of hash types supported by OpenSCAP */
73 static const struct oscap_string_map CRAPI_ALG_MAP[] = {
74 #ifdef OPENSCAP_ENABLE_MD5
75 {CRAPI_DIGEST_MD5, "MD5"},
76 #endif
77 #ifdef OPENSCAP_ENABLE_SHA1
78 {CRAPI_DIGEST_SHA1, "SHA-1"},
79 #endif
80 {CRAPI_DIGEST_SHA224, "SHA-224"},
81 {CRAPI_DIGEST_SHA256, "SHA-256"},
82 {CRAPI_DIGEST_SHA384, "SHA-384"},
83 {CRAPI_DIGEST_SHA512, "SHA-512"},
84 /* {CRAPI_DIGEST_RMD160, "RMD-160"}, OVAL doesn't support this */
85 {0, NULL}
86 };
87
88 static const struct oscap_string_map CRAPI_ALG_MAP_SIZE[] = {
89 #ifdef OPENSCAP_ENABLE_MD5
90 {16, "MD5"},
91 #endif
92 #ifdef OPENSCAP_ENABLE_SHA1
93 {20, "SHA-1"},
94 #endif
95 {28, "SHA-224"},
96 {32, "SHA-256"},
97 {48, "SHA-384"},
98 {64, "SHA-512"},
99 /* {20, "RMD-160"}, OVAL doesn't support this */
100 {0, NULL}
101 };
102
103
104 static int mem2hex (uint8_t *mem, size_t mlen, char *str, size_t slen)
105 {
106 const char ch[] = "0123456789abcdef";
107 register size_t i;
108
109 if (slen < (mlen * 2) + 1) {
110 errno = ENOBUFS;
111 return (-1);
112 }
113
114 for (i = 0; i < mlen; ++i) {
115 str[i*2 ] = ch[(mem[i] & 0xf0) >> 4];
116 str[i*2+1] = ch[(mem[i] & 0x0f)];
117 }
118
119 str[i*2] = '\0';
120
121 return (0);
122 }
123
124 static int filehash58_cb(const char *prefix, const char *p, const char *f, const char *h, probe_ctx *ctx)
125 {
126 SEXP_t *itm;
127
128 char pbuf[PATH_MAX+1];
129 size_t plen, flen;
130
131 int fd;
132
133 if (f == NULL)
134 return (0);
135
136 /*
137 * Prepare path
138 */
139 plen = strlen (p);
140 flen = strlen (f);
141
142 if (plen + flen + 1 > PATH_MAX)
143 return (-1);
144
145 memcpy (pbuf, p, sizeof (char) * plen);
146
147 if (p[plen - 1] != FILE_SEPARATOR) {
148 pbuf[plen] = FILE_SEPARATOR;
149 ++plen;
150 }
151
152 memcpy (pbuf + plen, f, sizeof (char) * flen);
153 pbuf[plen+flen] = '\0';
154
155 if (probe_path_is_blocked(pbuf, ctx->blocked_paths)) {
156 return 0;
157 }
158
159 /*
160 * Open the file
161 */
162 if (prefix == NULL) {
163 fd = open(pbuf, O_RDONLY);
164 } else {
165 char *path_with_prefix = oscap_path_join(prefix, pbuf);
166 fd = open(path_with_prefix, O_RDONLY);
167 free(path_with_prefix);
168 }
169
170 if (fd < 0) {
171 #define __ERRBUF_SIZE 128
172 char errbuf[__ERRBUF_SIZE] = {0};
173 oscap_strerror_r(errno, errbuf, sizeof errbuf - 1);
174
175 itm = probe_item_create (OVAL_INDEPENDENT_FILE_HASH58, NULL,
176 "filepath", OVAL_DATATYPE_STRING, pbuf,
177 "path", OVAL_DATATYPE_STRING, p,
178 "filename", OVAL_DATATYPE_STRING, f,
179 "hash_type",OVAL_DATATYPE_STRING, h,
180 NULL);
181 probe_item_add_msg(itm, OVAL_MESSAGE_LEVEL_ERROR,
182 "Can't open \"%s\": %s (errno=%d).", pbuf, errbuf, errno);
183 probe_item_setstatus(itm, SYSCHAR_STATUS_ERROR);
184
185 probe_item_collect(ctx, itm);
186 return 0;
187 }
188
189 uint8_t hash_dst[1025];
190 size_t hash_dstlen = sizeof(hash_dst);
191 char hash_str[2051];
192 crapi_alg_t hash_type;
193
194 hash_type = oscap_string_to_enum(CRAPI_ALG_MAP, h);
195 if (hash_type == 0) {
196 char *msg = oscap_sprintf("This version of OpenSCAP doesn't support the '%s' hash algorithm.", h);
197 dW("%s", msg);
198 itm = probe_item_create (OVAL_INDEPENDENT_FILE_HASH58, NULL,
199 "filepath", OVAL_DATATYPE_STRING, pbuf,
200 "path", OVAL_DATATYPE_STRING, p,
201 "filename", OVAL_DATATYPE_STRING, f,
202 "hash_type", OVAL_DATATYPE_STRING, h,
203 NULL);
204 probe_item_add_msg(itm, OVAL_MESSAGE_LEVEL_ERROR, "%s", msg);
205 free(msg);
206 probe_item_setstatus(itm, SYSCHAR_STATUS_ERROR);
207 probe_item_collect(ctx, itm);
208 close(fd);
209 return 0;
210 }
211
212 hash_dstlen = oscap_string_to_enum(CRAPI_ALG_MAP_SIZE, h);
213
214 /*
215 * Compute hash value
216 */
217 if (crapi_mdigest_fd(fd, 1, hash_type, hash_dst, &hash_dstlen) != 0) {
218 close (fd);
219 return (-1);
220 }
221
222 close (fd);
223
224 hash_str[0] = '\0';
225 mem2hex(hash_dst, hash_dstlen, hash_str, sizeof(hash_str));
226
227 /*
228 * Create and add the item
229 */
230 itm = probe_item_create(OVAL_INDEPENDENT_FILE_HASH58, NULL,
231 "filepath", OVAL_DATATYPE_STRING, pbuf,
232 "path", OVAL_DATATYPE_STRING, p,
233 "filename", OVAL_DATATYPE_STRING, f,
234 "hash_type",OVAL_DATATYPE_STRING, h,
235 "hash", OVAL_DATATYPE_STRING, hash_str,
236 NULL);
237
238 if (hash_dstlen == 0) {
239 probe_item_add_msg(itm, OVAL_MESSAGE_LEVEL_ERROR,
240 "Unable to compute %s hash value of \"%s\".", h, pbuf);
241 probe_item_setstatus(itm, SYSCHAR_STATUS_ERROR);
242 }
243
244 probe_item_collect(ctx, itm);
245
246 return (0);
247 }
248
249 int filehash58_probe_offline_mode_supported()
250 {
251 return PROBE_OFFLINE_OWN;
252 }
253
254 void *filehash58_probe_init(void)
255 {
256 /*
257 * Initialize mutex.
258 */
259 pthread_mutex_t *filehash58_probe_mutex = malloc(sizeof(pthread_mutex_t));
260 switch (pthread_mutex_init(filehash58_probe_mutex, NULL)) {
261 case 0:
262 return ((void *)filehash58_probe_mutex);
263 default:
264 dD("Can't initialize mutex: errno=%u, %s.", errno, strerror (errno));
265 free(filehash58_probe_mutex);
266 }
267
268 return (NULL);
269 }
270
271 void filehash58_probe_fini(void *arg)
272 {
273 /*
274 * Destroy mutex.
275 */
276 pthread_mutex_t *filehash58_probe_mutex = (pthread_mutex_t *)arg;
277 (void) pthread_mutex_destroy(filehash58_probe_mutex);
278 free(filehash58_probe_mutex);
279 }
280
281 int filehash58_probe_main(probe_ctx *ctx, void *arg)
282 {
283 SEXP_t *probe_in;
284 SEXP_t *path, *filename, *behaviors, *filepath, *hash_type;
285 char hash_type_str[128];
286 int err = 0;
287
288 OVAL_FTS *ofts;
289 OVAL_FTSENT *ofts_ent;
290
291 pthread_mutex_t *filehash58_probe_mutex = (pthread_mutex_t *)arg;
292 if (filehash58_probe_mutex == NULL) {
293 return (PROBE_EINIT);
294 }
295
296 probe_in = probe_ctx_getobject(ctx);
297
298 path = probe_obj_getent (probe_in, "path", 1);
299 filename = probe_obj_getent (probe_in, "filename", 1);
300 behaviors = probe_obj_getent (probe_in, "behaviors", 1);
301 filepath = probe_obj_getent (probe_in, "filepath", 1);
302 hash_type = probe_obj_getent (probe_in, "hash_type", 1);
303
304 /* we want hash_type and either path+filename or filepath */
305 if ( ! hash_type ||
306 ((path == NULL || filename == NULL) && filepath==NULL )
307 ) {
308 err = PROBE_ENOENT;
309 goto cleanup;
310 }
311
312 PROBE_ENT_STRVAL(hash_type, hash_type_str, sizeof hash_type_str, err = PROBE_ENOVAL;, err = PROBE_ENOVAL;);
313 if (err != 0)
314 goto cleanup;
315
316 probe_filebehaviors_canonicalize(&behaviors);
317
318 switch (pthread_mutex_lock(filehash58_probe_mutex)) {
319 case 0:
320 break;
321 default:
322 dD("Can't lock mutex(%p): %u, %s.", filehash58_probe_mutex, errno, strerror(errno));
323
324 err = PROBE_EFATAL;
325 goto cleanup;
326 }
327
328 const char *prefix = getenv("OSCAP_PROBE_ROOT");
329 if ((ofts = oval_fts_open_prefixed(prefix, path, filename, filepath, behaviors, probe_ctx_getresult(ctx))) != NULL) {
330 while ((ofts_ent = oval_fts_read(ofts)) != NULL) {
331 /* find hash types to compare with entity, think "not satisfy" */
332 for (int i = 0; OVAL_FILEHASH58_HASH_TYPES[i] != NULL; i++) {
333 const char *oval_filehash58_hash_type = OVAL_FILEHASH58_HASH_TYPES[i];
334 SEXP_t *oval_filehash58_hash_type_sexp = SEXP_string_new(oval_filehash58_hash_type, strlen(oval_filehash58_hash_type));
335 if (probe_entobj_cmp(hash_type, oval_filehash58_hash_type_sexp) == OVAL_RESULT_TRUE) {
336 filehash58_cb(prefix, ofts_ent->path, ofts_ent->file, oval_filehash58_hash_type, ctx);
337 }
338
339 SEXP_free(oval_filehash58_hash_type_sexp);
340 }
341 oval_ftsent_free(ofts_ent);
342 }
343
344 oval_fts_close(ofts);
345 }
346
347 cleanup:
348 SEXP_free (behaviors);
349 SEXP_free (path);
350 SEXP_free (filename);
351 SEXP_free (filepath);
352 SEXP_free (hash_type);
353
354 switch (pthread_mutex_unlock(filehash58_probe_mutex)) {
355 case 0:
356 break;
357 default:
358 dD("Can't unlock mutex(%p): %u, %s.", filehash58_probe_mutex, errno, strerror(errno));
359
360 err = PROBE_EFATAL;
361 }
362
363 return err;
364 }