Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 21c7eae2 authored by Lukasz Pawelczyk's avatar Lukasz Pawelczyk Committed by Casey Schaufler
Browse files

Make Smack operate on smack_known struct where it still used char*



Smack used to use a mix of smack_known struct and char* throughout its
APIs and implementation. This patch unifies the behaviour and makes it
store and operate exclusively on smack_known struct pointers when managing
labels.

Signed-off-by: default avatarLukasz Pawelczyk <l.pawelczyk@samsung.com>

Conflicts:
	security/smack/smack_access.c
	security/smack/smack_lsm.c
parent d0175790
Loading
Loading
Loading
Loading
+17 −17
Original line number Diff line number Diff line
@@ -71,10 +71,10 @@ struct smack_known {
#define SMK_CIPSOLEN	24

struct superblock_smack {
	char		*smk_root;
	char		*smk_floor;
	char		*smk_hat;
	char		*smk_default;
	struct smack_known	*smk_root;
	struct smack_known	*smk_floor;
	struct smack_known	*smk_hat;
	struct smack_known	*smk_default;
	int			smk_initialized;
};

@@ -88,7 +88,7 @@ struct socket_smack {
 * Inode smack data
 */
struct inode_smack {
	char			*smk_inode;	/* label of the fso */
	struct smack_known	*smk_inode;	/* label of the fso */
	struct smack_known	*smk_task;	/* label of the task */
	struct smack_known	*smk_mmap;	/* label of the mmap domain */
	struct mutex		smk_lock;	/* initialization lock */
@@ -112,7 +112,7 @@ struct task_smack {
struct smack_rule {
	struct list_head	list;
	struct smack_known	*smk_subject;
	char			*smk_object;
	struct smack_known	*smk_object;
	int			smk_access;
};

@@ -123,7 +123,7 @@ struct smk_netlbladdr {
	struct list_head	list;
	struct sockaddr_in	smk_host;	/* network address */
	struct in_addr		smk_mask;	/* network mask */
	char			*smk_label;	/* label */
	struct smack_known	*smk_label;	/* label */
};

/*
@@ -227,23 +227,23 @@ struct smk_audit_info {
/*
 * These functions are in smack_lsm.c
 */
struct inode_smack *new_inode_smack(char *);
struct inode_smack *new_inode_smack(struct smack_known *);

/*
 * These functions are in smack_access.c
 */
int smk_access_entry(char *, char *, struct list_head *);
int smk_access(struct smack_known *, char *, int, struct smk_audit_info *);
int smk_tskacc(struct task_smack *, char *, u32, struct smk_audit_info *);
int smk_curacc(char *, u32, struct smk_audit_info *);
int smk_access(struct smack_known *, struct smack_known *,
	       int, struct smk_audit_info *);
int smk_tskacc(struct task_smack *, struct smack_known *,
	       u32, struct smk_audit_info *);
int smk_curacc(struct smack_known *, u32, struct smk_audit_info *);
struct smack_known *smack_from_secid(const u32);
char *smk_parse_smack(const char *string, int len);
int smk_netlbl_mls(int, char *, struct netlbl_lsm_secattr *, int);
char *smk_import(const char *, int);
struct smack_known *smk_import_entry(const char *, int);
void smk_insert_entry(struct smack_known *skp);
struct smack_known *smk_find_entry(const char *);
u32 smack_to_secid(const char *);

/*
 * Shared data.
@@ -253,7 +253,7 @@ extern int smack_cipso_mapped;
extern struct smack_known *smack_net_ambient;
extern struct smack_known *smack_onlycap;
extern struct smack_known *smack_syslog_label;
extern const char *smack_cipso_option;
extern struct smack_known smack_cipso_option;
extern int smack_ptrace_rule;

extern struct smack_known smack_known_floor;
@@ -282,9 +282,9 @@ static inline int smk_inode_transmutable(const struct inode *isp)
}

/*
 * Present a pointer to the smack label in an inode blob.
 * Present a pointer to the smack label entry in an inode blob.
 */
static inline char *smk_of_inode(const struct inode *isp)
static inline struct smack_known *smk_of_inode(const struct inode *isp)
{
	struct inode_smack *sip = isp->i_security;
	return sip->smk_inode;
+30 −64
Original line number Diff line number Diff line
@@ -94,7 +94,7 @@ int smk_access_entry(char *subject_label, char *object_label,
	struct smack_rule *srp;

	list_for_each_entry_rcu(srp, rule_list, list) {
		if (srp->smk_object == object_label &&
		if (srp->smk_object->smk_known == object_label &&
		    srp->smk_subject->smk_known == subject_label) {
			may = srp->smk_access;
			break;
@@ -111,8 +111,8 @@ int smk_access_entry(char *subject_label, char *object_label,

/**
 * smk_access - determine if a subject has a specific access to an object
 * @subject_known: a pointer to the subject's Smack label entry
 * @object_label: a pointer to the object's Smack label
 * @subject: a pointer to the subject's Smack label entry
 * @object: a pointer to the object's Smack label entry
 * @request: the access requested, in "MAY" format
 * @a : a pointer to the audit data
 *
@@ -122,7 +122,7 @@ int smk_access_entry(char *subject_label, char *object_label,
 *
 * Smack labels are shared on smack_list
 */
int smk_access(struct smack_known *subject_known, char *object_label,
int smk_access(struct smack_known *subject, struct smack_known *object,
	       int request, struct smk_audit_info *a)
{
	int may = MAY_NOT;
@@ -133,7 +133,7 @@ int smk_access(struct smack_known *subject_known, char *object_label,
	 *
	 * A star subject can't access any object.
	 */
	if (subject_known == &smack_known_star) {
	if (subject == &smack_known_star) {
		rc = -EACCES;
		goto out_audit;
	}
@@ -142,28 +142,28 @@ int smk_access(struct smack_known *subject_known, char *object_label,
	 * Tasks cannot be assigned the internet label.
	 * An internet subject can access any object.
	 */
	if (object_label == smack_known_web.smk_known ||
	    subject_known == &smack_known_web)
	if (object == &smack_known_web ||
	    subject == &smack_known_web)
		goto out_audit;
	/*
	 * A star object can be accessed by any subject.
	 */
	if (object_label == smack_known_star.smk_known)
	if (object == &smack_known_star)
		goto out_audit;
	/*
	 * An object can be accessed in any way by a subject
	 * with the same label.
	 */
	if (subject_known->smk_known == object_label)
	if (subject->smk_known == object->smk_known)
		goto out_audit;
	/*
	 * A hat subject can read any object.
	 * A floor object can be read by any subject.
	 */
	if ((request & MAY_ANYREAD) == request) {
		if (object_label == smack_known_floor.smk_known)
		if (object == &smack_known_floor)
			goto out_audit;
		if (subject_known == &smack_known_hat)
		if (subject == &smack_known_hat)
			goto out_audit;
	}
	/*
@@ -174,8 +174,8 @@ int smk_access(struct smack_known *subject_known, char *object_label,
	 * indicates there is no entry for this pair.
	 */
	rcu_read_lock();
	may = smk_access_entry(subject_known->smk_known, object_label,
				&subject_known->smk_rules);
	may = smk_access_entry(subject->smk_known, object->smk_known,
			       &subject->smk_rules);
	rcu_read_unlock();

	if (may <= 0 || (request & may) != request) {
@@ -195,8 +195,8 @@ int smk_access(struct smack_known *subject_known, char *object_label,
out_audit:
#ifdef CONFIG_AUDIT
	if (a)
		smack_log(subject_known->smk_known, object_label, request,
				rc, a);
		smack_log(subject->smk_known, object->smk_known,
			  request, rc, a);
#endif

	return rc;
@@ -204,8 +204,8 @@ int smk_access(struct smack_known *subject_known, char *object_label,

/**
 * smk_tskacc - determine if a task has a specific access to an object
 * @tsp: a pointer to the subject task
 * @obj_label: a pointer to the object's Smack label
 * @tsp: a pointer to the subject's task
 * @obj_known: a pointer to the object's label entry
 * @mode: the access requested, in "MAY" format
 * @a : common audit data
 *
@@ -214,24 +214,25 @@ int smk_access(struct smack_known *subject_known, char *object_label,
 * non zero otherwise. It allows that the task may have the capability
 * to override the rules.
 */
int smk_tskacc(struct task_smack *subject, char *obj_label,
int smk_tskacc(struct task_smack *tsp, struct smack_known *obj_known,
	       u32 mode, struct smk_audit_info *a)
{
	struct smack_known *skp = smk_of_task(subject);
	struct smack_known *sbj_known = smk_of_task(tsp);
	int may;
	int rc;

	/*
	 * Check the global rule list
	 */
	rc = smk_access(skp, obj_label, mode, NULL);
	rc = smk_access(sbj_known, obj_known, mode, NULL);
	if (rc >= 0) {
		/*
		 * If there is an entry in the task's rule list
		 * it can further restrict access.
		 */
		may = smk_access_entry(skp->smk_known, obj_label,
					&subject->smk_rules);
		may = smk_access_entry(sbj_known->smk_known,
				       obj_known->smk_known,
				       &tsp->smk_rules);
		if (may < 0)
			goto out_audit;
		if ((mode & may) == mode)
@@ -248,14 +249,15 @@ int smk_tskacc(struct task_smack *subject, char *obj_label,
out_audit:
#ifdef CONFIG_AUDIT
	if (a)
		smack_log(skp->smk_known, obj_label, mode, rc, a);
		smack_log(sbj_known->smk_known, obj_known->smk_known,
			  mode, rc, a);
#endif
	return rc;
}

/**
 * smk_curacc - determine if current has a specific access to an object
 * @obj_label: a pointer to the object's Smack label
 * @obj_known: a pointer to the object's Smack label entry
 * @mode: the access requested, in "MAY" format
 * @a : common audit data
 *
@@ -264,11 +266,12 @@ int smk_tskacc(struct task_smack *subject, char *obj_label,
 * non zero otherwise. It allows that current may have the capability
 * to override the rules.
 */
int smk_curacc(char *obj_label, u32 mode, struct smk_audit_info *a)
int smk_curacc(struct smack_known *obj_known,
	       u32 mode, struct smk_audit_info *a)
{
	struct task_smack *tsp = current_security();

	return smk_tskacc(tsp, obj_label, mode, a);
	return smk_tskacc(tsp, obj_known, mode, a);
}

#ifdef CONFIG_AUDIT
@@ -561,27 +564,6 @@ struct smack_known *smk_import_entry(const char *string, int len)
	return skp;
}

/**
 * smk_import - import a smack label
 * @string: a text string that might be a Smack label
 * @len: the maximum size, or zero if it is NULL terminated.
 *
 * Returns a pointer to the label in the label list that
 * matches the passed string, adding it if necessary.
 */
char *smk_import(const char *string, int len)
{
	struct smack_known *skp;

	/* labels cannot begin with a '-' */
	if (string[0] == '-')
		return NULL;
	skp = smk_import_entry(string, len);
	if (skp == NULL)
		return NULL;
	return skp->smk_known;
}

/**
 * smack_from_secid - find the Smack label associated with a secid
 * @secid: an integer that might be associated with a Smack label
@@ -608,19 +590,3 @@ struct smack_known *smack_from_secid(const u32 secid)
	rcu_read_unlock();
	return &smack_known_invalid;
}

/**
 * smack_to_secid - find the secid associated with a Smack label
 * @smack: the Smack label
 *
 * Returns the appropriate secid if there is one,
 * otherwise 0
 */
u32 smack_to_secid(const char *smack)
{
	struct smack_known *skp = smk_find_entry(smack);

	if (skp == NULL)
		return 0;
	return skp->smk_secid;
}
+153 −146

File changed.

Preview size limit exceeded, changes collapsed.

+33 −28
Original line number Diff line number Diff line
@@ -131,14 +131,17 @@ LIST_HEAD(smack_rule_list);

struct smack_parsed_rule {
	struct smack_known	*smk_subject;
	char			*smk_object;
	struct smack_known	*smk_object;
	int			smk_access1;
	int			smk_access2;
};

static int smk_cipso_doi_value = SMACK_CIPSO_DOI_DEFAULT;

const char *smack_cipso_option = SMACK_CIPSO_OPTION;
struct smack_known smack_cipso_option = {
	.smk_known	= SMACK_CIPSO_OPTION,
	.smk_secid	= 0,
};

/*
 * Values for parsing cipso rules
@@ -339,7 +342,7 @@ static int smk_fill_rule(const char *subject, const char *object,
		if (rule->smk_subject == NULL)
			return -EINVAL;

		rule->smk_object = smk_import(object, len);
		rule->smk_object = smk_import_entry(object, len);
		if (rule->smk_object == NULL)
			return -EINVAL;
	} else {
@@ -359,7 +362,7 @@ static int smk_fill_rule(const char *subject, const char *object,
		kfree(cp);
		if (skp == NULL)
			return -ENOENT;
		rule->smk_object = skp->smk_known;
		rule->smk_object = skp;
	}

	rule->smk_access1 = smk_perm_from_str(access1);
@@ -598,13 +601,15 @@ static void smk_rule_show(struct seq_file *s, struct smack_rule *srp, int max)
	 * anything you read back.
	 */
	if (strlen(srp->smk_subject->smk_known) >= max ||
	    strlen(srp->smk_object) >= max)
	    strlen(srp->smk_object->smk_known) >= max)
		return;

	if (srp->smk_access == 0)
		return;

	seq_printf(s, "%s %s", srp->smk_subject->smk_known, srp->smk_object);
	seq_printf(s, "%s %s",
		   srp->smk_subject->smk_known,
		   srp->smk_object->smk_known);

	seq_putc(s, ' ');

@@ -1073,7 +1078,7 @@ static int netlbladdr_seq_show(struct seq_file *s, void *v)
	for (maskn = 0; temp_mask; temp_mask <<= 1, maskn++);

	seq_printf(s, "%u.%u.%u.%u/%d %s\n",
		hp[0], hp[1], hp[2], hp[3], maskn, skp->smk_label);
		hp[0], hp[1], hp[2], hp[3], maskn, skp->smk_label->smk_known);

	return 0;
}
@@ -1153,10 +1158,10 @@ static void smk_netlbladdr_insert(struct smk_netlbladdr *new)
static ssize_t smk_write_netlbladdr(struct file *file, const char __user *buf,
				size_t count, loff_t *ppos)
{
	struct smk_netlbladdr *skp;
	struct smk_netlbladdr *snp;
	struct sockaddr_in newname;
	char *smack;
	char *sp;
	struct smack_known *skp;
	char *data;
	char *host = (char *)&newname.sin_addr.s_addr;
	int rc;
@@ -1219,15 +1224,15 @@ static ssize_t smk_write_netlbladdr(struct file *file, const char __user *buf,
	 * If smack begins with '-', it is an option, don't import it
	 */
	if (smack[0] != '-') {
		sp = smk_import(smack, 0);
		if (sp == NULL) {
		skp = smk_import_entry(smack, 0);
		if (skp == NULL) {
			rc = -EINVAL;
			goto free_out;
		}
	} else {
		/* check known options */
		if (strcmp(smack, smack_cipso_option) == 0)
			sp = (char *)smack_cipso_option;
		if (strcmp(smack, smack_cipso_option.smk_known) == 0)
			skp = &smack_cipso_option;
		else {
			rc = -EINVAL;
			goto free_out;
@@ -1250,9 +1255,9 @@ static ssize_t smk_write_netlbladdr(struct file *file, const char __user *buf,
	nsa = newname.sin_addr.s_addr;
	/* try to find if the prefix is already in the list */
	found = 0;
	list_for_each_entry_rcu(skp, &smk_netlbladdr_list, list) {
		if (skp->smk_host.sin_addr.s_addr == nsa &&
		    skp->smk_mask.s_addr == mask.s_addr) {
	list_for_each_entry_rcu(snp, &smk_netlbladdr_list, list) {
		if (snp->smk_host.sin_addr.s_addr == nsa &&
		    snp->smk_mask.s_addr == mask.s_addr) {
			found = 1;
			break;
		}
@@ -1260,26 +1265,26 @@ static ssize_t smk_write_netlbladdr(struct file *file, const char __user *buf,
	smk_netlabel_audit_set(&audit_info);

	if (found == 0) {
		skp = kzalloc(sizeof(*skp), GFP_KERNEL);
		if (skp == NULL)
		snp = kzalloc(sizeof(*snp), GFP_KERNEL);
		if (snp == NULL)
			rc = -ENOMEM;
		else {
			rc = 0;
			skp->smk_host.sin_addr.s_addr = newname.sin_addr.s_addr;
			skp->smk_mask.s_addr = mask.s_addr;
			skp->smk_label = sp;
			smk_netlbladdr_insert(skp);
			snp->smk_host.sin_addr.s_addr = newname.sin_addr.s_addr;
			snp->smk_mask.s_addr = mask.s_addr;
			snp->smk_label = skp;
			smk_netlbladdr_insert(snp);
		}
	} else {
		/* we delete the unlabeled entry, only if the previous label
		 * wasn't the special CIPSO option */
		if (skp->smk_label != smack_cipso_option)
		if (snp->smk_label != &smack_cipso_option)
			rc = netlbl_cfg_unlbl_static_del(&init_net, NULL,
					&skp->smk_host.sin_addr, &skp->smk_mask,
					&snp->smk_host.sin_addr, &snp->smk_mask,
					PF_INET, &audit_info);
		else
			rc = 0;
		skp->smk_label = sp;
		snp->smk_label = skp;
	}

	/*
@@ -1287,10 +1292,10 @@ static ssize_t smk_write_netlbladdr(struct file *file, const char __user *buf,
	 * this host so that incoming packets get labeled.
	 * but only if we didn't get the special CIPSO option
	 */
	if (rc == 0 && sp != smack_cipso_option)
	if (rc == 0 && skp != &smack_cipso_option)
		rc = netlbl_cfg_unlbl_static_add(&init_net, NULL,
			&skp->smk_host.sin_addr, &skp->smk_mask, PF_INET,
			smack_to_secid(skp->smk_label), &audit_info);
			&snp->smk_host.sin_addr, &snp->smk_mask, PF_INET,
			snp->smk_label->smk_secid, &audit_info);

	if (rc == 0)
		rc = count;