Skip to content

Commit

Permalink
lib-sieve: util: realpath: Add assertions to protect memory allocatio…
Browse files Browse the repository at this point in the history
…ns and boundaries.
  • Loading branch information
stephanbosch committed Feb 5, 2018
1 parent 721b2af commit a960252
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/lib-sieve/util/realpath.c
Expand Up @@ -82,8 +82,10 @@ static int path_normalize(const char *path, bool resolve_links,
}
} else {
/* make sure npath now ends in slash */
if (*(npath_pos-1) != '/')
if (*(npath_pos-1) != '/') {
i_assert(npath_pos + 1 < npath + asize);
*(npath_pos++) = '/';
}

/* allocate space if necessary */
if ((npath_pos + seglen + 1) >= (npath + asize)) {
Expand All @@ -94,6 +96,7 @@ static int path_normalize(const char *path, bool resolve_links,
}

/* copy segment to normalized path */
i_assert(p + seglen < npath + asize);
(void)memmove(npath_pos, p, seglen);
npath_pos += seglen;
}
Expand Down Expand Up @@ -141,6 +144,8 @@ static int path_normalize(const char *path, bool resolve_links,
for (;;) {
npath_link = (npath_pos + 1) + ltlen;

i_assert(npath_link + lsize < npath + asize);

/* attempt to read the link */
if ((ret=readlink(npath, npath_link, lsize)) < 0)
return -1;
Expand Down Expand Up @@ -169,10 +174,13 @@ static int path_normalize(const char *path, bool resolve_links,
}

/* add tail of previous path at end of symlink */
if (ltlen > 0)
if (ltlen > 0) {
i_assert(npath_pos + 1 + tlen < npath + asize);
(void)memcpy(npath_link + ret, npath_pos + 1, tlen);
else
} else {
i_assert(segend + tlen < npath + asize);
(void)memcpy(npath_link + ret, segend, tlen);
}
*(npath_link+ret+tlen) = '\0';

/* use as new source path */
Expand Down Expand Up @@ -200,6 +208,8 @@ static int path_normalize(const char *path, bool resolve_links,
p = segend;
}

i_assert(npath_pos < npath + asize);

/* remove any trailing slash */
if (npath_pos > npath + 1 && *(npath_pos-1) == '/')
npath_pos--;
Expand Down

0 comments on commit a960252

Please sign in to comment.