30

Which permissions affect hard link creation? Does file ownership itself matter?


Suppose user alice wants to create a hard link to the file target.txt in a directory target-dir.

  • Which permissions does alice need on both target.txt and target-dir?
  • If target.txt is owned by user bill and target-dir is owned by user chad, does that change anything?

I've tried to simulate this situation by creating the following folder/file structure on an ext4 filesystem:

#> ls -lh . *
.:
drwxr-xr-x 2 bill bill 60 Oct  1 11:29 source-dir
drwxrwxrwx 2 chad chad 60 Oct  1 11:40 target-dir

source-dir:
-r--r--r-- 1 bill bill 0 Oct  1 11:29 target.txt

target-dir:
-rw-rw-r-- 1 alice alice 0 Oct  1 11:40 dummy

While alice can create a soft link to target.txt, she can't create a hard link:

#> ln source-dir/target.txt target-dir/
ln: failed to create hard link ‘target-dir/target.txt’ => ‘source-dir/target.txt’: Operation not permitted

If alice owns target.txt and no permissions are changed, the hard link succeeds. What am I missing here?

4
  • Is this for an assignment? It may affect the kind of answers some will choose to give if so (not that it would be out of bounds to ask a question about an assignment of course). Anyway, have you tried just creating that scenario and seeing what happens? Commented Oct 1, 2015 at 13:17
  • No it's not for an assignment. I'm facing this exact scenario, and have some guesses, but I thought it would be better to ask from a theoretical standpoint. In my case i'm unable to create the hard-link regardless of target.txt and target-dir permissions, unless I sudo as root.
    – gcscaglia
    Commented Oct 1, 2015 at 13:20
  • Hard links share the same ownership and permissions, so it's the same as accessing the original file.
    – teppic
    Commented Oct 1, 2015 at 17:29
  • @teppic My difficult is not accessing the created hard-link, it is creating the hard-link as alice.I can access both the original file and a hard-link to it (created with sudo), but I can't create the link as the user alice despite we all agreeing these permissions should be enough for it.
    – gcscaglia
    Commented Oct 1, 2015 at 18:38

2 Answers 2

35

To create the hard link, alice will need write+execute permissions on target-dir on all cases. The permissions needed on target.txt will vary:

  • If fs.protected_hardlinks = 1 then alice needs either ownership of target.txt or at least read+write permissions on it.
  • If fs.protected_hardlinks = 0 then any set of permissions will do; Even 000 is okay.

This answer to a similar question had the missing piece of information to answer this question.

From this commit message (emphasis mine):

On systems that have user-writable directories on the same partition as system files, a long-standing class of security issues is the hardlink-based time-of-check-time-of-use race, most commonly seen in world-writable directories like /tmp. The common method of exploitation of this flaw is to cross privilege boundaries when following a given hardlink (i.e. a root process follows a hardlink created by another user). Additionally, an issue exists where users can "pin" a potentially vulnerable setuid/setgid file so that an administrator will not actually upgrade a system fully.

The solution is to permit hardlinks to only be created when the user is already the existing file's owner, or if they already have read/write access to the existing file.

1
  • 2
    Why would I get "operation not permitted" even when I have rwx perms on the dir and rw perms on the file?
    – Michael
    Commented May 30, 2019 at 22:56
1

alice needs at least read permission on target.txt and write+execute permission on target-dir.

Now, the permission structure works as a threefold separated set:

  1. User permissions: apply to the user that owns the node.
  2. Group permissions: apply to any user belonging to the group that owns the node.
  3. Others' permissions: apply to any other user/group not owning the node.

Therefore, the ownership question affects only in which set of permissions the required permissions for alice are, being:

  • If alice is the owner user, the required permissions must be in the "user" part.
  • If alice is part of the group that owns it, the required permissions must be in the "group" part.
  • If alice does not own it and is not part of the group that owns it, the required permissions must be in the "other" part.
4
  • I thought so as well. But in my situation target.txt has 444 permissions and target-dir has 777 permissions yet I'm unable to create a hard-link unless I do so as root or I own the file I'm linking to. Oddly enough, a soft-link will work as you describe.
    – gcscaglia
    Commented Oct 1, 2015 at 14:41
  • That information would be useful to have in the question itself, I recommend you to update. Oddly enough I didn't think of it before... but does the file system where you are working support Unix-like ownerships/permissions, to start with? Commented Oct 1, 2015 at 14:47
  • Yes it does, it is an out-of-the-box ext4 on a fedora 21 system; The link do works when I'm the owner of the file.
    – gcscaglia
    Commented Oct 1, 2015 at 14:57
  • Oddly enough, a soft-link will work as you describe. A soft-link is nothing more than a file of a special type with some text. The text is the path pointed to by the link. You can put arbitrary text there: ln -s 'this is my text' my-soft-link works just fine. Since it's just a file with a special type, you only need write permission to the directory where you create the soft-link. Commented May 3, 2022 at 12:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .