util: move filename_is_valid() and path_is_safe() to path-util.[ch]
This commit is contained in:
parent
4349cd7c1d
commit
bb15fafe9c
|
@ -23,6 +23,7 @@
|
||||||
|
|
||||||
#include "fd-util.h"
|
#include "fd-util.h"
|
||||||
#include "locale-util.h"
|
#include "locale-util.h"
|
||||||
|
#include "path-util.h"
|
||||||
#include "set.h"
|
#include "set.h"
|
||||||
#include "string-util.h"
|
#include "string-util.h"
|
||||||
#include "strv.h"
|
#include "strv.h"
|
||||||
|
|
|
@ -30,6 +30,7 @@
|
||||||
#include "fd-util.h"
|
#include "fd-util.h"
|
||||||
#include "fileio.h"
|
#include "fileio.h"
|
||||||
#include "lockfile-util.h"
|
#include "lockfile-util.h"
|
||||||
|
#include "path-util.h"
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
|
|
||||||
int make_lock_file(const char *p, int operation, LockFile *ret) {
|
int make_lock_file(const char *p, int operation, LockFile *ret) {
|
||||||
|
|
|
@ -723,3 +723,46 @@ char* dirname_malloc(const char *path) {
|
||||||
|
|
||||||
return dir2;
|
return dir2;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool filename_is_valid(const char *p) {
|
||||||
|
const char *e;
|
||||||
|
|
||||||
|
if (isempty(p))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
if (streq(p, "."))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
if (streq(p, ".."))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
e = strchrnul(p, '/');
|
||||||
|
if (*e != 0)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
if (e - p > FILENAME_MAX)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool path_is_safe(const char *p) {
|
||||||
|
|
||||||
|
if (isempty(p))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
if (streq(p, "..") || startswith(p, "../") || endswith(p, "/..") || strstr(p, "/../"))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
if (strlen(p)+1 > PATH_MAX)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
/* The following two checks are not really dangerous, but hey, they still are confusing */
|
||||||
|
if (streq(p, ".") || startswith(p, "./") || endswith(p, "/.") || strstr(p, "/./"))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
if (strstr(p, "//"))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
|
@ -102,3 +102,6 @@ char *prefix_root(const char *root, const char *path);
|
||||||
int parse_path_argument_and_warn(const char *path, bool suppress_root, char **arg);
|
int parse_path_argument_and_warn(const char *path, bool suppress_root, char **arg);
|
||||||
|
|
||||||
char* dirname_malloc(const char *path);
|
char* dirname_malloc(const char *path);
|
||||||
|
|
||||||
|
bool filename_is_valid(const char *p) _pure_;
|
||||||
|
bool path_is_safe(const char *p) _pure_;
|
||||||
|
|
|
@ -1439,26 +1439,6 @@ bool in_initrd(void) {
|
||||||
return saved;
|
return saved;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool filename_is_valid(const char *p) {
|
|
||||||
|
|
||||||
if (isempty(p))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
if (strchr(p, '/'))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
if (streq(p, "."))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
if (streq(p, ".."))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
if (strlen(p) > FILENAME_MAX)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool string_is_safe(const char *p) {
|
bool string_is_safe(const char *p) {
|
||||||
const char *t;
|
const char *t;
|
||||||
|
|
||||||
|
@ -1476,27 +1456,6 @@ bool string_is_safe(const char *p) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool path_is_safe(const char *p) {
|
|
||||||
|
|
||||||
if (isempty(p))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
if (streq(p, "..") || startswith(p, "../") || endswith(p, "/..") || strstr(p, "/../"))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
if (strlen(p)+1 > PATH_MAX)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
/* The following two checks are not really dangerous, but hey, they still are confusing */
|
|
||||||
if (streq(p, ".") || startswith(p, "./") || endswith(p, "/.") || strstr(p, "/./"))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
if (strstr(p, "//"))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* hey glibc, APIs with callbacks without a user pointer are so useless */
|
/* hey glibc, APIs with callbacks without a user pointer are so useless */
|
||||||
void *xbsearch_r(const void *key, const void *base, size_t nmemb, size_t size,
|
void *xbsearch_r(const void *key, const void *base, size_t nmemb, size_t size,
|
||||||
int (*compar) (const void *, const void *, void *), void *arg) {
|
int (*compar) (const void *, const void *, void *), void *arg) {
|
||||||
|
|
|
@ -303,8 +303,6 @@ _alloc_(2, 3) static inline void *memdup_multiply(const void *p, size_t a, size_
|
||||||
return memdup(p, a * b);
|
return memdup(p, a * b);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool filename_is_valid(const char *p) _pure_;
|
|
||||||
bool path_is_safe(const char *p) _pure_;
|
|
||||||
bool string_is_safe(const char *p) _pure_;
|
bool string_is_safe(const char *p) _pure_;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -31,6 +31,7 @@
|
||||||
#include "fileio-label.h"
|
#include "fileio-label.h"
|
||||||
#include "hostname-util.h"
|
#include "hostname-util.h"
|
||||||
#include "parse-util.h"
|
#include "parse-util.h"
|
||||||
|
#include "path-util.h"
|
||||||
#include "selinux-util.h"
|
#include "selinux-util.h"
|
||||||
#include "strv.h"
|
#include "strv.h"
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
|
|
|
@ -27,6 +27,7 @@
|
||||||
#include "escape.h"
|
#include "escape.h"
|
||||||
#include "fd-util.h"
|
#include "fd-util.h"
|
||||||
#include "io-util.h"
|
#include "io-util.h"
|
||||||
|
#include "path-util.h"
|
||||||
#include "process-util.h"
|
#include "process-util.h"
|
||||||
#include "pull-common.h"
|
#include "pull-common.h"
|
||||||
#include "pull-job.h"
|
#include "pull-job.h"
|
||||||
|
|
|
@ -37,6 +37,7 @@
|
||||||
#include "login-util.h"
|
#include "login-util.h"
|
||||||
#include "macro.h"
|
#include "macro.h"
|
||||||
#include "parse-util.h"
|
#include "parse-util.h"
|
||||||
|
#include "path-util.h"
|
||||||
#include "socket-util.h"
|
#include "socket-util.h"
|
||||||
#include "string-util.h"
|
#include "string-util.h"
|
||||||
#include "strv.h"
|
#include "strv.h"
|
||||||
|
|
|
@ -30,20 +30,21 @@
|
||||||
|
|
||||||
#include "sd-bus.h"
|
#include "sd-bus.h"
|
||||||
|
|
||||||
#include "util.h"
|
|
||||||
#include "mkdir.h"
|
|
||||||
#include "strv.h"
|
|
||||||
#include "def.h"
|
|
||||||
#include "env-util.h"
|
|
||||||
#include "fileio.h"
|
|
||||||
#include "fileio-label.h"
|
|
||||||
#include "bus-util.h"
|
|
||||||
#include "bus-error.h"
|
#include "bus-error.h"
|
||||||
#include "bus-message.h"
|
#include "bus-message.h"
|
||||||
|
#include "bus-util.h"
|
||||||
|
#include "def.h"
|
||||||
|
#include "env-util.h"
|
||||||
#include "event-util.h"
|
#include "event-util.h"
|
||||||
#include "locale-util.h"
|
|
||||||
#include "selinux-util.h"
|
|
||||||
#include "fd-util.h"
|
#include "fd-util.h"
|
||||||
|
#include "fileio-label.h"
|
||||||
|
#include "fileio.h"
|
||||||
|
#include "locale-util.h"
|
||||||
|
#include "mkdir.h"
|
||||||
|
#include "path-util.h"
|
||||||
|
#include "selinux-util.h"
|
||||||
|
#include "strv.h"
|
||||||
|
#include "util.h"
|
||||||
|
|
||||||
enum {
|
enum {
|
||||||
/* We don't list LC_ALL here on purpose. People should be
|
/* We don't list LC_ALL here on purpose. People should be
|
||||||
|
|
|
@ -25,6 +25,7 @@
|
||||||
#include "fd-util.h"
|
#include "fd-util.h"
|
||||||
#include "fileio-label.h"
|
#include "fileio-label.h"
|
||||||
#include "mkdir.h"
|
#include "mkdir.h"
|
||||||
|
#include "path-util.h"
|
||||||
#include "string-util.h"
|
#include "string-util.h"
|
||||||
#include "strv.h"
|
#include "strv.h"
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
|
|
|
@ -20,9 +20,10 @@
|
||||||
***/
|
***/
|
||||||
|
|
||||||
#include "btrfs-util.h"
|
#include "btrfs-util.h"
|
||||||
|
#include "import-util.h"
|
||||||
|
#include "path-util.h"
|
||||||
#include "string-util.h"
|
#include "string-util.h"
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
#include "import-util.h"
|
|
||||||
|
|
||||||
int import_url_last_component(const char *url, char **ret) {
|
int import_url_last_component(const char *url, char **ret) {
|
||||||
const char *e, *p;
|
const char *e, *p;
|
||||||
|
|
|
@ -50,6 +50,7 @@
|
||||||
#include "user-util.h"
|
#include "user-util.h"
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
#include "virt.h"
|
#include "virt.h"
|
||||||
|
#include "path-util.h"
|
||||||
|
|
||||||
static void test_streq_ptr(void) {
|
static void test_streq_ptr(void) {
|
||||||
assert_se(streq_ptr(NULL, NULL));
|
assert_se(streq_ptr(NULL, NULL));
|
||||||
|
|
Loading…
Reference in a new issue