[Toybox] cp --preserve questions.

Rob Landley rob at landley.net
Sun Dec 27 12:37:18 PST 2015


I recently went through Jose's cp --preserve patch from way back when, and hit:

+  if ((listlen = flistxattr(fdin, NULL, 0)) > 0 && (list = malloc(listlen))) {
+    flistxattr(fdin, list, listlen);
+    for (name = list ; (name - list) < listlen ; name += strlen(name)+1) {
+      /* test if the xattribute have to be copied */
+      if ((TT.preserve & p_xattr)
+      || ((TT.preserve & p_mode) &&
+              !strncmp(name,"system.posix_acl_",sizeof("system.posix_acl_")-1))
+      || ((TT.preserve & p_context) &&
+              !strncmp(name,"security.",sizeof("security.")-1)))
+      {

Questions:

1) Is the "security." prefix good enough for both smack and selinux?

2) You call flistxattr() to grab the length, then call flistxattr() again
to fetch the data. Anybody else spot the race condition of an attribute
being added between the two calls? If you increase the size of an existing
xattr, will it be properly null terminate the last entry?

3) p_mode means you're preserving the suid/sgid/sticky bits. I.E. you're
calling fchown() on the thing to feed it bits you can't set in open().
Why is there now a SECOND meaning, having to do with xattrs, meaning
simply doing cp -p is now doing all these extra xattr system calls
to check for "system.posix_acl_"?

4) It's only copying xattrs for files. Not for directories or anything
else. Is this normal? (It's doing it because there's no openat() versions
of these commands, see "nobody's updated this API since the 90's" rant
earlier...)

Anyway, I've done a first pass. Haven't got an environment set up to test
it, but it compiled and "scripts/test.sh cp" didn't regress.

Rob



More information about the Toybox mailing list