Skip to content

Commit 6f2f724

Browse files
cschauflerpcmoore
authored andcommitted
lsm: add lsmprop_to_secctx hook
Add a new hook security_lsmprop_to_secctx() and its LSM specific implementations. The LSM specific code will use the lsm_prop element allocated for that module. This allows for the possibility that more than one module may be called upon to translate a secid to a string, as can occur in the audit code. Signed-off-by: Casey Schaufler <casey@schaufler-ca.com> [PM: subject line tweak] Signed-off-by: Paul Moore <paul@paul-moore.com>
1 parent 870b7fd commit 6f2f724

File tree

9 files changed

+100
-11
lines changed

9 files changed

+100
-11
lines changed

include/linux/lsm_hook_defs.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,8 @@ LSM_HOOK(int, -EINVAL, setprocattr, const char *name, void *value, size_t size)
294294
LSM_HOOK(int, 0, ismaclabel, const char *name)
295295
LSM_HOOK(int, -EOPNOTSUPP, secid_to_secctx, u32 secid, char **secdata,
296296
u32 *seclen)
297+
LSM_HOOK(int, -EOPNOTSUPP, lsmprop_to_secctx, struct lsm_prop *prop,
298+
char **secdata, u32 *seclen)
297299
LSM_HOOK(int, 0, secctx_to_secid, const char *secdata, u32 seclen, u32 *secid)
298300
LSM_HOOK(void, LSM_RET_VOID, release_secctx, char *secdata, u32 seclen)
299301
LSM_HOOK(void, LSM_RET_VOID, inode_invalidate_secctx, struct inode *inode)

include/linux/security.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,7 @@ int security_setprocattr(int lsmid, const char *name, void *value, size_t size);
535535
int security_netlink_send(struct sock *sk, struct sk_buff *skb);
536536
int security_ismaclabel(const char *name);
537537
int security_secid_to_secctx(u32 secid, char **secdata, u32 *seclen);
538+
int security_lsmprop_to_secctx(struct lsm_prop *prop, char **secdata, u32 *seclen);
538539
int security_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid);
539540
void security_release_secctx(char *secdata, u32 seclen);
540541
void security_inode_invalidate_secctx(struct inode *inode);
@@ -1488,7 +1489,14 @@ static inline int security_ismaclabel(const char *name)
14881489
return 0;
14891490
}
14901491

1491-
static inline int security_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
1492+
static inline int security_secid_to_secctx(u32 secid, char **secdata,
1493+
u32 *seclen)
1494+
{
1495+
return -EOPNOTSUPP;
1496+
}
1497+
1498+
static inline int security_lsmprop_to_secctx(struct lsm_prop *prop,
1499+
char **secdata, u32 *seclen)
14921500
{
14931501
return -EOPNOTSUPP;
14941502
}

security/apparmor/include/secid.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ extern int apparmor_display_secid_mode;
2626

2727
struct aa_label *aa_secid_to_label(u32 secid);
2828
int apparmor_secid_to_secctx(u32 secid, char **secdata, u32 *seclen);
29+
int apparmor_lsmprop_to_secctx(struct lsm_prop *prop, char **secdata,
30+
u32 *seclen);
2931
int apparmor_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid);
3032
void apparmor_release_secctx(char *secdata, u32 seclen);
3133

security/apparmor/lsm.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1517,6 +1517,7 @@ static struct security_hook_list apparmor_hooks[] __ro_after_init = {
15171517
#endif
15181518

15191519
LSM_HOOK_INIT(secid_to_secctx, apparmor_secid_to_secctx),
1520+
LSM_HOOK_INIT(lsmprop_to_secctx, apparmor_lsmprop_to_secctx),
15201521
LSM_HOOK_INIT(secctx_to_secid, apparmor_secctx_to_secid),
15211522
LSM_HOOK_INIT(release_secctx, apparmor_release_secctx),
15221523

security/apparmor/secid.c

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,10 @@ struct aa_label *aa_secid_to_label(u32 secid)
6161
return xa_load(&aa_secids, secid);
6262
}
6363

64-
int apparmor_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
64+
static int apparmor_label_to_secctx(struct aa_label *label, char **secdata,
65+
u32 *seclen)
6566
{
6667
/* TODO: cache secctx and ref count so we don't have to recreate */
67-
struct aa_label *label = aa_secid_to_label(secid);
6868
int flags = FLAG_VIEW_SUBNS | FLAG_HIDDEN_UNCONFINED | FLAG_ABS_ROOT;
6969
int len;
7070

@@ -90,6 +90,27 @@ int apparmor_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
9090
return 0;
9191
}
9292

93+
int apparmor_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
94+
{
95+
struct aa_label *label = aa_secid_to_label(secid);
96+
97+
return apparmor_label_to_secctx(label, secdata, seclen);
98+
}
99+
100+
int apparmor_lsmprop_to_secctx(struct lsm_prop *prop, char **secdata,
101+
u32 *seclen)
102+
{
103+
struct aa_label *label;
104+
105+
/* scaffolding */
106+
if (!prop->apparmor.label && prop->scaffold.secid)
107+
label = aa_secid_to_label(prop->scaffold.secid);
108+
else
109+
label = prop->apparmor.label;
110+
111+
return apparmor_label_to_secctx(label, secdata, seclen);
112+
}
113+
93114
int apparmor_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
94115
{
95116
struct aa_label *label;

security/security.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4311,6 +4311,27 @@ int security_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
43114311
}
43124312
EXPORT_SYMBOL(security_secid_to_secctx);
43134313

4314+
/**
4315+
* security_lsmprop_to_secctx() - Convert a lsm_prop to a secctx
4316+
* @prop: lsm specific information
4317+
* @secdata: secctx
4318+
* @seclen: secctx length
4319+
*
4320+
* Convert a @prop entry to security context. If @secdata is NULL the
4321+
* length of the result will be returned in @seclen, but no @secdata
4322+
* will be returned. This does mean that the length could change between
4323+
* calls to check the length and the next call which actually allocates
4324+
* and returns the @secdata.
4325+
*
4326+
* Return: Return 0 on success, error on failure.
4327+
*/
4328+
int security_lsmprop_to_secctx(struct lsm_prop *prop, char **secdata,
4329+
u32 *seclen)
4330+
{
4331+
return call_int_hook(lsmprop_to_secctx, prop, secdata, seclen);
4332+
}
4333+
EXPORT_SYMBOL(security_lsmprop_to_secctx);
4334+
43144335
/**
43154336
* security_secctx_to_secid() - Convert a secctx to a secid
43164337
* @secdata: secctx

security/selinux/hooks.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6601,8 +6601,19 @@ static int selinux_ismaclabel(const char *name)
66016601

66026602
static int selinux_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
66036603
{
6604-
return security_sid_to_context(secid,
6605-
secdata, seclen);
6604+
return security_sid_to_context(secid, secdata, seclen);
6605+
}
6606+
6607+
static int selinux_lsmprop_to_secctx(struct lsm_prop *prop, char **secdata,
6608+
u32 *seclen)
6609+
{
6610+
u32 secid = prop->selinux.secid;
6611+
6612+
/* scaffolding */
6613+
if (!secid)
6614+
secid = prop->scaffold.secid;
6615+
6616+
return selinux_secid_to_secctx(secid, secdata, seclen);
66066617
}
66076618

66086619
static int selinux_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
@@ -7347,6 +7358,7 @@ static struct security_hook_list selinux_hooks[] __ro_after_init = {
73477358
LSM_HOOK_INIT(inode_alloc_security, selinux_inode_alloc_security),
73487359
LSM_HOOK_INIT(sem_alloc_security, selinux_sem_alloc_security),
73497360
LSM_HOOK_INIT(secid_to_secctx, selinux_secid_to_secctx),
7361+
LSM_HOOK_INIT(lsmprop_to_secctx, selinux_lsmprop_to_secctx),
73507362
LSM_HOOK_INIT(inode_getsecctx, selinux_inode_getsecctx),
73517363
LSM_HOOK_INIT(sk_alloc_security, selinux_sk_alloc_security),
73527364
LSM_HOOK_INIT(tun_dev_alloc_security, selinux_tun_dev_alloc_security),

security/selinux/include/audit.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ void selinux_audit_rule_free(void *rule);
4949
* Returns 1 if the context id matches the rule, 0 if it does not, and
5050
* -errno on failure.
5151
*/
52-
int selinux_audit_rule_match(struct lsm_prop *prop, u32 field, u32 op, void *rule);
52+
int selinux_audit_rule_match(struct lsm_prop *prop, u32 field, u32 op,
53+
void *rule);
5354

5455
/**
5556
* selinux_audit_rule_known - check to see if rule contains selinux fields.

security/smack/smack_lsm.c

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4768,7 +4768,7 @@ static int smack_audit_rule_known(struct audit_krule *krule)
47684768
static int smack_audit_rule_match(struct lsm_prop *prop, u32 field, u32 op,
47694769
void *vrule)
47704770
{
4771-
struct smack_known *skp;
4771+
struct smack_known *skp = prop->smack.skp;
47724772
char *rule = vrule;
47734773

47744774
if (unlikely(!rule)) {
@@ -4780,10 +4780,8 @@ static int smack_audit_rule_match(struct lsm_prop *prop, u32 field, u32 op,
47804780
return 0;
47814781

47824782
/* scaffolding */
4783-
if (!prop->smack.skp && prop->scaffold.secid)
4783+
if (!skp && prop->scaffold.secid)
47844784
skp = smack_from_secid(prop->scaffold.secid);
4785-
else
4786-
skp = prop->smack.skp;
47874785

47884786
/*
47894787
* No need to do string comparisons. If a match occurs,
@@ -4814,7 +4812,6 @@ static int smack_ismaclabel(const char *name)
48144812
return (strcmp(name, XATTR_SMACK_SUFFIX) == 0);
48154813
}
48164814

4817-
48184815
/**
48194816
* smack_secid_to_secctx - return the smack label for a secid
48204817
* @secid: incoming integer
@@ -4833,6 +4830,29 @@ static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
48334830
return 0;
48344831
}
48354832

4833+
/**
4834+
* smack_lsmprop_to_secctx - return the smack label
4835+
* @prop: includes incoming Smack data
4836+
* @secdata: destination
4837+
* @seclen: how long it is
4838+
*
4839+
* Exists for audit code.
4840+
*/
4841+
static int smack_lsmprop_to_secctx(struct lsm_prop *prop, char **secdata,
4842+
u32 *seclen)
4843+
{
4844+
struct smack_known *skp = prop->smack.skp;
4845+
4846+
/* scaffolding */
4847+
if (!skp && prop->scaffold.secid)
4848+
skp = smack_from_secid(prop->scaffold.secid);
4849+
4850+
if (secdata)
4851+
*secdata = skp->smk_known;
4852+
*seclen = strlen(skp->smk_known);
4853+
return 0;
4854+
}
4855+
48364856
/**
48374857
* smack_secctx_to_secid - return the secid for a smack label
48384858
* @secdata: smack label
@@ -5192,6 +5212,7 @@ static struct security_hook_list smack_hooks[] __ro_after_init = {
51925212

51935213
LSM_HOOK_INIT(ismaclabel, smack_ismaclabel),
51945214
LSM_HOOK_INIT(secid_to_secctx, smack_secid_to_secctx),
5215+
LSM_HOOK_INIT(lsmprop_to_secctx, smack_lsmprop_to_secctx),
51955216
LSM_HOOK_INIT(secctx_to_secid, smack_secctx_to_secid),
51965217
LSM_HOOK_INIT(inode_notifysecctx, smack_inode_notifysecctx),
51975218
LSM_HOOK_INIT(inode_setsecctx, smack_inode_setsecctx),

0 commit comments

Comments
 (0)