| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
#ifdef HAVE_CONFIG_H |
| 34 |
#include <config.h> |
| 35 |
#endif |
| 36 |
|
| 37 |
#include "_seap.h" |
| 38 |
#include <probe-api.h> |
| 39 |
#include <stdlib.h> |
| 40 |
#include <string.h> |
| 41 |
#include <sys/types.h> |
| 42 |
#include <sys/stat.h> |
| 43 |
#include <fcntl.h> |
| 44 |
#include <limits.h> |
| 45 |
#include <pthread.h> |
| 46 |
#include <errno.h> |
| 47 |
#include <crapi/crapi.h> |
| 48 |
#include <probe/probe.h> |
| 49 |
#include <probe/option.h> |
| 50 |
|
| 51 |
#include "oval_fts.h" |
| 52 |
#include <common/debug_priv.h> |
| 53 |
#include "filehash_probe.h" |
| 54 |
|
| 55 |
#define FILE_SEPARATOR '/' |
| 56 |
|
| 57 |
static int mem2hex (uint8_t *mem, size_t mlen, char *str, size_t slen) |
| 58 |
{ |
| 59 |
const char ch[] = "0123456789abcdef"; |
| 60 |
register size_t i; |
| 61 |
|
| 62 |
if (slen < (mlen * 2) + 1) { |
| 63 |
errno = ENOBUFS; |
| 64 |
return (-1); |
| 65 |
} |
| 66 |
|
| 67 |
for (i = 0; i < mlen; ++i) { |
| 68 |
str[i*2 ] = ch[(mem[i] & 0xf0) >> 4]; |
| 69 |
str[i*2+1] = ch[(mem[i] & 0x0f)]; |
| 70 |
} |
| 71 |
|
| 72 |
str[i*2] = '\0'; |
| 73 |
|
| 74 |
return (0); |
| 75 |
} |
| 76 |
|
| 77 |
static int filehash_cb (const char *prefix, const char *p, const char *f, probe_ctx *ctx, oval_schema_version_t over) |
| 78 |
{ |
| 79 |
SEXP_t *itm; |
| 80 |
char pbuf[PATH_MAX+1]; |
| 81 |
size_t plen, flen; |
| 82 |
bool include_filepath; |
| 83 |
int fd; |
| 84 |
|
| 85 |
if (f == NULL) |
| 86 |
return (0); |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
plen = strlen (p); |
| 92 |
flen = strlen (f); |
| 93 |
|
| 94 |
if (plen + flen + 1 > PATH_MAX) |
| 95 |
return (-1); |
| 96 |
|
| 97 |
memcpy (pbuf, p, sizeof (char) * plen); |
| 98 |
|
| 99 |
if (p[plen - 1] != FILE_SEPARATOR) { |
| 100 |
pbuf[plen] = FILE_SEPARATOR; |
| 101 |
++plen; |
| 102 |
} |
| 103 |
|
| 104 |
memcpy (pbuf + plen, f, sizeof (char) * flen); |
| 105 |
pbuf[plen+flen] = '\0'; |
| 106 |
include_filepath = oval_schema_version_cmp(over, OVAL_SCHEMA_VERSION(5.6)) >= 0; |
| 107 |
|
| 108 |
if (probe_path_is_blocked(pbuf, ctx->blocked_paths)) { |
| 109 |
return 0; |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
if (prefix == NULL) { |
| 116 |
fd = open(pbuf, O_RDONLY); |
| 117 |
} else { |
| 118 |
char *path_with_prefix = oscap_path_join(prefix, pbuf); |
| 119 |
fd = open(path_with_prefix, O_RDONLY); |
| 120 |
free(path_with_prefix); |
| 121 |
} |
| 122 |
|
| 123 |
if (fd < 0) { |
| 124 |
#define __ERRBUF_SIZE 128 |
| 125 |
char errbuf[__ERRBUF_SIZE] = {0}; |
| 126 |
oscap_strerror_r(errno, errbuf, sizeof errbuf - 1); |
| 127 |
|
| 128 |
itm = probe_item_create(OVAL_INDEPENDENT_FILE_HASH, NULL, |
| 129 |
"filepath", OVAL_DATATYPE_STRING, include_filepath ? pbuf : NULL, |
| 130 |
"path", OVAL_DATATYPE_STRING, p, |
| 131 |
"filename", OVAL_DATATYPE_STRING, f, |
| 132 |
NULL |
| 133 |
); |
| 134 |
probe_item_add_msg(itm, OVAL_MESSAGE_LEVEL_ERROR, |
| 135 |
"Can't open \"%s\": %s (errno=%d).", pbuf, errbuf, errno); |
| 136 |
probe_item_setstatus(itm, SYSCHAR_STATUS_ERROR); |
| 137 |
|
| 138 |
} else { |
| 139 |
uint8_t md5_dst[16]; |
| 140 |
size_t md5_dstlen = sizeof md5_dst; |
| 141 |
char md5_str[(sizeof md5_dst * 2) + 1]; |
| 142 |
|
| 143 |
uint8_t sha1_dst[20]; |
| 144 |
size_t sha1_dstlen = sizeof sha1_dst; |
| 145 |
char sha1_str[(sizeof sha1_dst * 2) + 1]; |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
if (crapi_mdigest_fd (fd, 2, |
| 151 |
CRAPI_DIGEST_MD5, md5_dst, &md5_dstlen, |
| 152 |
CRAPI_DIGEST_SHA1, sha1_dst, &sha1_dstlen) != 0) |
| 153 |
{ |
| 154 |
close (fd); |
| 155 |
return (-1); |
| 156 |
} |
| 157 |
|
| 158 |
close (fd); |
| 159 |
|
| 160 |
md5_str[0] = '\0'; |
| 161 |
sha1_str[0] = '\0'; |
| 162 |
mem2hex (md5_dst, md5_dstlen, md5_str, sizeof md5_str); |
| 163 |
mem2hex (sha1_dst, sha1_dstlen, sha1_str, sizeof sha1_str); |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
itm = probe_item_create(OVAL_INDEPENDENT_FILE_HASH, NULL, |
| 169 |
"filepath", OVAL_DATATYPE_STRING, include_filepath ? pbuf : NULL, |
| 170 |
"path", OVAL_DATATYPE_STRING, p, |
| 171 |
"filename", OVAL_DATATYPE_STRING, f, |
| 172 |
"md5", OVAL_DATATYPE_STRING, md5_str, |
| 173 |
"sha1", OVAL_DATATYPE_STRING, sha1_str, |
| 174 |
NULL); |
| 175 |
|
| 176 |
if (md5_dstlen == 0 || sha1_dstlen == 0) |
| 177 |
probe_item_setstatus(itm, SYSCHAR_STATUS_ERROR); |
| 178 |
if (md5_dstlen == 0) |
| 179 |
probe_item_add_msg(itm, OVAL_MESSAGE_LEVEL_ERROR, |
| 180 |
"Unable to compute md5 hash value of \"%s\".", pbuf); |
| 181 |
if (sha1_dstlen == 0) |
| 182 |
probe_item_add_msg(itm, OVAL_MESSAGE_LEVEL_ERROR, |
| 183 |
"Unable to compute sha1 hash value of \"%s\".", pbuf); |
| 184 |
} |
| 185 |
|
| 186 |
probe_item_collect(ctx, itm); |
| 187 |
|
| 188 |
return (0); |
| 189 |
} |
| 190 |
|
| 191 |
int filehash_probe_offline_mode_supported() |
| 192 |
{ |
| 193 |
return PROBE_OFFLINE_OWN; |
| 194 |
} |
| 195 |
|
| 196 |
void *filehash_probe_init(void) |
| 197 |
{ |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
pthread_mutex_t *filehash_probe_mutex = malloc(sizeof(pthread_mutex_t)); |
| 202 |
switch (pthread_mutex_init(filehash_probe_mutex, NULL)) { |
| 203 |
case 0: |
| 204 |
return ((void *)filehash_probe_mutex); |
| 205 |
default: |
| 206 |
dD("Can't initialize mutex: errno=%u, %s.", errno, strerror (errno)); |
| 207 |
free(filehash_probe_mutex); |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
return (NULL); |
| 212 |
} |
| 213 |
|
| 214 |
void filehash_probe_fini(void *arg) |
| 215 |
{ |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
pthread_mutex_t *filehash_probe_mutex = (pthread_mutex_t *)arg; |
| 220 |
(void) pthread_mutex_destroy(filehash_probe_mutex); |
| 221 |
free(filehash_probe_mutex); |
| 222 |
} |
| 223 |
|
| 224 |
int filehash_probe_main(probe_ctx *ctx, void *arg) |
| 225 |
{ |
| 226 |
SEXP_t *path, *filename, *behaviors, *filepath, *probe_in; |
| 227 |
|
| 228 |
OVAL_FTS *ofts; |
| 229 |
OVAL_FTSENT *ofts_ent; |
| 230 |
oval_schema_version_t over; |
| 231 |
|
| 232 |
pthread_mutex_t *filehash_probe_mutex = (pthread_mutex_t *)arg; |
| 233 |
if (filehash_probe_mutex == NULL) { |
| 234 |
return (PROBE_EINIT); |
| 235 |
} |
| 236 |
|
| 237 |
probe_in = probe_ctx_getobject(ctx); |
| 238 |
|
| 239 |
path = probe_obj_getent (probe_in, "path", 1); |
| 240 |
filename = probe_obj_getent (probe_in, "filename", 1); |
| 241 |
behaviors = probe_obj_getent (probe_in, "behaviors", 1); |
| 242 |
filepath = probe_obj_getent (probe_in, "filepath", 1); |
| 243 |
over = probe_obj_get_platform_schema_version(probe_in); |
| 244 |
|
| 245 |
|
| 246 |
if ( (path == NULL || filename == NULL) && filepath==NULL ) { |
| 247 |
SEXP_free (behaviors); |
| 248 |
SEXP_free (path); |
| 249 |
SEXP_free (filename); |
| 250 |
SEXP_free (filepath); |
| 251 |
|
| 252 |
return (PROBE_ENOELM); |
| 253 |
} |
| 254 |
|
| 255 |
probe_filebehaviors_canonicalize(&behaviors); |
| 256 |
|
| 257 |
switch (pthread_mutex_lock(filehash_probe_mutex)) { |
| 258 |
case 0: |
| 259 |
break; |
| 260 |
default: |
| 261 |
dD("Can't lock mutex(%p): %u, %s.", filehash_probe_mutex, errno, strerror (errno)); |
| 262 |
|
| 263 |
SEXP_free (behaviors); |
| 264 |
SEXP_free (path); |
| 265 |
SEXP_free (filename); |
| 266 |
SEXP_free (filepath); |
| 267 |
|
| 268 |
return (PROBE_EFATAL); |
| 269 |
} |
| 270 |
|
| 271 |
const char *prefix = getenv("OSCAP_PROBE_ROOT"); |
| 272 |
if ((ofts = oval_fts_open_prefixed(prefix, path, filename, filepath, behaviors, probe_ctx_getresult(ctx))) != NULL) { |
| 273 |
while ((ofts_ent = oval_fts_read(ofts)) != NULL) { |
| 274 |
filehash_cb(prefix, ofts_ent->path, ofts_ent->file, ctx, over); |
| 275 |
oval_ftsent_free(ofts_ent); |
| 276 |
} |
| 277 |
|
| 278 |
oval_fts_close(ofts); |
| 279 |
} |
| 280 |
|
| 281 |
SEXP_free (behaviors); |
| 282 |
SEXP_free (path); |
| 283 |
SEXP_free (filename); |
| 284 |
SEXP_free (filepath); |
| 285 |
|
| 286 |
switch (pthread_mutex_unlock(filehash_probe_mutex)) { |
| 287 |
case 0: |
| 288 |
break; |
| 289 |
default: |
| 290 |
dD("Can't unlock mutex(%p): %u, %s.", filehash_probe_mutex, errno, strerror (errno)); |
| 291 |
|
| 292 |
return (PROBE_EFATAL); |
| 293 |
} |
| 294 |
|
| 295 |
return 0; |
| 296 |
} |