| 1 |
/* |
| 2 |
* Copyright (C) 2019-2024 Cisco Systems, Inc. and/or its affiliates. All rights reserved. |
| 3 |
* |
| 4 |
* Authors: Mickey Sola |
| 5 |
* |
| 6 |
* This program is free software; you can redistribute it and/or modify |
| 7 |
* it under the terms of the GNU General Public License version 2 as |
| 8 |
* published by the Free Software Foundation. |
| 9 |
* |
| 10 |
* This program is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License |
| 16 |
* along with this program; if not, write to the Free Software |
| 17 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
| 18 |
* MA 02110-1301, USA. |
| 19 |
*/ |
| 20 |
|
| 21 |
#if HAVE_CONFIG_H |
| 22 |
#include "clamav-config.h" |
| 23 |
#endif |
| 24 |
|
| 25 |
#if defined(HAVE_SYS_FANOTIFY_H) |
| 26 |
|
| 27 |
#include <stdio.h> |
| 28 |
#include <unistd.h> |
| 29 |
#include <sys/types.h> |
| 30 |
#include <sys/stat.h> |
| 31 |
#include <fcntl.h> |
| 32 |
#include <pthread.h> |
| 33 |
#include <string.h> |
| 34 |
#include <errno.h> |
| 35 |
#include <time.h> |
| 36 |
|
| 37 |
#include <sys/fanotify.h> |
| 38 |
|
| 39 |
// libclamav |
| 40 |
#include "clamav.h" |
| 41 |
#include "scanners.h" |
| 42 |
|
| 43 |
// common |
| 44 |
#include "optparser.h" |
| 45 |
#include "output.h" |
| 46 |
|
| 47 |
// clamd |
| 48 |
#include "server.h" |
| 49 |
|
| 50 |
#include "../inotif/hash.h" |
| 51 |
#include "../inotif/inotif.h" |
| 52 |
|
| 53 |
#include "../client/client.h" |
| 54 |
|
| 55 |
#include "../scan/thread.h" |
| 56 |
#include "../scan/onas_queue.h" |
| 57 |
|
| 58 |
#include "../misc/utils.h" |
| 59 |
|
| 60 |
#include "fanotif.h" |
| 61 |
|
| 62 |
extern pthread_t ddd_pid; |
| 63 |
extern pthread_t scan_queue_pid; |
| 64 |
static int onas_fan_fd; |
| 65 |
|
| 66 |
cl_error_t onas_setup_fanotif(struct onas_context **ctx) |
| 67 |
{ |
| 68 |
|
| 69 |
const struct optstruct *pt; |
| 70 |
uint64_t fan_mask = FAN_EVENT_ON_CHILD; |
| 71 |
|
| 72 |
const struct optstruct *pt_tmpdir; |
| 73 |
const char *clamd_tmpdir; |
| 74 |
|
| 75 |
ddd_pid = 0; |
| 76 |
|
| 77 |
if (!ctx || !*ctx) { |
| 78 |
logg(LOGG_ERROR, "ClamFanotif: unable to start clamonacc. (bad context)\n"); |
| 79 |
return CL_EARG; |
| 80 |
} |
| 81 |
|
| 82 |
onas_fan_fd = (*ctx)->fan_fd; |
| 83 |
(*ctx)->fan_mask = fan_mask; |
| 84 |
|
| 85 |
if (optget((*ctx)->clamdopts, "OnAccessPrevention")->enabled && !optget((*ctx)->clamdopts, "OnAccessMountPath")->enabled) { |
| 86 |
logg(LOGG_DEBUG, "ClamFanotif: kernel-level blocking feature enabled ... preventing malicious files access attempts\n"); |
| 87 |
(*ctx)->fan_mask |= FAN_ACCESS_PERM | FAN_OPEN_PERM; |
| 88 |
} else { |
| 89 |
logg(LOGG_DEBUG, "ClamFanotif: kernel-level blocking feature disabled ...\n"); |
| 90 |
if (optget((*ctx)->clamdopts, "OnAccessPrevention")->enabled && optget((*ctx)->clamdopts, "OnAccessMountPath")->enabled) { |
| 91 |
logg(LOGG_DEBUG, "ClamFanotif: feature not available when watching mounts ... \n"); |
| 92 |
} |
| 93 |
(*ctx)->fan_mask |= FAN_ACCESS | FAN_OPEN; |
| 94 |
} |
| 95 |
|
| 96 |
pt_tmpdir = optget((*ctx)->clamdopts, "TemporaryDirectory"); |
| 97 |
if (pt_tmpdir->enabled) { |
| 98 |
clamd_tmpdir = pt_tmpdir->strarg; |
| 99 |
} else { |
| 100 |
clamd_tmpdir = cli_gettmpdir(); |
| 101 |
} |
| 102 |
|
| 103 |
if ((pt = optget((*ctx)->clamdopts, "OnAccessMountPath"))->enabled) { |
| 104 |
while (pt) { |
| 105 |
if (fanotify_mark(onas_fan_fd, FAN_MARK_ADD | FAN_MARK_MOUNT, (*ctx)->fan_mask, (*ctx)->fan_fd, pt->strarg) != 0) { |
| 106 |
logg(LOGG_ERROR, "ClamFanotif: can't include mountpoint '%s'\n", pt->strarg); |
| 107 |
return CL_EARG; |
| 108 |
} else { |
| 109 |
logg(LOGG_DEBUG, "ClamFanotif: recursively watching the mount point '%s'\n", pt->strarg); |
| 110 |
} |
| 111 |
pt = (struct optstruct *)pt->nextarg; |
| 112 |
} |
| 113 |
|
| 114 |
} else if (!optget((*ctx)->clamdopts, "OnAccessDisableDDD")->enabled) { |
| 115 |
(*ctx)->ddd_enabled = 1; |
| 116 |
} else { |
| 117 |
if ((pt = optget((*ctx)->clamdopts, "OnAccessIncludePath"))->enabled) { |
| 118 |
while (pt) { |
| 119 |
if (0 == strcmp(clamd_tmpdir, pt->strarg)) { |
| 120 |
logg(LOGG_ERROR, "ClamFanotif: Not watching path '%s'\n", pt->strarg); |
| 121 |
logg(LOGG_ERROR, "ClamFanotif: ClamOnAcc should not watch the directory clamd is using for temp files\n"); |
| 122 |
logg(LOGG_ERROR, "ClamFanotif: Consider setting TemporaryDirectory in clamd.conf to a different directory.\n"); |
| 123 |
pt = (struct optstruct *)pt->nextarg; |
| 124 |
continue; |
| 125 |
} |
| 126 |
|
| 127 |
if (fanotify_mark(onas_fan_fd, FAN_MARK_ADD, (*ctx)->fan_mask, (*ctx)->fan_fd, pt->strarg) != 0) { |
| 128 |
logg(LOGG_ERROR, "ClamFanotif: can't include path '%s'\n", pt->strarg); |
| 129 |
return CL_EARG; |
| 130 |
} else { |
| 131 |
logg(LOGG_DEBUG, "ClamFanotif: watching directory '%s' (non-recursively)\n", pt->strarg); |
| 132 |
} |
| 133 |
pt = (struct optstruct *)pt->nextarg; |
| 134 |
} |
| 135 |
} else { |
| 136 |
logg(LOGG_ERROR, "ClamFanotif: please specify at least one path with OnAccessIncludePath\n"); |
| 137 |
return CL_EARG; |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
/* Load other options. */ |
| 142 |
(*ctx)->sizelimit = optget((*ctx)->clamdopts, "OnAccessMaxFileSize")->numarg; |
| 143 |
if ((*ctx)->sizelimit) { |
| 144 |
logg(LOGG_DEBUG, "ClamFanotif: max file size limited to %lu bytes\n", (*ctx)->sizelimit); |
| 145 |
} else { |
| 146 |
logg(LOGG_DEBUG, "ClamFanotif: file size limit disabled\n"); |
| 147 |
} |
| 148 |
|
| 149 |
return CL_SUCCESS; |
| 150 |
} |
| 151 |
|
| 152 |
int onas_fan_eloop(struct onas_context **ctx) |
| 153 |
{ |
| 154 |
int ret = 0; |
| 155 |
int err_cnt = 0; |
| 156 |
short int scan; |
| 157 |
fd_set rfds; |
| 158 |
char buf[4096]; |
| 159 |
ssize_t bread; |
| 160 |
struct fanotify_event_metadata *fmd; |
| 161 |
char proc_fd_fname[1024]; |
| 162 |
char fname[1024]; |
| 163 |
int len, check; |
| 164 |
|
| 165 |
FD_ZERO(&rfds); |
| 166 |
FD_SET((*ctx)->fan_fd, &rfds); |
| 167 |
|
| 168 |
logg(LOGG_DEBUG, "ClamFanotif: starting fanotify event loop with process id (%d) ... \n", getpid()); |
| 169 |
do { |
| 170 |
ret = select((*ctx)->fan_fd + 1, &rfds, NULL, NULL, NULL); |
| 171 |
} while ((ret == -1 && errno == EINTR)); |
| 172 |
|
| 173 |
time_t start = time(NULL) - 30; |
| 174 |
while (((bread = read((*ctx)->fan_fd, buf, sizeof(buf))) > 0) || (errno == EOVERFLOW || errno == EMFILE || errno == EACCES)) { |
| 175 |
switch (errno) { |
| 176 |
case EOVERFLOW: |
| 177 |
if (time(NULL) - start >= 30) { |
| 178 |
logg(LOGG_DEBUG, "ClamFanotif: internal error (failed to read data) ... %s\n", strerror(errno)); |
| 179 |
logg(LOGG_DEBUG, "ClamFanotif: file too large for fanotify ... recovering and continuing scans...\n"); |
| 180 |
start = time(NULL); |
| 181 |
} |
| 182 |
|
| 183 |
errno = 0; |
| 184 |
continue; |
| 185 |
case EACCES: |
| 186 |
logg(LOGG_DEBUG, "ClamFanotif: internal error (failed to read data) ... %s\n", strerror(errno)); |
| 187 |
logg(LOGG_DEBUG, "ClamFanotif: check your SELinux audit logs and consider adding an exception \ |
| 188 |
... recovering and continuing scans...\n"); |
| 189 |
|
| 190 |
errno = 0; |
| 191 |
continue; |
| 192 |
case EMFILE: |
| 193 |
logg(LOGG_DEBUG, "ClamFanotif: internal error (failed to read data) ... %s\n", strerror(errno)); |
| 194 |
logg(LOGG_DEBUG, "ClamFanotif: waiting for consumer thread to catch up then retrying ...\n"); |
| 195 |
sleep(3); |
| 196 |
|
| 197 |
errno = 0; |
| 198 |
continue; |
| 199 |
default: |
| 200 |
break; |
| 201 |
} |
| 202 |
|
| 203 |
fmd = (struct fanotify_event_metadata *)buf; |
| 204 |
while (FAN_EVENT_OK(fmd, bread)) { |
| 205 |
if (fmd->vers != FANOTIFY_METADATA_VERSION) { |
| 206 |
logg(LOGG_ERROR, "ClamFanotif: Mismatch of fanotify metadata version.\n"); |
| 207 |
return 2; |
| 208 |
} |
| 209 |
scan = 1; |
| 210 |
if (fmd->fd >= 0) { |
| 211 |
sprintf(proc_fd_fname, "/proc/self/fd/%d", fmd->fd); |
| 212 |
errno = 0; |
| 213 |
len = readlink(proc_fd_fname, fname, sizeof(fname) - 1); |
| 214 |
if (len == -1) { |
| 215 |
close(fmd->fd); |
| 216 |
logg(LOGG_ERROR, "ClamFanotif: internal error (readlink() failed), %d, %s\n", fmd->fd, strerror(errno)); |
| 217 |
if (errno == EBADF) { |
| 218 |
logg(LOGG_INFO, "ClamWorker: fd already closed ... recovering ...\n"); |
| 219 |
fmd = FAN_EVENT_NEXT(fmd, bread); |
| 220 |
continue; |
| 221 |
} else { |
| 222 |
return 2; |
| 223 |
} |
| 224 |
} |
| 225 |
fname[len] = '\0'; |
| 226 |
|
| 227 |
if ((check = onas_fan_checkowner(fmd->pid, (*ctx)->clamdopts))) { |
| 228 |
scan = 0; |
| 229 |
if (check != CHK_SELF) { |
| 230 |
logg(LOGG_DEBUG, "ClamFanotif: %s skipped (excluded UID)\n", fname); |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
if (scan) { |
| 235 |
struct onas_scan_event *event_data; |
| 236 |
|
| 237 |
event_data = calloc(1, sizeof(struct onas_scan_event)); |
| 238 |
if (NULL == event_data) { |
| 239 |
close(fmd->fd); |
| 240 |
logg(LOGG_ERROR, "ClamFanotif: could not allocate memory for event data struct\n"); |
| 241 |
return 2; |
| 242 |
} |
| 243 |
|
| 244 |
/* general mapping */ |
| 245 |
onas_map_context_info_to_event_data(*ctx, &event_data); |
| 246 |
scan ? event_data->bool_opts |= ONAS_SCTH_B_SCAN : scan; |
| 247 |
|
| 248 |
/* fanotify specific stuffs */ |
| 249 |
event_data->bool_opts |= ONAS_SCTH_B_FANOTIFY; |
| 250 |
event_data->fmd = malloc(sizeof(struct fanotify_event_metadata)); |
| 251 |
if (NULL == event_data->fmd) { |
| 252 |
close(fmd->fd); |
| 253 |
free(event_data); |
| 254 |
logg(LOGG_ERROR, "ClamFanotif: could not allocate memory for event data struct fmd\n"); |
| 255 |
return 2; |
| 256 |
} |
| 257 |
memcpy(event_data->fmd, fmd, sizeof(struct fanotify_event_metadata)); |
| 258 |
event_data->pathname = cli_safer_strdup(fname); |
| 259 |
if (NULL == event_data->pathname) { |
| 260 |
close(fmd->fd); |
| 261 |
free(event_data->fmd); |
| 262 |
free(event_data); |
| 263 |
logg(LOGG_ERROR, "ClamFanotif: could not allocate memory for event data struct pathname\n"); |
| 264 |
return 2; |
| 265 |
} |
| 266 |
|
| 267 |
logg(LOGG_DEBUG, "ClamFanotif: attempting to feed consumer queue\n"); |
| 268 |
/* feed consumer queue */ |
| 269 |
if (CL_SUCCESS != onas_queue_event(event_data)) { |
| 270 |
close(fmd->fd); |
| 271 |
free(event_data->pathname); |
| 272 |
free(event_data->fmd); |
| 273 |
free(event_data); |
| 274 |
logg(LOGG_ERROR, "ClamFanotif: error occurred while feeding consumer queue ... \n"); |
| 275 |
if ((*ctx)->retry_on_error) { |
| 276 |
err_cnt++; |
| 277 |
if (err_cnt < (*ctx)->retry_attempts) { |
| 278 |
logg(LOGG_INFO, "ClamFanotif: ... recovering ...\n"); |
| 279 |
fmd = FAN_EVENT_NEXT(fmd, bread); |
| 280 |
continue; |
| 281 |
} |
| 282 |
} |
| 283 |
return 2; |
| 284 |
} |
| 285 |
} else { |
| 286 |
if (fmd->mask & FAN_ALL_PERM_EVENTS) { |
| 287 |
struct fanotify_response res; |
| 288 |
|
| 289 |
res.fd = fmd->fd; |
| 290 |
res.response = FAN_ALLOW; |
| 291 |
|
| 292 |
if (-1 == write((*ctx)->fan_fd, &res, sizeof(res))) { |
| 293 |
close(fmd->fd); |
| 294 |
logg(LOGG_ERROR, "ClamFanotif: error occurred while excluding event\n"); |
| 295 |
return 2; |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
if (-1 == close(fmd->fd)) { |
| 300 |
logg(LOGG_ERROR, "ClamFanotif: error occurred while closing metadata fd, %d\n", fmd->fd); |
| 301 |
if (errno == EBADF) { |
| 302 |
logg(LOGG_INFO, "ClamFanotif: fd already closed ... recovering ...\n"); |
| 303 |
} else { |
| 304 |
return 2; |
| 305 |
} |
| 306 |
} |
| 307 |
} |
| 308 |
} |
| 309 |
fmd = FAN_EVENT_NEXT(fmd, bread); |
| 310 |
} |
| 311 |
do { |
| 312 |
ret = select((*ctx)->fan_fd + 1, &rfds, NULL, NULL, NULL); |
| 313 |
} while ((ret == -1 && errno == EINTR)); |
| 314 |
} |
| 315 |
|
| 316 |
if (bread < 0) { |
| 317 |
logg(LOGG_ERROR, "ClamFanotif: internal error (failed to read data) ... %s\n", strerror(errno)); |
| 318 |
return 2; |
| 319 |
} |
| 320 |
|
| 321 |
return ret; |
| 322 |
} |
| 323 |
#endif |