2021-04-11 17:37:48 +00:00
|
|
|
/*
|
|
|
|
* CDDL HEADER START
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the terms of the
|
|
|
|
* Common Development and Distribution License (the "License").
|
|
|
|
* You may not use this file except in compliance with the License.
|
|
|
|
*
|
|
|
|
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
|
2022-07-11 21:16:13 +00:00
|
|
|
* or https://opensource.org/licenses/CDDL-1.0.
|
2021-04-11 17:37:48 +00:00
|
|
|
* See the License for the specific language governing permissions
|
|
|
|
* and limitations under the License.
|
|
|
|
*
|
|
|
|
* When distributing Covered Code, include this CDDL HEADER in each
|
|
|
|
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
|
|
|
|
* If applicable, add the following below this CDDL HEADER, with the
|
|
|
|
* fields enclosed by brackets "[]" replaced with your own identifying
|
|
|
|
* information: Portions Copyright [yyyy] [name of copyright owner]
|
|
|
|
*
|
|
|
|
* CDDL HEADER END
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
2021-04-11 17:42:07 +00:00
|
|
|
#include <sys/file.h>
|
2021-04-11 17:37:48 +00:00
|
|
|
#include <fcntl.h>
|
2022-02-28 19:42:22 +00:00
|
|
|
#include <ctype.h>
|
2021-04-11 17:37:48 +00:00
|
|
|
#include <stdio.h>
|
2021-04-11 17:42:07 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <libshare.h>
|
2022-09-09 17:54:16 +00:00
|
|
|
#include <unistd.h>
|
2021-04-11 17:37:48 +00:00
|
|
|
#include "nfs.h"
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* nfs_exports_[lock|unlock] are used to guard against conconcurrent
|
|
|
|
* updates to the exports file. Each protocol is responsible for
|
|
|
|
* providing the necessary locking to ensure consistency.
|
|
|
|
*/
|
2021-04-15 22:40:22 +00:00
|
|
|
static int
|
2022-02-28 11:57:47 +00:00
|
|
|
nfs_exports_lock(const char *name, int *nfs_lock_fd)
|
2021-04-11 17:37:48 +00:00
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
2022-02-28 11:57:47 +00:00
|
|
|
*nfs_lock_fd = open(name, O_RDWR | O_CREAT | O_CLOEXEC, 0600);
|
|
|
|
if (*nfs_lock_fd == -1) {
|
2021-04-11 17:37:48 +00:00
|
|
|
err = errno;
|
|
|
|
fprintf(stderr, "failed to lock %s: %s\n", name, strerror(err));
|
|
|
|
return (err);
|
|
|
|
}
|
|
|
|
|
2022-02-28 11:57:47 +00:00
|
|
|
while ((err = flock(*nfs_lock_fd, LOCK_EX)) != 0 && errno == EINTR)
|
2021-05-17 16:08:35 +00:00
|
|
|
;
|
|
|
|
if (err != 0) {
|
2021-04-11 17:37:48 +00:00
|
|
|
err = errno;
|
|
|
|
fprintf(stderr, "failed to lock %s: %s\n", name, strerror(err));
|
2022-02-28 11:57:47 +00:00
|
|
|
(void) close(*nfs_lock_fd);
|
|
|
|
*nfs_lock_fd = -1;
|
2021-04-11 17:37:48 +00:00
|
|
|
return (err);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2021-04-15 22:40:22 +00:00
|
|
|
static void
|
2022-02-28 11:57:47 +00:00
|
|
|
nfs_exports_unlock(const char *name, int *nfs_lock_fd)
|
2021-04-11 17:37:48 +00:00
|
|
|
{
|
2022-02-28 11:57:47 +00:00
|
|
|
verify(*nfs_lock_fd > 0);
|
2021-04-11 17:37:48 +00:00
|
|
|
|
2022-02-28 11:57:47 +00:00
|
|
|
if (flock(*nfs_lock_fd, LOCK_UN) != 0)
|
2021-04-11 17:37:48 +00:00
|
|
|
fprintf(stderr, "failed to unlock %s: %s\n",
|
|
|
|
name, strerror(errno));
|
|
|
|
|
2022-02-28 11:57:47 +00:00
|
|
|
(void) close(*nfs_lock_fd);
|
|
|
|
*nfs_lock_fd = -1;
|
2021-04-11 17:37:48 +00:00
|
|
|
}
|
2021-04-11 17:42:07 +00:00
|
|
|
|
2021-05-17 16:13:18 +00:00
|
|
|
struct tmpfile {
|
|
|
|
/*
|
|
|
|
* This only needs to be as wide as ZFS_EXPORTS_FILE and mktemp suffix,
|
|
|
|
* 64 is more than enough.
|
|
|
|
*/
|
|
|
|
char name[64];
|
|
|
|
FILE *fp;
|
|
|
|
};
|
|
|
|
|
|
|
|
static boolean_t
|
|
|
|
nfs_init_tmpfile(const char *prefix, const char *mdir, struct tmpfile *tmpf)
|
2021-04-11 17:42:07 +00:00
|
|
|
{
|
|
|
|
if (mdir != NULL &&
|
2021-05-18 16:00:26 +00:00
|
|
|
mkdir(mdir, 0755) < 0 &&
|
|
|
|
errno != EEXIST) {
|
2021-04-11 17:42:07 +00:00
|
|
|
fprintf(stderr, "failed to create %s: %s\n",
|
|
|
|
mdir, strerror(errno));
|
2021-05-17 16:13:18 +00:00
|
|
|
return (B_FALSE);
|
2021-04-11 17:42:07 +00:00
|
|
|
}
|
|
|
|
|
Fix unsafe string operations
Coverity caught unsafe use of `strcpy()` in `ztest_dmu_objset_own()`,
`nfs_init_tmpfile()` and `dump_snapshot()`. It also caught an unsafe use
of `strlcat()` in `nfs_init_tmpfile()`.
Inspired by this, I did an audit of every single usage of `strcpy()` and
`strcat()` in the code. If I could not prove that the usage was safe, I
changed the code to use either `strlcpy()` or `strlcat()`, depending on
which function was originally used. In some cases, `snprintf()` was used
to replace multiple uses of `strcat` because it was cleaner.
Whenever I changed a function, I preferred to use `sizeof(dst)` when the
compiler is able to provide the string size via that. When it could not
because the string was passed by a caller, I checked the entire call
tree of the function to find out how big the buffer was and hard coded
it. Hardcoding is less than ideal, but it is safe unless someone shrinks
the buffer sizes being passed.
Additionally, Coverity reported three more string related issues:
* It caught a case where we do an overlapping memory copy in a call to
`snprintf()`. We fix that via `kmem_strdup()` and `kmem_strfree()`.
* It caught `sizeof (buf)` being used instead of `buflen` in
`zdb_nicenum()`'s call to `zfs_nicenum()`, which is passed to
`snprintf()`. We change that to pass `buflen`.
* It caught a theoretical unterminated string passed to `strcmp()`.
This one is likely a false positive, but we have the information
needed to do this more safely, so we change this to silence the false
positive not just in coverity, but potentially other static analysis
tools too. We switch to `strncmp()`.
* There was a false positive in tests/zfs-tests/cmd/dir_rd_update.c. We
suppress it by switching to `snprintf()` since other static analysis
tools might complain about it too. Interestingly, there is a possible
real bug there too, since it assumes that the passed directory path
ends with '/'. We add a '/' to fix that potential bug.
Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov>
Signed-off-by: Richard Yao <richard.yao@alumni.stonybrook.edu>
Closes #13913
2022-09-27 23:47:24 +00:00
|
|
|
strlcpy(tmpf->name, prefix, sizeof (tmpf->name));
|
|
|
|
strlcat(tmpf->name, ".XXXXXXXX", sizeof (tmpf->name) - strlen(prefix));
|
2021-04-11 17:42:07 +00:00
|
|
|
|
2021-05-17 16:13:18 +00:00
|
|
|
int fd = mkostemp(tmpf->name, O_CLOEXEC);
|
2021-04-11 17:42:07 +00:00
|
|
|
if (fd == -1) {
|
|
|
|
fprintf(stderr, "Unable to create temporary file: %s",
|
|
|
|
strerror(errno));
|
2021-05-17 16:13:18 +00:00
|
|
|
return (B_FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
tmpf->fp = fdopen(fd, "w+");
|
|
|
|
if (tmpf->fp == NULL) {
|
|
|
|
fprintf(stderr, "Unable to reopen temporary file: %s",
|
|
|
|
strerror(errno));
|
|
|
|
close(fd);
|
|
|
|
return (B_FALSE);
|
2021-04-11 17:42:07 +00:00
|
|
|
}
|
2021-05-17 16:13:18 +00:00
|
|
|
|
|
|
|
return (B_TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
nfs_abort_tmpfile(struct tmpfile *tmpf)
|
|
|
|
{
|
|
|
|
unlink(tmpf->name);
|
|
|
|
fclose(tmpf->fp);
|
2021-04-11 17:42:07 +00:00
|
|
|
}
|
|
|
|
|
2021-04-15 22:40:22 +00:00
|
|
|
static int
|
2021-05-17 16:13:18 +00:00
|
|
|
nfs_fini_tmpfile(const char *exports, struct tmpfile *tmpf)
|
2021-04-11 17:42:07 +00:00
|
|
|
{
|
2021-05-17 16:13:18 +00:00
|
|
|
if (fflush(tmpf->fp) != 0) {
|
|
|
|
fprintf(stderr, "Failed to write to temporary file: %s\n",
|
2021-04-11 17:42:07 +00:00
|
|
|
strerror(errno));
|
2021-05-17 16:13:18 +00:00
|
|
|
nfs_abort_tmpfile(tmpf);
|
|
|
|
return (SA_SYSTEM_ERR);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rename(tmpf->name, exports) == -1) {
|
|
|
|
fprintf(stderr, "Unable to rename %s -> %s: %s\n",
|
|
|
|
tmpf->name, exports, strerror(errno));
|
|
|
|
nfs_abort_tmpfile(tmpf);
|
2021-04-11 17:42:07 +00:00
|
|
|
return (SA_SYSTEM_ERR);
|
|
|
|
}
|
2021-05-17 16:13:18 +00:00
|
|
|
|
2021-05-17 19:08:16 +00:00
|
|
|
(void) fchmod(fileno(tmpf->fp), 0644);
|
2021-05-17 16:13:18 +00:00
|
|
|
fclose(tmpf->fp);
|
2021-04-11 17:42:07 +00:00
|
|
|
return (SA_OK);
|
|
|
|
}
|
|
|
|
|
2022-02-28 19:42:22 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2021-05-17 17:56:48 +00:00
|
|
|
static int
|
2021-05-17 18:25:29 +00:00
|
|
|
nfs_process_exports(const char *exports, const char *mountpoint,
|
|
|
|
boolean_t (*cbk)(void *userdata, char *line, boolean_t found_mountpoint),
|
|
|
|
void *userdata)
|
2021-05-17 17:56:48 +00:00
|
|
|
{
|
|
|
|
int error = SA_OK;
|
2021-05-17 18:25:29 +00:00
|
|
|
boolean_t cont = B_TRUE;
|
2021-05-17 17:56:48 +00:00
|
|
|
|
|
|
|
FILE *oldfp = fopen(exports, "re");
|
|
|
|
if (oldfp != NULL) {
|
2022-02-28 19:42:22 +00:00
|
|
|
boolean_t need_mp_free;
|
|
|
|
char *mp;
|
|
|
|
if ((error = nfs_escape_mountpoint(mountpoint,
|
|
|
|
&mp, &need_mp_free)) != SA_OK) {
|
|
|
|
(void) fclose(oldfp);
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
2021-05-17 17:56:48 +00:00
|
|
|
char *buf = NULL, *sep;
|
2022-02-28 19:42:22 +00:00
|
|
|
size_t buflen = 0, mplen = strlen(mp);
|
2021-05-17 17:56:48 +00:00
|
|
|
|
2021-05-17 18:25:29 +00:00
|
|
|
while (cont && getline(&buf, &buflen, oldfp) != -1) {
|
2021-05-17 17:56:48 +00:00
|
|
|
if (buf[0] == '\n' || buf[0] == '#')
|
|
|
|
continue;
|
|
|
|
|
2021-05-17 18:25:29 +00:00
|
|
|
cont = cbk(userdata, buf,
|
|
|
|
(sep = strpbrk(buf, "\t \n")) != NULL &&
|
2021-05-17 17:56:48 +00:00
|
|
|
sep - buf == mplen &&
|
2022-02-28 19:42:22 +00:00
|
|
|
strncmp(buf, mp, mplen) == 0);
|
2021-05-17 17:56:48 +00:00
|
|
|
}
|
2021-05-17 18:25:29 +00:00
|
|
|
free(buf);
|
2022-02-28 19:42:22 +00:00
|
|
|
if (need_mp_free)
|
|
|
|
free(mp);
|
2021-05-17 17:56:48 +00:00
|
|
|
|
|
|
|
if (ferror(oldfp) != 0)
|
|
|
|
error = ferror(oldfp);
|
|
|
|
|
|
|
|
if (fclose(oldfp) != 0) {
|
|
|
|
fprintf(stderr, "Unable to close file %s: %s\n",
|
|
|
|
exports, strerror(errno));
|
|
|
|
error = error != SA_OK ? error : SA_SYSTEM_ERR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-17 18:25:29 +00:00
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
static boolean_t
|
|
|
|
nfs_copy_entries_cb(void *userdata, char *line, boolean_t found_mountpoint)
|
|
|
|
{
|
|
|
|
FILE *newfp = userdata;
|
|
|
|
if (!found_mountpoint)
|
|
|
|
fputs(line, newfp);
|
|
|
|
return (B_TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copy all entries from the exports file (if it exists) to newfp,
|
|
|
|
* omitting any entries for the specified mountpoint.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
nfs_copy_entries(FILE *newfp, const char *exports, const char *mountpoint)
|
|
|
|
{
|
|
|
|
fputs(FILE_HEADER, newfp);
|
|
|
|
|
|
|
|
int error = nfs_process_exports(
|
|
|
|
exports, mountpoint, nfs_copy_entries_cb, newfp);
|
|
|
|
|
2021-05-17 17:56:48 +00:00
|
|
|
if (error == SA_OK && ferror(newfp) != 0)
|
|
|
|
error = ferror(newfp);
|
|
|
|
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
2021-06-03 21:50:07 +00:00
|
|
|
int
|
2021-04-15 22:40:22 +00:00
|
|
|
nfs_toggle_share(const char *lockfile, const char *exports,
|
|
|
|
const char *expdir, sa_share_impl_t impl_share,
|
2021-05-17 16:13:18 +00:00
|
|
|
int(*cbk)(sa_share_impl_t impl_share, FILE *tmpfile))
|
2021-04-11 17:42:07 +00:00
|
|
|
{
|
2022-02-28 11:57:47 +00:00
|
|
|
int error, nfs_lock_fd = -1;
|
2021-05-17 16:13:18 +00:00
|
|
|
struct tmpfile tmpf;
|
2021-04-11 17:42:07 +00:00
|
|
|
|
2021-05-17 16:13:18 +00:00
|
|
|
if (!nfs_init_tmpfile(exports, expdir, &tmpf))
|
2021-04-11 17:42:07 +00:00
|
|
|
return (SA_SYSTEM_ERR);
|
|
|
|
|
2022-02-28 11:57:47 +00:00
|
|
|
error = nfs_exports_lock(lockfile, &nfs_lock_fd);
|
2021-04-11 17:42:07 +00:00
|
|
|
if (error != 0) {
|
2021-05-17 16:13:18 +00:00
|
|
|
nfs_abort_tmpfile(&tmpf);
|
2021-04-11 17:42:07 +00:00
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
2021-05-17 17:56:48 +00:00
|
|
|
error = nfs_copy_entries(tmpf.fp, exports, impl_share->sa_mountpoint);
|
2021-04-15 22:40:22 +00:00
|
|
|
if (error != SA_OK)
|
|
|
|
goto fullerr;
|
|
|
|
|
2021-05-17 16:13:18 +00:00
|
|
|
error = cbk(impl_share, tmpf.fp);
|
2021-04-15 22:40:22 +00:00
|
|
|
if (error != SA_OK)
|
|
|
|
goto fullerr;
|
2021-04-11 17:42:07 +00:00
|
|
|
|
2021-05-17 16:13:18 +00:00
|
|
|
error = nfs_fini_tmpfile(exports, &tmpf);
|
2022-02-28 11:57:47 +00:00
|
|
|
nfs_exports_unlock(lockfile, &nfs_lock_fd);
|
2021-04-11 17:42:07 +00:00
|
|
|
return (error);
|
2021-04-15 22:40:22 +00:00
|
|
|
|
|
|
|
fullerr:
|
2021-05-17 16:13:18 +00:00
|
|
|
nfs_abort_tmpfile(&tmpf);
|
2022-02-28 11:57:47 +00:00
|
|
|
nfs_exports_unlock(lockfile, &nfs_lock_fd);
|
2021-04-15 22:40:22 +00:00
|
|
|
return (error);
|
2021-04-11 17:42:07 +00:00
|
|
|
}
|
2021-05-17 18:25:29 +00:00
|
|
|
|
2022-09-09 17:54:16 +00:00
|
|
|
void
|
|
|
|
nfs_reset_shares(const char *lockfile, const char *exports)
|
|
|
|
{
|
|
|
|
int nfs_lock_fd = -1;
|
|
|
|
|
|
|
|
if (nfs_exports_lock(lockfile, &nfs_lock_fd) == 0) {
|
|
|
|
(void) ! truncate(exports, 0);
|
|
|
|
nfs_exports_unlock(lockfile, &nfs_lock_fd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-17 18:25:29 +00:00
|
|
|
static boolean_t
|
|
|
|
nfs_is_shared_cb(void *userdata, char *line, boolean_t found_mountpoint)
|
|
|
|
{
|
2021-12-12 14:26:39 +00:00
|
|
|
(void) line;
|
|
|
|
|
2021-05-17 18:25:29 +00:00
|
|
|
boolean_t *found = userdata;
|
|
|
|
*found = found_mountpoint;
|
|
|
|
return (!found_mountpoint);
|
|
|
|
}
|
|
|
|
|
|
|
|
boolean_t
|
|
|
|
nfs_is_shared_impl(const char *exports, sa_share_impl_t impl_share)
|
|
|
|
{
|
|
|
|
boolean_t found = B_FALSE;
|
|
|
|
nfs_process_exports(exports, impl_share->sa_mountpoint,
|
|
|
|
nfs_is_shared_cb, &found);
|
|
|
|
return (found);
|
|
|
|
}
|