Server IP : 103.169.32.36 / Your IP : 216.73.217.13 Web Server : Apache System : Linux web.dpmptsp 3.10.0-1160.119.1.el7.x86_64 #1 SMP Tue Jun 4 14:43:51 UTC 2024 x86_64 User : apache ( 48) PHP Version : 5.6.40 Disable Function : NONE MySQL : ON | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : ON Directory : /var/opt/eset/efs/eventd/eset_rtp/ |
Upload File : |
| Current File : /var/opt/eset/efs/eventd/eset_rtp/ertp_excludes.c |
/*
* eset_rtp (ESET Real-time file system protection module)
* Copyright (C) 1992-2023 ESET, spol. s r.o.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* 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, see <https://www.gnu.org/licenses/>.
*
* In case of any questions, you can contact us at ESET, spol. s r.o., Einsteinova 24, 851 01 Bratislava, Slovakia.
*/
#include "ertp_excludes.h"
#include "ertp_logs.h"
#include <linux/rwsem.h>
#include <linux/slab.h>
#include <linux/version.h>
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 8, 0)
#include <linux/mmap_lock.h>
#endif
struct ertp_excludes_path {
struct ertp_excludes_path *next;
char *path_name;
size_t path_len;
};
struct ertp_excludes_container {
struct ertp_excludes_path *list;
struct rw_semaphore lock;
};
static struct ertp_excludes_container ertp_excludes_proc_default;
static struct ertp_excludes_container ertp_excludes_proc_user;
static struct ertp_excludes_container ertp_excludes_file_default;
static struct ertp_excludes_container ertp_excludes_file_user;
static struct ertp_excludes_path *ertp_excludes_path_alloc(char *path_name) {
struct ertp_excludes_path *path;
if (path_name == NULL || path_name[0] == '\0') return ERR_PTR(-EINVAL);
path = kzalloc(sizeof(struct ertp_excludes_path), GFP_KERNEL);
if (!path) return ERR_PTR(-ENOMEM);
path->next = NULL;
path->path_name = path_name;
path->path_len = strlen(path_name);
return path;
}
static void ertp_excludes_path_free(struct ertp_excludes_path *path) {
if (!path || IS_ERR(path)) return;
while (path) {
struct ertp_excludes_path *next = path->next;
if (path->path_name) kfree(path->path_name);
kfree(path);
path = next;
}
}
static void ertp_excludes_container_init(
struct ertp_excludes_container *excludes) {
excludes->list = NULL;
init_rwsem(&excludes->lock);
}
static void ertp_excludes_container_deinit(
struct ertp_excludes_container *excludes) {
down_write(&excludes->lock);
ertp_excludes_path_free(excludes->list);
excludes->list = NULL;
up_write(&excludes->lock);
}
void ertp_excludes_init(void) {
ertp_excludes_container_init(&ertp_excludes_proc_default);
ertp_excludes_container_init(&ertp_excludes_proc_user);
ertp_excludes_container_init(&ertp_excludes_file_default);
ertp_excludes_container_init(&ertp_excludes_file_user);
}
void ertp_excludes_deinit(void) {
ertp_excludes_container_deinit(&ertp_excludes_proc_default);
ertp_excludes_container_deinit(&ertp_excludes_proc_user);
ertp_excludes_container_deinit(&ertp_excludes_file_default);
ertp_excludes_container_deinit(&ertp_excludes_file_user);
}
#ifdef ERTP_DEBUG
static void print_list_items(const struct ertp_excludes_path *excl_list) {
const struct ertp_excludes_path *path = excl_list;
while (path) {
ertp_pr_log(ERTP_LOG_EXCLUSIONS, "%s", path->path_name);
path = path->next;
}
}
#endif
static void ertp_excludes_swap(struct ertp_excludes_path **old_list,
struct ertp_excludes_path **new_list) {
struct ertp_excludes_path *tmp = *old_list;
#ifdef ERTP_DEBUG
ertp_pr_log(ERTP_LOG_EXCLUSIONS, "old excludes:");
print_list_items(*old_list);
#endif
*old_list = *new_list;
*new_list = tmp;
#ifdef ERTP_DEBUG
ertp_pr_log(ERTP_LOG_EXCLUSIONS, "new excludes:");
print_list_items(*old_list);
#endif
}
static int ertp_excl_add_paths(char **buf, uint32_t size,
struct ertp_excludes_container *excludes) {
int err = 0;
int i;
struct ertp_excludes_path *tmp_list_head = NULL;
for (i = 0; i < size; ++i) {
struct ertp_excludes_path *item = ertp_excludes_path_alloc(buf[i]);
if (IS_ERR(item)) {
err = PTR_ERR(item);
goto end;
}
buf[i] = NULL;
item->next = tmp_list_head;
tmp_list_head = item;
ertp_pr_log(ERTP_LOG_EXCLUSIONS, "path %s added to excludes",
item->path_name);
}
down_write(&excludes->lock);
ertp_excludes_swap(&excludes->list, &tmp_list_head);
up_write(&excludes->lock);
end:
ertp_excludes_path_free(tmp_list_head);
return err;
}
int ertp_proc_default_excludes_add(char **paths, uint32_t size) {
return ertp_excl_add_paths(paths, size, &ertp_excludes_proc_default);
}
int ertp_proc_user_excludes_add(char **paths, uint32_t size) {
return ertp_excl_add_paths(paths, size, &ertp_excludes_proc_user);
}
int ertp_file_default_excludes_add(char **paths, uint32_t size) {
return ertp_excl_add_paths(paths, size, &ertp_excludes_file_default);
}
int ertp_file_user_excludes_add(char **paths, uint32_t size) {
return ertp_excl_add_paths(paths, size, &ertp_excludes_file_user);
}
static bool ertp_path_excluded(const char *path_name,
struct ertp_excludes_container *excludes) {
const struct ertp_excludes_path *item;
bool found = false;
down_read(&excludes->lock);
item = excludes->list;
while (item) {
if (item->path_len) {
BUG_ON(item->path_name == NULL);
if (item->path_name[item->path_len - 1] == '/') {
if (!strncmp(item->path_name, path_name, item->path_len)) {
found = true;
break;
}
} else if (!strcmp(item->path_name, path_name)) {
found = true;
break;
}
}
item = item->next;
}
up_read(&excludes->lock);
return found;
}
bool ertp_proc_excluded_by_default(const char *process_path) {
BUG_ON(!process_path);
return ertp_path_excluded(process_path, &ertp_excludes_proc_default);
}
bool ertp_proc_excluded(const char *process_path) {
BUG_ON(!process_path);
return ertp_proc_excluded_by_default(process_path) ||
ertp_path_excluded(process_path, &ertp_excludes_proc_user);
}
bool ertp_file_excluded_by_default(const char *file_path) {
BUG_ON(!file_path);
return ertp_path_excluded(file_path, &ertp_excludes_file_default);
}
bool ertp_file_excluded(const char *file_path) {
BUG_ON(!file_path);
return ertp_file_excluded_by_default(file_path) ||
ertp_path_excluded(file_path, &ertp_excludes_file_user);
}