Skip to content

Commit f51e55a

Browse files
mtth-bfftl0kod
authored andcommitted
samples/landlock: Refactor help message
Help message is getting larger with each new supported feature (scopes, and soon UDP). Also the large number of calls to fprintf with environment variables make it hard to read. Refactor it away into a single simpler constant format string. Signed-off-by: Matthieu Buffet <[email protected]> Link: https://lore.kernel.org/r/[email protected] [mic: Move the small cleanups in the next commit] Signed-off-by: Mickaël Salaün <[email protected]>
1 parent 3872855 commit f51e55a

File tree

1 file changed

+38
-41
lines changed

1 file changed

+38
-41
lines changed

samples/landlock/sandboxer.c

Lines changed: 38 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,43 @@ static bool check_ruleset_scope(const char *const env_var,
290290

291291
#define LANDLOCK_ABI_LAST 6
292292

293+
#define XSTR(s) #s
294+
#define STR(s) XSTR(s)
295+
296+
/* clang-format off */
297+
298+
static const char help[] =
299+
"usage: "
300+
ENV_FS_RO_NAME "=\"...\" "
301+
ENV_FS_RW_NAME "=\"...\" "
302+
ENV_TCP_BIND_NAME "=\"...\" "
303+
ENV_TCP_CONNECT_NAME "=\"...\" "
304+
ENV_SCOPED_NAME "=\"...\" %1$s <cmd> [args]...\n"
305+
"\n"
306+
"Execute a command in a restricted environment.\n"
307+
"\n"
308+
"Environment variables containing paths and ports each separated by a colon:\n"
309+
"* " ENV_FS_RO_NAME ": list of paths allowed to be used in a read-only way.\n"
310+
"* " ENV_FS_RW_NAME ": list of paths allowed to be used in a read-write way.\n"
311+
"\n"
312+
"Environment variables containing ports are optional and could be skipped.\n"
313+
"* " ENV_TCP_BIND_NAME ": list of ports allowed to bind (server).\n"
314+
"* " ENV_TCP_CONNECT_NAME ": list of ports allowed to connect (client).\n"
315+
"* " ENV_SCOPED_NAME ": list of scoped IPCs.\n"
316+
"\n"
317+
"example:\n"
318+
ENV_FS_RO_NAME "=\"${PATH}:/lib:/usr:/proc:/etc:/dev/urandom\" "
319+
ENV_FS_RW_NAME "=\"/dev/null:/dev/full:/dev/zero:/dev/pts:/tmp\" "
320+
ENV_TCP_BIND_NAME "=\"9418\" "
321+
ENV_TCP_CONNECT_NAME "=\"80:443\" "
322+
ENV_SCOPED_NAME "=\"a:s\" "
323+
"%1$s bash -i\n"
324+
"\n"
325+
"This sandboxer can use Landlock features up to ABI version "
326+
STR(LANDLOCK_ABI_LAST) ".\n";
327+
328+
/* clang-format on */
329+
293330
int main(const int argc, char *const argv[], char *const *const envp)
294331
{
295332
const char *cmd_path;
@@ -308,47 +345,7 @@ int main(const int argc, char *const argv[], char *const *const envp)
308345
};
309346

310347
if (argc < 2) {
311-
fprintf(stderr,
312-
"usage: %s=\"...\" %s=\"...\" %s=\"...\" %s=\"...\" %s=\"...\" %s "
313-
"<cmd> [args]...\n\n",
314-
ENV_FS_RO_NAME, ENV_FS_RW_NAME, ENV_TCP_BIND_NAME,
315-
ENV_TCP_CONNECT_NAME, ENV_SCOPED_NAME, argv[0]);
316-
fprintf(stderr,
317-
"Execute a command in a restricted environment.\n\n");
318-
fprintf(stderr,
319-
"Environment variables containing paths and ports "
320-
"each separated by a colon:\n");
321-
fprintf(stderr,
322-
"* %s: list of paths allowed to be used in a read-only way.\n",
323-
ENV_FS_RO_NAME);
324-
fprintf(stderr,
325-
"* %s: list of paths allowed to be used in a read-write way.\n\n",
326-
ENV_FS_RW_NAME);
327-
fprintf(stderr,
328-
"Environment variables containing ports are optional "
329-
"and could be skipped.\n");
330-
fprintf(stderr,
331-
"* %s: list of ports allowed to bind (server).\n",
332-
ENV_TCP_BIND_NAME);
333-
fprintf(stderr,
334-
"* %s: list of ports allowed to connect (client).\n",
335-
ENV_TCP_CONNECT_NAME);
336-
fprintf(stderr, "* %s: list of scoped IPCs.\n",
337-
ENV_SCOPED_NAME);
338-
fprintf(stderr,
339-
"\nexample:\n"
340-
"%s=\"${PATH}:/lib:/usr:/proc:/etc:/dev/urandom\" "
341-
"%s=\"/dev/null:/dev/full:/dev/zero:/dev/pts:/tmp\" "
342-
"%s=\"9418\" "
343-
"%s=\"80:443\" "
344-
"%s=\"a:s\" "
345-
"%s bash -i\n\n",
346-
ENV_FS_RO_NAME, ENV_FS_RW_NAME, ENV_TCP_BIND_NAME,
347-
ENV_TCP_CONNECT_NAME, ENV_SCOPED_NAME, argv[0]);
348-
fprintf(stderr,
349-
"This sandboxer can use Landlock features "
350-
"up to ABI version %d.\n",
351-
LANDLOCK_ABI_LAST);
348+
fprintf(stderr, help, argv[0]);
352349
return 1;
353350
}
354351

0 commit comments

Comments
 (0)