diff options
author | наб <[email protected]> | 2022-02-28 20:42:22 +0100 |
---|---|---|
committer | Brian Behlendorf <[email protected]> | 2022-05-12 09:26:54 -0700 |
commit | 9b06aa634a80d6820ffb1453ccb3e4572be303d4 (patch) | |
tree | 16dbd09665ce99f0fa563965d52ddccdecddfc70 /lib/libshare | |
parent | a31fcd4bad22d20a22cdd8e9f7e388a99508a8d7 (diff) |
libshare/nfs: escape mount points when needed
Reviewed-by: Brian Behlendorf <[email protected]>
Signed-off-by: Ahelenia Ziemiańska <[email protected]>
Closes #13165
Closes #13153
Diffstat (limited to 'lib/libshare')
-rw-r--r-- | lib/libshare/nfs.c | 43 | ||||
-rw-r--r-- | lib/libshare/nfs.h | 1 | ||||
-rw-r--r-- | lib/libshare/os/freebsd/nfs.c | 15 | ||||
-rw-r--r-- | lib/libshare/os/linux/nfs.c | 13 |
4 files changed, 64 insertions, 8 deletions
diff --git a/lib/libshare/nfs.c b/lib/libshare/nfs.c index 4a90bcbd3..2146346cf 100644 --- a/lib/libshare/nfs.c +++ b/lib/libshare/nfs.c @@ -24,6 +24,7 @@ #include <sys/stat.h> #include <sys/file.h> #include <fcntl.h> +#include <ctype.h> #include <stdio.h> #include <errno.h> #include <libshare.h> @@ -143,6 +144,34 @@ nfs_fini_tmpfile(const char *exports, struct tmpfile *tmpf) return (SA_OK); } +int +nfs_escape_mountpoint(const char *mp, char **out, boolean_t *need_free) +{ + if (strpbrk(mp, "\t\n\v\f\r \\") == NULL) { + *out = (char *)mp; + *need_free = B_FALSE; + return (SA_OK); + } else { + size_t len = strlen(mp); + *out = malloc(len * 4 + 1); + if (!*out) + return (SA_NO_MEMORY); + *need_free = B_TRUE; + + char *oc = *out; + for (const char *c = mp; c < mp + len; ++c) + if (memchr("\t\n\v\f\r \\", *c, + strlen("\t\n\v\f\r \\"))) { + sprintf(oc, "\\%03hho", *c); + oc += 4; + } else + *oc++ = *c; + *oc = '\0'; + } + + return (SA_OK); +} + static int nfs_process_exports(const char *exports, const char *mountpoint, boolean_t (*cbk)(void *userdata, char *line, boolean_t found_mountpoint), @@ -153,8 +182,16 @@ nfs_process_exports(const char *exports, const char *mountpoint, FILE *oldfp = fopen(exports, "re"); if (oldfp != NULL) { + boolean_t need_mp_free; + char *mp; + if ((error = nfs_escape_mountpoint(mountpoint, + &mp, &need_mp_free)) != SA_OK) { + (void) fclose(oldfp); + return (error); + } + char *buf = NULL, *sep; - size_t buflen = 0, mplen = strlen(mountpoint); + size_t buflen = 0, mplen = strlen(mp); while (cont && getline(&buf, &buflen, oldfp) != -1) { if (buf[0] == '\n' || buf[0] == '#') @@ -163,9 +200,11 @@ nfs_process_exports(const char *exports, const char *mountpoint, cont = cbk(userdata, buf, (sep = strpbrk(buf, "\t \n")) != NULL && sep - buf == mplen && - strncmp(buf, mountpoint, mplen) == 0); + strncmp(buf, mp, mplen) == 0); } free(buf); + if (need_mp_free) + free(mp); if (ferror(oldfp) != 0) error = ferror(oldfp); diff --git a/lib/libshare/nfs.h b/lib/libshare/nfs.h index 777581625..24ae1f8ad 100644 --- a/lib/libshare/nfs.h +++ b/lib/libshare/nfs.h @@ -28,6 +28,7 @@ #define FILE_HEADER "# !!! DO NOT EDIT THIS FILE MANUALLY !!!\n\n" +int nfs_escape_mountpoint(const char *mp, char **out, boolean_t *need_free); boolean_t nfs_is_shared_impl(const char *exports, sa_share_impl_t impl_share); int nfs_toggle_share(const char *lockfile, const char *exports, const char *expdir, sa_share_impl_t impl_share, diff --git a/lib/libshare/os/freebsd/nfs.c b/lib/libshare/os/freebsd/nfs.c index 338d25fcd..78977a25f 100644 --- a/lib/libshare/os/freebsd/nfs.c +++ b/lib/libshare/os/freebsd/nfs.c @@ -109,15 +109,24 @@ nfs_enable_share_impl(sa_share_impl_t impl_share, FILE *tmpfile) if (strcmp(shareopts, "on") == 0) shareopts = ""; - if (fputs(impl_share->sa_mountpoint, tmpfile) == EOF || + boolean_t need_free; + char *mp; + int rc = nfs_escape_mountpoint(impl_share->sa_mountpoint, &mp, + &need_free); + if (rc != SA_OK) + return (rc); + + if (fputs(mp, tmpfile) == EOF || fputc('\t', tmpfile) == EOF || translate_opts(shareopts, tmpfile) == EOF || fputc('\n', tmpfile) == EOF) { fprintf(stderr, "failed to write to temporary file\n"); - return (SA_SYSTEM_ERR); + rc = SA_SYSTEM_ERR; } - return (SA_OK); + if (need_free) + free(mp); + return (rc); } static int diff --git a/lib/libshare/os/linux/nfs.c b/lib/libshare/os/linux/nfs.c index b93d73ecf..a7b40ea77 100644 --- a/lib/libshare/os/linux/nfs.c +++ b/lib/libshare/os/linux/nfs.c @@ -387,14 +387,21 @@ nfs_add_entry(FILE *tmpfile, const char *sharepath, if (linux_opts == NULL) linux_opts = ""; - if (fprintf(tmpfile, "%s %s(sec=%s,%s,%s)\n", sharepath, + boolean_t need_free; + char *mp; + int rc = nfs_escape_mountpoint(sharepath, &mp, &need_free); + if (rc != SA_OK) + return (rc); + if (fprintf(tmpfile, "%s %s(sec=%s,%s,%s)\n", mp, get_linux_hostspec(host), security, access_opts, linux_opts) < 0) { fprintf(stderr, "failed to write to temporary file\n"); - return (SA_SYSTEM_ERR); + rc = SA_SYSTEM_ERR; } - return (SA_OK); + if (need_free) + free(mp); + return (rc); } /* |