Path: blob/master/tools/testing/selftests/firmware/fw_namespace.c
26285 views
// SPDX-License-Identifier: GPL-2.01/* Test triggering of loading of firmware from different mount2* namespaces. Expect firmware to be always loaded from the mount3* namespace of PID 1. */4#define _GNU_SOURCE5#include <errno.h>6#include <fcntl.h>7#include <sched.h>8#include <stdarg.h>9#include <stdbool.h>10#include <stdio.h>11#include <stdlib.h>12#include <string.h>13#include <sys/mount.h>14#include <sys/stat.h>15#include <sys/types.h>16#include <sys/wait.h>17#include <unistd.h>1819static char *fw_path = NULL;2021static void die(char *fmt, ...)22{23va_list ap;2425va_start(ap, fmt);26vfprintf(stderr, fmt, ap);27va_end(ap);28if (fw_path)29unlink(fw_path);30umount("/lib/firmware");31exit(EXIT_FAILURE);32}3334static void trigger_fw(const char *fw_name, const char *sys_path)35{36int fd;3738fd = open(sys_path, O_WRONLY);39if (fd < 0)40die("open failed: %s\n",41strerror(errno));42if (write(fd, fw_name, strlen(fw_name)) != strlen(fw_name))43exit(EXIT_FAILURE);44close(fd);45}4647static void setup_fw(const char *fw_path)48{49int fd;50const char fw[] = "ABCD0123";5152fd = open(fw_path, O_WRONLY | O_CREAT, 0600);53if (fd < 0)54die("open failed: %s\n",55strerror(errno));56if (write(fd, fw, sizeof(fw) -1) != sizeof(fw) -1)57die("write failed: %s\n",58strerror(errno));59close(fd);60}6162static bool test_fw_in_ns(const char *fw_name, const char *sys_path, bool block_fw_in_parent_ns)63{64pid_t child;6566if (block_fw_in_parent_ns)67if (mount("test", "/lib/firmware", "tmpfs", MS_RDONLY, NULL) == -1)68die("blocking firmware in parent ns failed\n");6970child = fork();71if (child == -1) {72die("fork failed: %s\n",73strerror(errno));74}75if (child != 0) { /* parent */76pid_t pid;77int status;7879pid = waitpid(child, &status, 0);80if (pid == -1) {81die("waitpid failed: %s\n",82strerror(errno));83}84if (pid != child) {85die("waited for %d got %d\n",86child, pid);87}88if (!WIFEXITED(status)) {89die("child did not terminate cleanly\n");90}91if (block_fw_in_parent_ns)92umount("/lib/firmware");93return WEXITSTATUS(status) == EXIT_SUCCESS;94}9596if (unshare(CLONE_NEWNS) != 0) {97die("unshare(CLONE_NEWNS) failed: %s\n",98strerror(errno));99}100if (mount(NULL, "/", NULL, MS_SLAVE|MS_REC, NULL) == -1)101die("remount root in child ns failed\n");102103if (!block_fw_in_parent_ns) {104if (mount("test", "/lib/firmware", "tmpfs", MS_RDONLY, NULL) == -1)105die("blocking firmware in child ns failed\n");106} else107umount("/lib/firmware");108109trigger_fw(fw_name, sys_path);110111exit(EXIT_SUCCESS);112}113114int main(int argc, char **argv)115{116const char *fw_name = "test-firmware.bin";117char *sys_path;118if (argc != 2)119die("usage: %s sys_path\n", argv[0]);120121/* Mount tmpfs to /lib/firmware so we don't have to assume122that it is writable for us.*/123if (mount("test", "/lib/firmware", "tmpfs", 0, NULL) == -1)124die("mounting tmpfs to /lib/firmware failed\n");125126sys_path = argv[1];127if (asprintf(&fw_path, "/lib/firmware/%s", fw_name) < 0)128die("error: failed to build full fw_path\n");129130setup_fw(fw_path);131132setvbuf(stdout, NULL, _IONBF, 0);133/* Positive case: firmware in PID1 mount namespace */134printf("Testing with firmware in parent namespace (assumed to be same file system as PID1)\n");135if (!test_fw_in_ns(fw_name, sys_path, false))136die("error: failed to access firmware\n");137138/* Negative case: firmware in child mount namespace, expected to fail */139printf("Testing with firmware in child namespace\n");140if (test_fw_in_ns(fw_name, sys_path, true))141die("error: firmware access did not fail\n");142143unlink(fw_path);144free(fw_path);145umount("/lib/firmware");146exit(EXIT_SUCCESS);147}148149150