libsmack: New functions handling file's attributes
authorJosé Bollo <jose.bollo@open.eurogiciel.org>
Thu, 9 Oct 2014 07:04:41 +0000 (09:04 +0200)
committerJosé Bollo <jose.bollo@open.eurogiciel.org>
Thu, 9 Oct 2014 08:04:28 +0000 (10:04 +0200)
The new functions are:
 - smack_new_label_from_file
 - smack_set_label_for_file
 - smack_remove_label_for_file

This functions allow to operate on opened
files using their linux file descritor for
reading, writing or destroying the named
file attribute given.

Change-Id: I454e96ca2eeb21e08a40c39c830e5b903875580b
Signed-off-by: José Bollo <jose.bollo@open.eurogiciel.org>
libsmack/libsmack.c
libsmack/libsmack.sym
libsmack/sys/smack.h

index 13fa0ab..2978de6 100644 (file)
@@ -648,6 +648,32 @@ ssize_t smack_new_label_from_path(const char *path, const char *xattr,
        return ret;
 }
 
+ssize_t smack_new_label_from_file(int fd, const char *xattr, 
+                                 char **label)
+{
+       char buf[SMACK_LABEL_LEN + 1];
+       char *result;
+       ssize_t ret = 0;
+
+       ret = fgetxattr(fd, xattr, buf, SMACK_LABEL_LEN + 1);
+       if (ret < 0)
+               return -1;
+       buf[ret] = '\0';
+
+       result = calloc(ret + 1, 1);
+       if (result == NULL)
+               return -1;
+
+       ret = get_label(result, buf, NULL);
+       if (ret < 0) {
+               free(result);
+               return -1;
+       }
+
+       *label = result;
+       return ret;
+}
+
 int smack_set_label_for_path(const char *path,
                                  const char *xattr,
                                  int follow,
@@ -666,6 +692,21 @@ int smack_set_label_for_path(const char *path,
        return ret;
 }
 
+int smack_set_label_for_file(int fd,
+                                 const char *xattr,
+                                 const char *label)
+{
+       int len;
+       int ret;
+
+       len = (int)smack_label_length(label);
+       if (len < 0)
+               return -2;
+
+       ret = fsetxattr(fd, xattr, label, len, 0);
+       return ret;
+}
+
 int smack_remove_label_for_path(const char *path,
                                  const char *xattr,
                                  int follow)
@@ -673,6 +714,11 @@ int smack_remove_label_for_path(const char *path,
        return follow ? removexattr(path, xattr) : lremovexattr(path, xattr);
 }
 
+int smack_remove_label_for_file(int fd, const char *xattr)
+{
+       return fremovexattr(fd, xattr);
+}
+
 int smack_set_label_for_self(const char *label)
 {
        int len;
index 2a0a627..a84f50d 100644 (file)
@@ -28,7 +28,10 @@ global:
        smack_label_length;
        smack_set_label_for_path;
        smack_remove_label_for_path;
-        smack_load_policy;
+       smack_load_policy;
+       smack_new_label_from_file;
+       smack_set_label_for_file;
+       smack_remove_label_for_file;
 local:
        *;
 } LIBSMACK_1.0;
index c099718..75828c5 100644 (file)
@@ -230,6 +230,20 @@ ssize_t smack_new_label_from_path(const char *path,
                                  char **label);
 
 /*!
+  * Get the SMACK label that is contained in an extended attribute.
+  * Caller is responsible of freeing the returned label.
+  *
+  * @param fd opened file descriptor of the file
+  * @param xattr the extended attribute containing the SMACK label
+  * @param label output variable for the returned label
+  * @return Returns length of the label on success and negative value
+  * on failure.
+  */
+ssize_t smack_new_label_from_file(int fd,
+                                 const char *xattr,
+                                 char **label);
+
+/*!
   * Set the SMACK label in an extended attribute.
   *
   * @param path path of the file
@@ -245,6 +259,19 @@ int smack_set_label_for_path(const char *path,
                                  const char *label);
 
 /*!
+  * Set the SMACK label in an extended attribute.
+  *
+  * @param fd opened file descriptor of the file
+  * @param xattr the extended attribute containing the SMACK label
+  * @param label output variable for the returned label
+  * @return Returns length of the label on success and negative value
+  * on failure.
+  */
+int smack_set_label_for_file(int fd,
+                                 const char *xattr,
+                                 const char *label);
+
+/*!
   * Remove the SMACK label in an extended attribute.
   *
   * @param path path of the file
@@ -257,6 +284,15 @@ int smack_remove_label_for_path(const char *path,
                                  int follow);
 
 /*!
+  * Remove the SMACK label in an extended attribute.
+  *
+  * @param fd opened file descriptor of the file
+  * @param xattr the extended attribute containing the SMACK label
+  * @return Returns 0 on success and negative on failure.
+  */
+int smack_remove_label_for_file(int fd, const char *xattr);
+
+/*!
  * Set the label associated with the callers process. The caller must have
  * CAP_MAC_ADMIN POSIX capability in order to do this.
  *