X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=src%2Fwgt.c;h=980719b3ddc03e1fdc303b4a5aca9a4ec78ac01b;hb=9ab266df6642c6e930e03b3024d7c3d53ef88bbc;hp=db590914290436c1d002aa6af73f988c08a0ce2f;hpb=f3d64b7c741677cd28e2a11deed67196cd02b46a;p=src%2Fapp-framework-main.git diff --git a/src/wgt.c b/src/wgt.c index db59091..980719b 100644 --- a/src/wgt.c +++ b/src/wgt.c @@ -36,6 +36,7 @@ struct wgt { char **locales; }; +/* a valid subpath is a relative path not looking deeper than root using .. */ static int validsubpath(const char *subpath) { int l = 0, i = 0; @@ -63,7 +64,8 @@ static int validsubpath(const char *subpath) default: while(subpath[i] && subpath[i] != '/') i++; - l++; + if (l >= 0) + l++; case '/': break; } @@ -71,6 +73,13 @@ static int validsubpath(const char *subpath) return l >= 0; } +static const char *normalsubpath(const char *subpath) +{ + while(*subpath == '/') + subpath++; + return validsubpath(subpath) ? subpath : NULL; +} + struct wgt *wgt_create() { struct wgt *wgt = malloc(sizeof * wgt); @@ -123,12 +132,10 @@ int wgt_connectat(struct wgt *wgt, int dirfd, const char *pathname) assert(wgt); - rfd = dirfd; - if (pathname) { - rfd = openat(rfd, pathname, O_PATH|O_DIRECTORY); - if (rfd < 0) - return rfd; - } + rfd = (pathname && *pathname) ? openat(dirfd, pathname, O_PATH|O_DIRECTORY) : dup(dirfd); + if (rfd < 0) + return rfd; + if (wgt->rootfd >= 0) close(wgt->rootfd); wgt->rootfd = rfd; @@ -140,6 +147,18 @@ int wgt_connect(struct wgt *wgt, const char *pathname) return wgt_connectat(wgt, AT_FDCWD, pathname); } +struct wgt *wgt_createat(int dirfd, const char *pathname) +{ + struct wgt *wgt = wgt_create(); + if (wgt) { + if (wgt_connectat(wgt, dirfd, pathname)) { + wgt_unref(wgt); + wgt = NULL; + } + } + return wgt; +} + int wgt_is_connected(struct wgt *wgt) { assert(wgt); @@ -150,7 +169,9 @@ int wgt_has(struct wgt *wgt, const char *filename) { assert(wgt); assert(wgt_is_connected(wgt)); - if (!validsubpath(filename)) { + + filename = normalsubpath(filename); + if (!filename) { errno = EINVAL; return -1; } @@ -161,7 +182,8 @@ int wgt_open_read(struct wgt *wgt, const char *filename) { assert(wgt); assert(wgt_is_connected(wgt)); - if (!validsubpath(filename)) { + filename = normalsubpath(filename); + if (!filename) { errno = EINVAL; return -1; } @@ -229,7 +251,8 @@ static const char *localize(struct wgt *wgt, const char *filename, char path[PAT { int i; - if (!validsubpath(filename)) { + filename = normalsubpath(filename); + if (!filename) { errno = EINVAL; return NULL; } @@ -282,3 +305,27 @@ int wgt_locales_open_read(struct wgt *wgt, const char *filename) } +#if defined(TEST_wgt_validsubpath) +#include +void t(const char *subpath, int validity) { + printf("%s -> %d = %d, %s\n", subpath, validity, validsubpath(subpath), validsubpath(subpath)==validity ? "ok" : "NOT OK"); +} +int main() { + t("/",0); + t("..",0); + t(".",1); + t("../a",0); + t("a/..",1); + t("a/../////..",0); + t("a/../b/..",1); + t("a/b/c/..",1); + t("a/b/c/../..",1); + t("a/b/c/../../..",1); + t("a/b/c/../../../.",1); + t("./..a/././..b/..c/./.././.././../.",1); + t("./..a/././..b/..c/./.././.././.././..",0); + t("./..a//.//./..b/..c/./.././/./././///.././.././a/a/a/a/a",1); + return 0; +} +#endif +