Simpler to use now from libcreds3.
#ifndef SMACK_H
#define SMACK_H
+#include <sys/types.h>
+
/*!
* Smack config file default paths.
*/
*
* @param path path to a file
* @param attr attribute name
- * @param smack current value
+ * @param smack attribute value
+ * @param size size of the character array reserved for the value
* @param labels label set. Not used if set to NULL. Otherwise, converts
* to long name.
* @return 0 on success
*/
-extern int smack_xattr_get_from_file(const char *path, const char *attr,
- char **smack, SmackLabelSet labels);
+extern ssize_t smack_xattr_get_from_file(const char *path, const char *attr,
+ char *smack, size_t size,
+ SmackLabelSet labels);
/*!
* Get SMACK64 security attribute for a given pid.
*
* @param pid pid of a process
- * @param smack current value
+ * @param smack attribute value
+ * @param size size of the character array reserved for the value
* @param labels label set. Not used if set to NULL. Otherwise, converts
* to long name.
* @return 0 on success
*/
-extern int smack_xattr_get_from_proc(int pid, char **smack,
- SmackLabelSet labels);
+extern ssize_t smack_xattr_get_from_proc(int pid, char *smack,
+ size_t size,
+ SmackLabelSet labels);
#ifdef __cplusplus
}
return ret;
}
-int smack_xattr_get_from_file(const char *path, const char *attr,
- char **smack, SmackLabelSet labels)
+ssize_t smack_xattr_get_from_file(const char *path, const char *attr,
+ char *smack, size_t size, SmackLabelSet labels)
{
ssize_t ret;
- char short_name[SMACK64_LEN + 2];
+ char buf[SMACK64_LEN + 2];
const char *result;
+ size_t rsize;
- ret = getxattr(path, attr, short_name, SMACK64_LEN + 1);
+ ret = getxattr(path, attr, buf, SMACK64_LEN + 1);
if (ret < 0)
return -1;
- short_name[ret] = '\0';
+ buf[ret] = '\0';
if (labels == NULL)
- result = short_name;
+ result = buf;
else
- result = smack_label_set_to_long_name(labels, short_name);
+ result = smack_label_set_to_long_name(labels, buf);
if (result == NULL)
return -1;
- *smack = strdup(result);
- if (*smack == NULL)
+ rsize = strlen(result) + 1;
+
+ if (smack == NULL)
+ return rsize;
+ else if (size < rsize)
return -1;
+ strcpy(smack, result);
+
return 0;
}
-int smack_xattr_get_from_proc(int pid, char **smack, SmackLabelSet labels)
+ssize_t smack_xattr_get_from_proc(int pid, char *smack,
+ size_t size,
+ SmackLabelSet labels)
{
char buf[512];
FILE *file;
+ const char *result;
+ size_t rsize;
- snprintf(buf, sizeof(buf), SMACK_PROC_PATH, pid);
+ snprintf(buf, sizeof(result), SMACK_PROC_PATH, pid);
file = fopen(buf, "r");
if (file == NULL)
return -1;
- if (fgets(buf, sizeof(buf), file) == NULL) {
+ if (fgets(buf, sizeof(result), file) == NULL) {
fclose(file);
return -1;
}
fclose(file);
- *smack = strdup(buf);
- return *smack != NULL ? 0 : - 1;
+
+ if (labels == NULL)
+ result = buf;
+ else
+ result = smack_label_set_to_long_name(labels, buf);
+
+ if (result == NULL)
+ return -1;
+
+ rsize = strlen(result) + 1;
+
+ if (smack == NULL)
+ return rsize;
+ else if (size < rsize)
+ return -1;
+
+ strcpy(smack, result);
+ return 0;
}
{
FILE *file;
int rc = 0;
- char *smack = NULL;
+ char smack[100];
file = fopen("set_smack-dummy.txt", "w");
fprintf(file, "dummy\n");
rc = smack_xattr_set_to_file("set_smack-dummy.txt", SMACK64, "Apple", NULL);
fail_unless(rc == 0, "Failed to set SMACK64");
- rc = smack_xattr_get_from_file("set_smack-dummy.txt", SMACK64, &smack, NULL);
+ rc = smack_xattr_get_from_file("set_smack-dummy.txt", SMACK64, smack, 100, NULL);
fail_unless(rc == 0, "Failed to get SMACK64");
rc = strcmp(smack, "Apple");
fail_unless(rc == 0, "smack %s not equal to Apple", smack);
-
- free(smack);
}
END_TEST
{
FILE *file;
int rc;
- char *smack = NULL;
+ char smack[100];
file = fopen("set_smack-dummy.txt", "w");
fprintf(file, "dummy\n");
rc = smack_xattr_set_to_file("set_smack-dummy.txt", SMACK64EXEC, "Apple", NULL);
fail_unless(rc == 0, "Failed to set SMACK64EXEC");
- rc = smack_xattr_get_from_file("set_smack-dummy.txt", SMACK64EXEC, &smack, NULL);
+ rc = smack_xattr_get_from_file("set_smack-dummy.txt", SMACK64EXEC, smack, 100, NULL);
fail_unless(rc == 0, "Failed to get SMACK64EXEC");
rc = strcmp(smack, "Apple");
fail_unless(rc == 0, "smack %s not equal to Apple", smack);
-
- free(smack);
}
END_TEST
FILE *file;
int rc = 0;
SmackLabelSet labels;
- char *smack = NULL;
+ char smack[100];
file = fopen("set_smack-dummy.txt", "w");
fprintf(file, "dummy\n");
rc = smack_xattr_set_to_file("set_smack-dummy.txt", SMACK64, LONG_LABEL_1, labels);
fail_unless(rc == 0, "Failed to set SMACK64");
- rc = smack_xattr_get_from_file("set_smack-dummy.txt", SMACK64, &smack, labels);
+ rc = smack_xattr_get_from_file("set_smack-dummy.txt", SMACK64, smack, 100, labels);
fail_unless(rc == 0, "Failed to get SMACK64");
rc = strcmp(smack, LONG_LABEL_1);
fail_unless(rc == 0, "smack %s not equal to Apple", smack);
- free(smack);
-
smack_label_set_delete(labels);
}
END_TEST