clamd/onaccess_scth.c
7ee85372
 /*
c442ca9c
  *  Copyright (C) 2015-2019 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
7ee85372
  *
  *  Authors: Mickey Sola
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License version 2 as
  *  published by the Free Software Foundation.
  *
  *  This program is distributed in the hope that it will be useful,
  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  *  GNU General Public License for more details.
  *
  *  You should have received a copy of the GNU General Public License
  *  along with this program; if not, write to the Free Software
  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  *  MA 02110-1301, USA.
  */
 
 #if HAVE_CONFIG_H
 #include "clamav-config.h"
 #endif
 
 #if defined(FANOTIFY)
 
 #include <stdio.h>
 #include <unistd.h>
 #include <string.h>
 #include <fcntl.h>
 #include <signal.h>
 #include <pthread.h>
 
 #include "shared/optparser.h"
 #include "shared/output.h"
 
 #include "others.h"
9a1b6ef5
 #include "priv_fts.h"
d39cb658
 #include "onaccess_others.h"
7ee85372
 #include "onaccess_scth.h"
d7979d4f
 #include "onaccess_others.h"
7ee85372
 
b71960cd
 #include "libclamav/clamav.h"
 
 
 static int onas_scth_scanfile(const char *fname, int fd, int extinfo, struct scth_thrarg *tharg);
 static int onas_scth_handle_dir(const char *pathname, struct scth_thrarg *tharg);
 static int onas_scth_handle_file(const char *pathname, struct scth_thrarg *tharg);
d98d6fdb
 
 static void onas_scth_exit(int sig);
 
7ee85372
 static void onas_scth_exit(int sig) {
 	logg("*ScanOnAccess: onas_scth_exit(), signal %d\n", sig);
 
 	pthread_exit(NULL);
 }
 
b71960cd
 static int onas_scth_scanfile(const char *fname, int fd, int extinfo, struct scth_thrarg *tharg)
 {
     int ret = 0;
e551468a
     const char *virname = NULL;
b71960cd
 
     return onas_scan(fname, fd, &virname, tharg->engine, tharg->options, extinfo);
 }
 
 static int onas_scth_handle_dir(const char *pathname, struct scth_thrarg *tharg) {
7ee85372
 	FTS *ftsp = NULL;
674a4aa5
 	int fd;
7ee85372
 	int ftspopts = FTS_PHYSICAL | FTS_XDEV;
674a4aa5
 	int extinfo;
 	int ret;
7ee85372
 	FTSENT *curr = NULL;
 
674a4aa5
 	extinfo = optget(tharg->opts, "ExtendedDetectionInfo")->enabled;
b71960cd
 
d98d6fdb
 	char *const pathargv[] = { (char *) pathname, NULL };
b71960cd
 	if (!(ftsp = _priv_fts_open(pathargv, ftspopts, NULL))) return CL_EOPEN;
7ee85372
 
9a1b6ef5
 	while ((curr = _priv_fts_read(ftsp))) {
7ee85372
 		if (curr->fts_info != FTS_D) {
b71960cd
 			if ((fd = safe_open(curr->fts_path, O_RDONLY | O_BINARY)) == -1)
                             return CL_EOPEN;
 
                         if (onas_scth_scanfile(curr->fts_path, fd, extinfo, tharg) == CL_VIRUS);
                             ret = CL_VIRUS;
 
 			close(fd);
7ee85372
 		}
 	}
 
b71960cd
 	return ret;
7ee85372
 }
 
 
b71960cd
 static int onas_scth_handle_file(const char *pathname, struct scth_thrarg *tharg) {
 	int fd;
674a4aa5
 	int extinfo;
 	int ret;
b71960cd
 
 	if (!pathname) return CL_ENULLARG;
7ee85372
 
674a4aa5
 	extinfo = optget(tharg->opts, "ExtendedDetectionInfo")->enabled;
7ee85372
 
b71960cd
 	if ((fd = safe_open(pathname, O_RDONLY | O_BINARY)) == -1)
 		return CL_EOPEN;
 	ret = onas_scth_scanfile(pathname, fd, extinfo, tharg);
674a4aa5
 
 	close(fd);
b71960cd
 
 	return ret;
7ee85372
 }
 
 void *onas_scan_th(void *arg) {
 	struct scth_thrarg *tharg = (struct scth_thrarg *) arg;
 	sigset_t sigset;
 	struct sigaction act;
 
 	/* ignore all signals except SIGUSR1 */
 	sigfillset(&sigset);
 	sigdelset(&sigset, SIGUSR1);
 	/* The behavior of a process is undefined after it ignores a
 	 * SIGFPE, SIGILL, SIGSEGV, or SIGBUS signal */
 	sigdelset(&sigset, SIGFPE);
 	sigdelset(&sigset, SIGILL);
 	sigdelset(&sigset, SIGSEGV);
 #ifdef SIGBUS
 	sigdelset(&sigset, SIGBUS);
 #endif
 	pthread_sigmask(SIG_SETMASK, &sigset, NULL);
 	memset(&act, 0, sizeof(struct sigaction));
 	act.sa_handler = onas_scth_exit;
 	sigfillset(&(act.sa_mask));
 	sigaction(SIGUSR1, &act, NULL);
 	sigaction(SIGSEGV, &act, NULL);
 
d7979d4f
 	if (NULL == tharg || NULL == tharg->pathname || NULL == tharg->opts || NULL == tharg->engine) {
 		logg("ScanOnAccess: Invalid thread arguments for extra scanning\n");
 		goto done;
 	}
 
 	if (tharg->extra_options & ONAS_SCTH_ISDIR) {
5bc6fdb8
 		logg("*ScanOnAccess: Performing additional scanning on directory '%s'\n", tharg->pathname);
b71960cd
 		onas_scth_handle_dir(tharg->pathname, tharg);
d7979d4f
 	} else if (tharg->extra_options & ONAS_SCTH_ISFILE) {
5bc6fdb8
 		logg("*ScanOnAccess: Performing additional scanning on file '%s'\n", tharg->pathname);
b71960cd
 		onas_scth_handle_file(tharg->pathname, tharg);
7ee85372
 	}
 
d7979d4f
 done:
 	if (NULL != tharg->pathname){
 		free(tharg->pathname);
 		tharg->pathname = NULL;
 	}
 	if (NULL != tharg->options) {
 		free(tharg->options);
 		tharg->options = NULL;
 	}
 	if (NULL != tharg) {
 		free(tharg);
 	}
7ee85372
 
 	return NULL;
 }
 #endif