From c9443892c2f82ac034ac0dad0d7a18030c2f3a7e Mon Sep 17 00:00:00 2001 From: liqiang Date: Thu, 28 Dec 2023 16:33:31 +0800 Subject: [PATCH] optimized uds whitelist logic Signed-off-by: liqiang --- qtfs/include/conn.h | 24 ++++++++++++++++++++++++ qtfs/qtfs_common/conn.c | 7 ++----- qtfs/qtfs_server/fsops.c | 25 ------------------------- 3 files changed, 26 insertions(+), 30 deletions(-) diff --git a/qtfs/include/conn.h b/qtfs/include/conn.h index 1ac44eb..5f74a46 100644 --- a/qtfs/include/conn.h +++ b/qtfs/include/conn.h @@ -75,6 +75,30 @@ static inline bool qtfs_support_epoll(umode_t mode) return (qtfs_epoll_mode || S_ISFIFO(mode)); } +enum { + WHITELIST_MATCH_PREFIX = 0, + WHITELIST_MATCH_EXACT, +}; +static inline int qtfs_white_list_match(char *path, char *wl, int wl_len, int match_type) +{ + if (strncmp(path, wl, wl_len)) + return 0; + switch (match_type) { + case WHITELIST_MATCH_PREFIX: + if (wl[wl_len - 1] != '/' && path[wl_len] != '\0' && path[wl_len] != '/') + return 0; + break; + case WHITELIST_MATCH_EXACT: + if (path[wl_len] != '\0') + return 0; + break; + default: + return 0; + } + // match success + return 1; +} + #define QTFS_SOCK_RCVTIMEO 1 #define QTFS_SOCK_SNDTIMEO 1 diff --git a/qtfs/qtfs_common/conn.c b/qtfs/qtfs_common/conn.c index 132652c..c0b9515 100644 --- a/qtfs/qtfs_common/conn.c +++ b/qtfs/qtfs_common/conn.c @@ -108,7 +108,7 @@ err_end: return -ECONNREFUSED; } -static int qtfs_uds_remote_whitelist(const char *path) +static int qtfs_uds_remote_whitelist(char *path) { int i; int ret = 1; @@ -116,10 +116,7 @@ static int qtfs_uds_remote_whitelist(const char *path) read_lock(&g_qtfs_wl.rwlock); cap = &g_qtfs_wl.cap[QTFS_WHITELIST_UDSCONNECT]; for (i = 0; i < cap->nums; i++) { - if (strncmp(path, cap->item[i], strlen(cap->item[i])) == 0) { - if (strlen(path) > strlen(cap->item[i]) && path[strlen(cap->item[i])] != '/') { - continue; - } + if (qtfs_white_list_match(path, cap->item[i], strlen(cap->item[i]), WHITELIST_MATCH_PREFIX)) { ret = 0; break; } diff --git a/qtfs/qtfs_server/fsops.c b/qtfs/qtfs_server/fsops.c index 038384c..9ff5642 100644 --- a/qtfs/qtfs_server/fsops.c +++ b/qtfs/qtfs_server/fsops.c @@ -51,31 +51,6 @@ #define USERP(arg) (arg->userp) DEFINE_MUTEX(fd_bitmap_lock); -enum { - WHITELIST_MATCH_PREFIX = 0, - WHITELIST_MATCH_EXACT, -}; - -static inline int qtfs_white_list_match(char *path, char *wl, int wl_len, int match_type) -{ - if (strncmp(path, wl, wl_len)) - return 0; - switch (match_type) { - case WHITELIST_MATCH_PREFIX: - if (wl[wl_len - 1] != '/' && path[wl_len] != '\0' && path[wl_len] != '/') - return 0; - break; - case WHITELIST_MATCH_EXACT: - if (path[wl_len] != '\0') - return 0; - break; - default: - return 0; - } - // match success - return 1; -} - static bool _in_white_list(char *path, int type, int match_type) { int i, in_wl = -1; -- Gitee