mirror_zfs/tests/zfs-tests/cmd/user_ns_exec.c
Richard Yao 6a42939fcd
Cleanup: Address Clang's static analyzer's unused code complaints
These were categorized as the following:

 * Dead assignment		23
 * Dead increment		4
 * Dead initialization		6
 * Dead nested assignment	18

Most of these are harmless, but since actual issues can hide among them,
we correct them.

That said, there were a few return values that were being ignored that
appeared to merit some correction:

 * `destroy_callback()` in `cmd/zfs/zfs_main.c` ignored the error from
   `destroy_batched()`. We handle it by returning -1 if there is an
   error.

 * `zfs_do_upgrade()` in `cmd/zfs/zfs_main.c` ignored the error from
   `zfs_for_each()`. We handle it by doing a binary OR of the error
   value from the subsequent `zfs_for_each()` call to the existing
   value. This is how errors are mostly handled inside `zfs_for_each()`.
   The error value here is passed to exit from the zfs command, so doing
   a binary or on it is better than what we did previously.

 * `get_zap_prop()` in `module/zfs/zcp_get.c` ignored the error from
   `dsl_prop_get_ds()` when the property is not of type string. We
   return an error when it does. There is a small concern that the
   `zfs_get_temporary_prop()` call would handle things, but in the case
   that it does not, we would be pushing an uninitialized numval onto
   the lua stack. It is expected that `dsl_prop_get_ds()` will succeed
   anytime that `zfs_get_temporary_prop()` does, so that not giving it a
   chance to fix things is not a problem.

 * `draid_merge_impl()` in `tests/zfs-tests/cmd/draid.c` used
   `nvlist_add_nvlist()` twice in ways in which errors are expected to
   be impossible, so we switch to `fnvlist_add_nvlist()`.

A few notable ones did not merit use of the return value, so we
suppressed it with `(void)`:

 * `write_free_diffs()` in `lib/libzfs/libzfs_diff.c` ignored the error
   value from `describe_free()`. A look through the commit history
   revealed that this was intentional.

 * `arc_evict_hdr()` in `module/zfs/arc.c` did not need to use the
   returned handle from `arc_hdr_realloc()` because it is already
   referenced in lists.

 * `spa_vdev_detach()` in `module/zfs/spa.c` has a comment explicitly
   saying not to use the error from `vdev_label_init()` because whatever
   causes the error could be the reason why a detach is being done.

Unfortunately, I am not presently able to analyze the kernel modules
with Clang's static analyzer, so I could have missed some cases of this.
In cases where reports were present in code that is duplicated between
Linux and FreeBSD, I made a conscious effort to fix the FreeBSD version
too.

After this commit is merged, regressions like dee8934 should become
extremely obvious with Clang's static analyzer since a regression would
appear in the results as the only instance of unused code. That assumes
that Coverity does not catch the issue first.

My local branch with fixes from all of my outstanding non-draft pull
requests shows 118 reports from Clang's static anlayzer after this
patch. That is down by 51 from 169.

Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov>
Reviewed-by: Cedric Berger <cedric@precidata.com>
Signed-off-by: Richard Yao <richard.yao@alumni.stonybrook.edu>
Closes #13986
2022-10-14 13:37:54 -07:00

179 lines
3.6 KiB
C

/*
* 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
* or https://opensource.org/licenses/CDDL-1.0.
* 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 <stdio.h>
#include <unistd.h>
#include <string.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <errno.h>
#include <signal.h>
#include <sched.h>
#define EXECSHELL "/bin/sh"
#define UIDMAP "0 100000 65536"
static int
child_main(int argc, char *argv[], int sync_pipe)
{
char sync_buf;
char cmds[BUFSIZ] = { 0 };
char sep[] = " ";
int i, len;
if (unshare(CLONE_NEWUSER | CLONE_NEWNS) != 0) {
perror("unshare");
return (1);
}
/* tell parent we entered the new namespace */
if (write(sync_pipe, "1", 1) != 1) {
perror("write");
return (1);
}
/* wait for parent to setup the uid mapping */
if (read(sync_pipe, &sync_buf, 1) != 1) {
(void) fprintf(stderr, "user namespace setup failed\n");
return (1);
}
close(sync_pipe);
if (setuid(0) != 0) {
perror("setuid");
return (1);
}
if (setgid(0) != 0) {
perror("setgid");
return (1);
}
len = 0;
for (i = 1; i < argc; i++) {
(void) snprintf(cmds+len, sizeof (cmds)-len,
"%s%s", argv[i], sep);
len += strlen(argv[i]) + strlen(sep);
}
if (execl(EXECSHELL, "sh", "-c", cmds, (char *)NULL) != 0) {
perror("execl: " EXECSHELL);
return (1);
}
return (0);
}
static int
set_idmap(pid_t pid, const char *file)
{
int result = 0;
int mapfd;
char path[PATH_MAX];
(void) snprintf(path, sizeof (path), "/proc/%d/%s", (int)pid, file);
mapfd = open(path, O_WRONLY);
if (mapfd < 0) {
perror("open");
return (errno);
}
if (write(mapfd, UIDMAP, sizeof (UIDMAP)-1) != sizeof (UIDMAP)-1) {
perror("write");
result = (errno);
}
close(mapfd);
return (result);
}
int
main(int argc, char *argv[])
{
char sync_buf;
int result, wstatus;
int syncfd[2];
pid_t child;
if (argc < 2 || strlen(argv[1]) == 0) {
(void) printf("\tUsage: %s <commands> ...\n", argv[0]);
return (1);
}
if (socketpair(AF_UNIX, SOCK_STREAM, 0, syncfd) != 0) {
perror("socketpair");
return (1);
}
child = fork();
if (child == (pid_t)-1) {
perror("fork");
return (1);
}
if (child == 0) {
close(syncfd[0]);
return (child_main(argc, argv, syncfd[1]));
}
close(syncfd[1]);
result = 0;
/* wait for the child to have unshared its namespaces */
if (read(syncfd[0], &sync_buf, 1) != 1) {
perror("read");
kill(child, SIGKILL);
result = 1;
goto reap;
}
/* write uid mapping */
if (set_idmap(child, "uid_map") != 0 ||
set_idmap(child, "gid_map") != 0) {
result = 1;
kill(child, SIGKILL);
goto reap;
}
/* tell the child to proceed */
if (write(syncfd[0], "1", 1) != 1) {
perror("write");
kill(child, SIGKILL);
result = 1;
goto reap;
}
close(syncfd[0]);
reap:
while (waitpid(child, &wstatus, 0) != child)
kill(child, SIGKILL);
if (result == 0)
result = WEXITSTATUS(wstatus);
return (result);
}