| 1 | /* |
| 2 | * AIDE (Advanced Intrusion Detection Environment) |
| 3 | * |
| 4 | * Copyright (C) 1999-2006, 2010-2011, 2016-2017, 2019-2025 Rami Lehti, |
| 5 | * Pablo Virolainen, Mike Markley, Richard van den Berg, |
| 6 | * Hannes von Haugwitz |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License as |
| 10 | * published by the Free Software Foundation; either version 2 of the |
| 11 | * License, or (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, but |
| 14 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | * General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along |
| 19 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 21 | */ |
| 22 | |
| 23 | #include "config.h" |
| 24 | #include <dirent.h> |
| 25 | #include <errno.h> |
| 26 | #include <fcntl.h> |
| 27 | #include <pthread.h> |
| 28 | #include <stdbool.h> |
| 29 | #include <stdio.h> |
| 30 | #include <stdlib.h> |
| 31 | #include <string.h> |
| 32 | #include <sys/stat.h> |
| 33 | #ifdef HAVE_FSTYPE |
| 34 | #include <sys/vfs.h> |
| 35 | #endif |
| 36 | #include <unistd.h> |
| 37 | #include "aide.h" |
| 38 | #include "attributes.h" |
| 39 | #include "do_md.h" |
| 40 | #include "db.h" |
| 41 | #include "db_config.h" |
| 42 | #include "db_disk.h" |
| 43 | #include "db_line.h" |
| 44 | #include "errorcodes.h" |
| 45 | #include "file.h" |
| 46 | #include "gen_list.h" |
| 47 | #include "log.h" |
| 48 | #include "queue.h" |
| 49 | #include "rx_rule.h" |
| 50 | #include "seltree_struct.h" |
| 51 | #include "util.h" |
| 52 | |
| 53 | queue_ts_t *queue_worker_entries = NULL; |
| 54 | |
| 55 | struct worker_args { |
| 56 | long worker_index; |
| 57 | bool dry_run; |
| 58 | }; |
| 59 | |
| 60 | typedef struct worker_thread { |
| 61 | pthread_t thread; |
| 62 | struct worker_args args; |
| 63 | } worker_thread; |
| 64 | |
| 65 | static char *name_construct (const char *dirpath, const char *filename) { |
| 66 | int dirpath_len = strlen (dirpath); |
| 67 | int len = dirpath_len + strlen(filename) + (dirpath[dirpath_len-1] != '/'?1:0) + 1; |
| 68 | char *ret = checked_malloc(len); |
| 69 | snprintf(ret, len, "%s%s%s", dirpath, dirpath[dirpath_len-1] != '/'?"/":"", filename); |
| 70 | log_msg(LOG_LEVEL_TRACE,"name_construct: dir: '%s' (%p) + filename: '%s' (%p): '%s' (%p)", dirpath, (void*) dirpath, filename, (void*) filename, ret, (void*) ret); |
| 71 | return ret; |
| 72 | } |
| 73 | |
| 74 | static bool open_for_reading(disk_entry *entry, bool dir_rec, const char *whoami) { |
| 75 | int fd = -1; |
| 76 | struct stat fs; |
| 77 | char* failure_str = NULL; |
| 78 | char* error_str = NULL; |
| 79 | |
| 80 | DB_ATTR_TYPE attrs_req_read = entry->attrs & (get_hashes(false) |
| 81 | #ifdef WITH_E2FSATTRS |
| 82 | | ATTR(attr_e2fsattrs) |
| 83 | #endif |
| 84 | #ifndef O_PATH |
| 85 | #ifdef WITH_POSIX_ACL |
| 86 | | ATTR(attr_acl) |
| 87 | #endif |
| 88 | #ifdef WITH_XATTR |
| 89 | | ATTR(attr_xattrs) |
| 90 | #endif |
| 91 | #ifdef WITH_CAPABILITIES |
| 92 | | ATTR(attr_capabilities) |
| 93 | #endif |
| 94 | #endif |
| 95 | ); |
| 96 | if (attrs_req_read || dir_rec) { |
| 97 | #ifdef O_NOATIME |
| 98 | if ((fd = open(entry->filename, O_NOFOLLOW | O_RDONLY | O_NOATIME | O_NONBLOCK)) == -1) { |
| 99 | LOG_WHOAMI(LOG_LEVEL_DEBUG, "%s> open() with O_NOATIME flag failed: %s (retrying without O_NOATIME)", entry->filename, strerror(errno)); |
| 100 | #endif |
| 101 | fd = open(entry->filename, O_NOFOLLOW | O_RDONLY | O_NONBLOCK); |
| 102 | #ifdef O_NOATIME |
| 103 | } |
| 104 | #endif |
| 105 | if (fd == -1) { |
| 106 | failure_str = "open() failed"; |
| 107 | error_str = checked_strdup(strerror(errno)); |
| 108 | } else { |
| 109 | LOG_WHOAMI(LOG_LEVEL_TRACE, "%s> open() returned O_RDONLY fd %d", entry->filename, fd); |
| 110 | if (fstat(fd, &fs) != 0) { |
| 111 | failure_str = "fstat() failed"; |
| 112 | error_str = checked_strdup(strerror(errno)); |
| 113 | if (close(fd) < 0) { |
| 114 | log_msg(LOG_LEVEL_WARNING, "close() failed for '%s' (fd: %d): %s", entry->filename, fd, strerror(errno)); |
| 115 | } |
| 116 | fd = -1; |
| 117 | } else { |
| 118 | if(!(entry->attrs&ATTR(attr_rdev))) { |
| 119 | fs.st_rdev=0; |
| 120 | } |
| 121 | DB_ATTR_TYPE stat_diff; |
| 122 | if ((stat_diff = stat_cmp(&entry->fs, &fs, entry->attrs&ATTR(attr_growing))) != RETOK) { |
| 123 | failure_str = "stat fields changed"; |
| 124 | error_str = diff_attributes(0, stat_diff); |
| 125 | if (close(fd) < 0) { |
| 126 | log_msg(LOG_LEVEL_WARNING, "close() failed for '%s' (fd: %d): %s", entry->filename, fd, strerror(errno)); |
| 127 | } |
| 128 | fd = -1; |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | if (fd == -1) { |
| 134 | char *attrs_str = diff_attributes(0, attrs_req_read); |
| 135 | log_msg(LOG_LEVEL_WARNING, |
| 136 | "failed to open %s '%s' for reading: %s: %s (%s%s%s%s)", |
| 137 | get_f_type_string_from_perm(entry->fs.st_mode), entry->filename, |
| 138 | failure_str, |
| 139 | error_str, |
| 140 | dir_rec ? "skipping recursion" : "", |
| 141 | dir_rec && attrs_req_read ? ", " : "", |
| 142 | attrs_req_read ? "disabling attrs requiring read permissions: " : "", |
| 143 | attrs_req_read ? attrs_str : "" |
| 144 | ); |
| 145 | free(error_str); |
| 146 | free(attrs_str); |
| 147 | entry->attrs &= ~attrs_req_read; |
| 148 | return false; |
| 149 | } else { |
| 150 | if (entry->fd >= 0) { |
| 151 | LOG_WHOAMI(LOG_LEVEL_TRACE, "%s> replace O_PATH fd %d with O_RDONLY fd %d", entry->filename, entry->fd, fd); |
| 152 | if (close(entry->fd) < 0) { |
| 153 | log_msg(LOG_LEVEL_WARNING, "close() failed for '%s' (fd: %d): %s", entry->filename, entry->fd, strerror(errno)); |
| 154 | } |
| 155 | } |
| 156 | entry->fd = fd; |
| 157 | return true; |
| 158 | } |
| 159 | } |
| 160 | return false; |
| 161 | } |
| 162 | |
| 163 | static void process_path(char *path, bool dry_run, const char *whoami) { |
| 164 | db_line *line = NULL; |
| 165 | |
| 166 | LOG_WHOAMI(LOG_LEVEL_DEBUG, "process '%s' (fullpath: '%s')", &path[conf->root_prefix_length], path); |
| 167 | |
| 168 | struct stat stat; |
| 169 | int fd = -1; |
| 170 | #ifdef O_PATH |
| 171 | fd = open(path, O_NOFOLLOW | O_PATH); |
| 172 | if (fd == -1) { |
| 173 | log_msg(LOG_LEVEL_WARNING, "failed to access '%s': %s (skipping)", path, strerror(errno)); |
| 174 | return; |
| 175 | } |
| 176 | LOG_WHOAMI(LOG_LEVEL_TRACE, "%s> open() returned O_PATH fd %d", path, fd); |
| 177 | if (fstat(fd, &stat) == -1) { |
| 178 | log_msg(LOG_LEVEL_WARNING, "fstat() failed for %s: %s (skipping)", path, strerror(errno)); |
| 179 | } else { |
| 180 | #else |
| 181 | if(lstat(path, &stat) == -1) { |
| 182 | log_msg(LOG_LEVEL_WARNING, "lstat() failed for '%s': %s (skipping)", path, strerror(errno)); |
| 183 | } else { |
| 184 | #endif |
| 185 | file_t file = { |
| 186 | .name = &path[conf->root_prefix_length], |
| 187 | .type = get_f_type_from_perm(stat.st_mode), |
| 188 | #ifdef HAVE_FSTYPE |
| 189 | .fs_type = 0UL, |
| 190 | #endif |
| 191 | }; |
| 192 | #ifdef HAVE_FSTYPE |
| 193 | struct statfs statfs; |
| 194 | if (fstatfs(fd, &statfs) == -1) { |
| 195 | log_msg(LOG_LEVEL_WARNING, "fstatfs() failed for %s: %s (file system type information missing)", path, strerror(errno)); |
| 196 | } else { |
| 197 | file.fs_type = statfs.f_type; |
| 198 | } |
| 199 | #endif |
| 200 | match_t path_match = check_rxtree(file, conf->tree, "disk", false, whoami); |
| 201 | char *attrs_str = NULL; |
| 202 | disk_entry entry = { |
| 203 | .filename = path, |
| 204 | .fs = stat, |
| 205 | .attrs = 0LLU, |
| 206 | #ifdef HAVE_FSTYPE |
| 207 | .fs_type = file.fs_type, |
| 208 | #endif |
| 209 | .fd = fd, |
| 210 | }; |
| 211 | if (!dry_run && path_match.result & (RESULT_SELECTIVE_MATCH|RESULT_EQUAL_MATCH)) { |
| 212 | entry.attrs = path_match.rule->attr; |
| 213 | |
| 214 | /* disable unsupported attributes */ |
| 215 | DB_ATTR_TYPE attrs_to_disable; |
| 216 | LOG_LEVEL log_level_unavailable = LOG_LEVEL_DEBUG; |
| 217 | if (!S_ISREG(stat.st_mode)) { |
| 218 | /* hashsum attributes */ |
| 219 | attrs_to_disable = entry.attrs & get_hashes(false); |
| 220 | if (attrs_to_disable) { |
| 221 | attrs_str = diff_attributes(0, attrs_to_disable); |
| 222 | LOG_WHOAMI(log_level_unavailable, "%s> disabling hashsum attribute(s) for non-regular file: %s)", |
| 223 | path, attrs_str); |
| 224 | free(attrs_str); |
| 225 | entry.attrs &= ~attrs_to_disable; |
| 226 | } |
| 227 | #ifdef WITH_CAPABILITIES |
| 228 | /* capability attribute */ |
| 229 | attrs_to_disable = entry.attrs & ATTR(attr_capabilities); |
| 230 | if (attrs_to_disable) { |
| 231 | LOG_WHOAMI(log_level_unavailable, "%s> disabling capability attribute for non-regular file", path); |
| 232 | entry.attrs &= ~attrs_to_disable; |
| 233 | } |
| 234 | #endif |
| 235 | } |
| 236 | if (!S_ISLNK(stat.st_mode)) { |
| 237 | /* linkname attribute */ |
| 238 | attrs_to_disable = entry.attrs & ATTR(attr_linkname); |
| 239 | if (attrs_to_disable) { |
| 240 | LOG_WHOAMI(log_level_unavailable, "%s> disabling linkname attribute for non-symlink file", path); |
| 241 | entry.attrs &= ~attrs_to_disable; |
| 242 | } |
| 243 | } |
| 244 | #ifdef WITH_E2FSATTRS |
| 245 | if (!(S_ISDIR(stat.st_mode) || S_ISREG(stat.st_mode))) { |
| 246 | /* e2fsattrs attribute */ |
| 247 | attrs_to_disable = entry.attrs & ATTR(attr_e2fsattrs); |
| 248 | if (attrs_to_disable) { |
| 249 | LOG_WHOAMI(log_level_unavailable, |
| 250 | "%s> disabling e2fsattrs attribute for non-directory/non-regular file", path); |
| 251 | entry.attrs &= ~attrs_to_disable; |
| 252 | } |
| 253 | } |
| 254 | #endif |
| 255 | } |
| 256 | if(!(entry.attrs&ATTR(attr_rdev))) { |
| 257 | entry.fs.st_rdev=0; |
| 258 | } |
| 259 | if (S_ISDIR(stat.st_mode)) { |
| 260 | const char * whoami_log_thread = whoami ? whoami : "(main)"; |
| 261 | DIR *dir = NULL; |
| 262 | switch (path_match.result) { |
| 263 | case RESULT_SELECTIVE_MATCH: |
| 264 | case RESULT_EQUAL_MATCH: |
| 265 | case RESULT_PARTIAL_MATCH: |
| 266 | case RESULT_RECURSIVE_NEGATIVE_MATCH: |
| 267 | case RESULT_PARTIAL_LIMIT_MATCH: |
| 268 | LOG_WHOAMI(LOG_LEVEL_DEBUG, "read directory contents of '%s' (reason: %s)", path, get_match_result_desc(path_match.result)); |
| 269 | if (open_for_reading(&entry, true, whoami)) { |
| 270 | int dupfd = dup(entry.fd); |
| 271 | if (dupfd == -1) { |
| 272 | log_msg(LOG_LEVEL_WARNING, "'%s': failed to duplicate file descriptor: %s", path, strerror(errno)); |
| 273 | } else { |
| 274 | if ((dir = fdopendir(dupfd)) == NULL) { |
| 275 | log_msg(LOG_LEVEL_WARNING, "failed to open directory '%s' for reading directory contents: %s (skipping recursion)", |
| 276 | path, strerror(errno)); |
| 277 | } else { |
| 278 | const struct dirent *entp = NULL; |
| 279 | while ((entp = readdir(dir)) != NULL) { |
| 280 | if (strcmp(entp->d_name, ".") != 0 && strcmp(entp->d_name, "..") != 0) { |
| 281 | char *entry_full_path = name_construct(path, entp->d_name); |
| 282 | log_msg(LOG_LEVEL_THREAD, |
| 283 | "%10s: add entry %p to queue of worker entries (filename: '%s')", whoami_log_thread, |
| 284 | (void *)entry_full_path, entry_full_path); |
| 285 | queue_ts_enqueue(queue_worker_entries, entry_full_path, whoami_log_thread); |
| 286 | } |
| 287 | } |
| 288 | if (closedir(dir) < 0) { |
| 289 | log_msg(LOG_LEVEL_WARNING, "closedir() failed for '%s': %s", path, strerror(errno)); |
| 290 | } |
| 291 | } |
| 292 | } |
| 293 | } |
| 294 | break; |
| 295 | case RESULT_NON_RECURSIVE_NEGATIVE_MATCH: |
| 296 | case RESULT_NEGATIVE_PARENT_MATCH: |
| 297 | case RESULT_NO_RULE_MATCH: |
| 298 | case RESULT_PART_LIMIT_AND_NO_RECURSE_MATCH: |
| 299 | LOG_WHOAMI(LOG_LEVEL_DEBUG, "do NOT read directory contents of '%s' (reason: %s)", path, get_match_result_desc(path_match.result)); |
| 300 | break; |
| 301 | case RESULT_NO_LIMIT_MATCH: |
| 302 | LOG_WHOAMI(LOG_LEVEL_LIMIT, "do NOT read directory contents of '%s' (reason: %s)", path, get_match_result_desc(path_match.result)); |
| 303 | break; |
| 304 | } |
| 305 | } |
| 306 | if (dry_run) { |
| 307 | print_match(file, path_match); |
| 308 | } else { |
| 309 | DB_ATTR_TYPE transition_hashsums = 0LL; |
| 310 | |
| 311 | if (path_match.result & (RESULT_SELECTIVE_MATCH|RESULT_EQUAL_MATCH)) { |
| 312 | if (S_ISREG(stat.st_mode)) { |
| 313 | if (open_for_reading(&entry, false, whoami)) { |
| 314 | if (conf->action & DO_COMPARE && entry.attrs & get_hashes(false)) { |
| 315 | const seltree *node = get_seltree_node(conf->tree, &path[conf->root_prefix_length]); |
| 316 | if (node && node->old_data) { |
| 317 | transition_hashsums = get_transition_hashsums( |
| 318 | (node->old_data)->filename, (node->old_data)->attr, &path[conf->root_prefix_length], entry.attrs); |
| 319 | } |
| 320 | } |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | attrs_str = diff_attributes(0, entry.attrs); |
| 325 | LOG_WHOAMI(LOG_LEVEL_DEBUG, "%s> requested attributes: %s", entry.filename, attrs_str); |
| 326 | free(attrs_str); |
| 327 | |
| 328 | line = get_file_attrs(&entry, entry.attrs, transition_hashsums, whoami); |
| 329 | |
| 330 | /* attr_filename is always needed/returned but never requested */ |
| 331 | DB_ATTR_TYPE returned_attr = (~ATTR(attr_filename) & line->attr); |
| 332 | attrs_str = diff_attributes(0, returned_attr); |
| 333 | LOG_WHOAMI(LOG_LEVEL_DEBUG, "%s> returned attributes: %llu (%s)", entry.filename, returned_attr, attrs_str); |
| 334 | free(attrs_str); |
| 335 | if (returned_attr ^ entry.attrs) { |
| 336 | attrs_str = diff_attributes(entry.attrs, returned_attr); |
| 337 | LOG_WHOAMI(LOG_LEVEL_DEBUG, "%s> requested (%llu) and returned (%llu) attributes are not equal: %s", entry.filename, entry.attrs, returned_attr, attrs_str); |
| 338 | free(attrs_str); |
| 339 | } |
| 340 | |
| 341 | add_file_to_tree(conf->tree, line, DB_NEW | DB_DISK, NULL, &entry, whoami); |
| 342 | } |
| 343 | } |
| 344 | if (entry.fd != -1) { |
| 345 | if (close(entry.fd) < 0) { |
| 346 | log_msg(LOG_LEVEL_WARNING, "close() failed for '%s': %s", path, strerror(errno)); |
| 347 | } |
| 348 | } |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | static void process_disk_entries(bool dry_run, const char *whoami) { |
| 353 | const char * whoami_log_thread = whoami ? whoami : "(main)"; |
| 354 | while (1) { |
| 355 | log_msg(LOG_LEVEL_THREAD, "%10s: process_disk_entries: wait for entries", whoami_log_thread); |
| 356 | char *data = queue_ts_dequeue_wait(queue_worker_entries, whoami_log_thread); |
| 357 | if (data) { |
| 358 | log_msg(LOG_LEVEL_THREAD, "%10s: process_disk_entries: got entry %p from queue of worker entries (path: '%s')", whoami_log_thread, (void*) data, data); |
| 359 | process_path(data, dry_run, whoami); |
| 360 | free(data); |
| 361 | } else { |
| 362 | log_msg(LOG_LEVEL_THREAD, "%10s: process_disk_entries: queue empty", whoami_log_thread); |
| 363 | break; |
| 364 | } |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | static void * worker(void *arg) { |
| 369 | struct worker_args args = *(struct worker_args *)arg; |
| 370 | char whoami[32]; |
| 371 | snprintf(whoami, 32, "(work-%03li)", args.worker_index); |
| 372 | |
| 373 | mask_sig(whoami); |
| 374 | |
| 375 | queue_ts_register(queue_worker_entries, whoami); |
| 376 | |
| 377 | log_msg(LOG_LEVEL_THREAD, "%10s: worker: initialized worker thread #%ld", whoami, args.worker_index); |
| 378 | |
| 379 | process_disk_entries(args.dry_run, whoami); |
| 380 | |
| 381 | log_msg(LOG_LEVEL_THREAD, "%10s: worker: exit thread", whoami); |
| 382 | return (void *) pthread_self(); |
| 383 | } |
| 384 | |
| 385 | void db_scan_disk(bool dry_run) { |
| 386 | const char *whoami_main = "(main)"; |
| 387 | |
| 388 | char* full_path=checked_malloc((conf->root_prefix_length+2)*sizeof(char)); /* freed in process_disk_entries() */ |
| 389 | strncpy(full_path, conf->root_prefix, conf->root_prefix_length+1); |
| 390 | strcat (full_path, "/"); |
| 391 | |
| 392 | if (dry_run || conf->num_workers == 0) { |
| 393 | queue_worker_entries = queue_ts_init(); /* freed below */ |
| 394 | queue_ts_enqueue(queue_worker_entries, full_path, whoami_main); |
| 395 | queue_ts_release(queue_worker_entries, whoami_main); |
| 396 | |
| 397 | process_disk_entries(dry_run, NULL); |
| 398 | |
| 399 | queue_ts_free(queue_worker_entries); |
| 400 | } else { |
| 401 | queue_worker_entries = queue_ts_init(); /* freed below */ |
| 402 | log_msg(LOG_LEVEL_THREAD, "%10s: initialized worker entries queue %p", whoami_main, (void*) queue_worker_entries); |
| 403 | |
| 404 | worker_thread *worker_threads = checked_malloc(conf->num_workers * sizeof(worker_thread)); /* freed below */ |
| 405 | |
| 406 | for (int i = 0 ; i < conf->num_workers ; ++i) { |
| 407 | worker_threads[i].args.worker_index = i + 1L; |
| 408 | worker_threads[i].args.dry_run = dry_run; |
| 409 | if (pthread_create(&worker_threads[i].thread, NULL, &worker, (void *) &worker_threads[i].args) != 0) { |
| 410 | log_msg(LOG_LEVEL_ERROR, "failed to start file attributes worker thread #%d", i+1); |
| 411 | exit(THREAD_ERROR); |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | queue_ts_enqueue(queue_worker_entries, full_path, whoami_main); |
| 416 | queue_ts_release(queue_worker_entries, whoami_main); |
| 417 | |
| 418 | log_msg(LOG_LEVEL_THREAD, "%10s: wait for worker threads to be finished", whoami_main); |
| 419 | for (int i = 0 ; i < conf->num_workers ; ++i) { |
| 420 | if (pthread_join(worker_threads[i].thread, NULL) != 0) { |
| 421 | log_msg(LOG_LEVEL_WARNING, "failed to join file attributes thread #%d", i); |
| 422 | } |
| 423 | log_msg(LOG_LEVEL_THREAD, "%10s: worker thread #%d finished", whoami_main, i); |
| 424 | } |
| 425 | free(worker_threads); |
| 426 | queue_ts_free(queue_worker_entries); |
| 427 | } |
| 428 | } |