From 45ea537cf2fc7d9804f510e823a8c340db52f7a4 Mon Sep 17 00:00:00 2001 From: Michael Kerrisk Date: Thu, 12 Aug 2021 05:16:42 +0200 Subject: [PATCH] mount_setattr.2: EXAMPLES: use -1 rather than -EBADF From email with Christian Braner: > [1]: In this code "source" is expected to be absolute. If it's not > absolute we should fail. This can be achieved by passing -1/-EBADF, > afaict. D'oh! Okay. I hadn't considered that use case for an invalid dirfd. (And now I've done some adjustments to openat(2),which contains a rationale for the *at() functions.) So, now I understand your purpose, but still the code is obscure, since * You use a magic value (-EBADF) rather than (say) -1. * There's no explanation (comment about) of the fact that you want to prevent relative pathnames. So, I've changed the code to use -1, not -EBADF, and I've added some comments to explain that the intent is to prevent relative pathnames. Signed-off-by: Michael Kerrisk --- man2/mount_setattr.2 | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/man2/mount_setattr.2 b/man2/mount_setattr.2 index 0739624cc..6ffe5e009 100644 --- a/man2/mount_setattr.2 +++ b/man2/mount_setattr.2 @@ -961,7 +961,10 @@ main(int argc, char *argv[]) const char *source = argv[optind]; const char *target = argv[optind + 1]; - int fd_tree = open_tree(\-EBADF, source, + /* In the following, \-1 as the \(aqdirfd\(aq argument ensures that + open_tree() fails if \(aqsource\(aq is not an absolute pathname. */ + + int fd_tree = open_tree(\-1, source, OPEN_TREE_CLONE | OPEN_TREE_CLOEXEC | AT_EMPTY_PATH | (recursive ? AT_RECURSIVE : 0)); if (fd_tree == \-1) @@ -980,7 +983,10 @@ main(int argc, char *argv[]) close(fd_userns); - ret = move_mount(fd_tree, "", \-EBADF, target, + /* In the following, \-1 as the \(aqto_dirfd\(aq argument ensures that + open_tree() fails if \(aqtarget\(aq is not an absolute pathname. */ + + ret = move_mount(fd_tree, "", \-1, target, MOVE_MOUNT_F_EMPTY_PATH); if (ret == \-1) exit_log("%m \- Failed to attach mount to %s\en", target);