I'm trying to set some extended attributes on a symbolic link on Fedora 15.

According to setfattr's usage, there is an option -h for such purpose:

setfattr 2.4.44 -- set extended attributes
Usage: setfattr {-n name} [-v value] [-h] file...
       setfattr {-x name} [-h] file...
  -n, --name=name         set the value of the named extended attribute
  -x, --remove=name       remove the named extended attribute
  -v, --value=value       use value as the attribute value
  -h, --no-dereference    do not dereference symbolic links
      --restore=file      restore extended attributes
      --version           print version and exit
      --help              this help text

However, the option doesn't seem to work. Using -h on symlink files just reports Operation not permitted without setting the extended attribute.

For example:

[dummy@notebook test]$ ls -l
total 0
-rw-rw-r-- 1 dummy dummy 0 Jul 12 14:35 file
lrwxrwxrwx 1 dummy dummy 6 Jul 12 14:35 link -> ./file
[dummy@notebook test]$ setfattr -n user.name -v value1 file
[dummy@notebook test]$ getfattr -n user.name file
# file: file

[dummy@notebook test]$ setfattr -n user.name -v value2 link
[dummy@notebook test]$ getfattr -n user.name file
# file: file

[dummy@notebook test]$ setfattr -n user.name -v value3 -h link
setfattr: link: Operation not permitted
[dummy@notebook test]$ getfattr -n user.name -h link
link: user.name: Operation not permitted

Why is this?


I found this comment in fs/xattr.c:

/* In user.* namespace, only regular files and directories can have
 * extended attributes. For sticky directories, only the owner and
 * privileged user can write attributes.

There you have it; the kernel won't allow setting attributes in user namespace on anything but a regular file or directory.

xattr(7) provides more details:

Extended user attributes

Extended user attributes may be assigned to files and directories for
storing arbitrary additional information such as the mime type,
character set or encoding of a file.  The access permissions for user
attributes are defined by the file permission bits: read permission
is required to retrieve the attribute value, and writer permission is
required to change it.

The file permission bits of regular files and directories are
interpreted differently from the file permission bits of special
files and symbolic links.  For regular files and directories the file
permission bits define access to the file's contents, while for
device special files they define access to the device described by
the special file.  The file permissions of symbolic links are not
used in access checks.  These differences would allow users to
consume filesystem resources in a way not controllable by disk quotas
for group or world writable special files and directories.

For this reason, extended user attributes are allowed only for
regular files and directories, and access to extended user attributes
is restricted to the owner and to users with appropriate capabilities
for directories with the sticky bit set (see the chmod(1) manual page
for an explanation of the sticky bit).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy