diff options
author | Kaz Kylheku <kaz@kylheku.com> | 2022-07-24 16:14:09 -0700 |
---|---|---|
committer | Kaz Kylheku <kaz@kylheku.com> | 2022-07-24 16:14:09 -0700 |
commit | 645b1184ec996696f24a65c1ad39fc50a1e5cbb2 (patch) | |
tree | 49b7a45ba5a9ec447d97e344a19c0a393bb64cd6 | |
parent | 527c20f525c0f178707dea37589f494c9f45e48f (diff) | |
download | safepath-645b1184ec996696f24a65c1ad39fc50a1e5cbb2.tar.gz safepath-645b1184ec996696f24a65c1ad39fc50a1e5cbb2.tar.bz2 safepath-645b1184ec996696f24a65c1ad39fc50a1e5cbb2.zip |
Correctly handle readlink overflow.
* safepath.h (SAFEPATH_TOOLONG): New enum constant.
* safepath.c (safepatch_check): Don't ignore the truncation
situation from readlink. Use the full buffer length,
and if readlink returns 256, then diagnose overflow using the
new SAFEPATH_TOOLONG error code and bail.
(safepath_strerr): Map SAFEPATH_TOOLONG.
-rw-r--r-- | safepath.c | 10 | ||||
-rw-r--r-- | safepath.h | 1 |
2 files changed, 9 insertions, 2 deletions
@@ -248,11 +248,16 @@ int safepath_check(const char *name) goto free_out; } - if ((len = readlink(copy, link, sizeof link - 1)) < 0) { + if ((len = readlink(copy, link, sizeof link)) < 0) { ret = safepath_err(errno); goto free_out; } + if (len == sizeof link) { + ret = SAFEPATH_TOOLONG; + goto free_out; + } + link[len] = 0; /* Resolve the symlink, using two different cases based @@ -352,7 +357,8 @@ const char *safepath_strerr(int err) [SAFEPATH_NOTDIR] = "path contains non-directory component", [SAFEPATH_INVAL] = "path is syntactically invalid", [SAFEPATH_NOMEM] = "out of memory", - [SAFEPATH_LOOP] = "too many symlink resolutions" + [SAFEPATH_LOOP] = "too many symlink resolutions", + [SAFEPATH_TOOLONG] = "path component or symlink target too long" }; const char *ret = "SAFEPATH_BAD_ERROR_CODE"; @@ -45,6 +45,7 @@ enum { SAFEPATH_INVAL, /* path is invalid */ SAFEPATH_NOMEM, /* out of memory */ SAFEPATH_LOOP, /* more than 8 levels of symlink */ + SAFEPATH_TOOLONG, /* component or symlink target too long */ }; int safepath_check(const char *name); |