lsm: replace context+len with lsm_context

Replace the (secctx,seclen) pointer pair with a single
lsm_context pointer to allow return of the LSM identifier
along with the context and context length. This allows
security_release_secctx() to know how to release the
context. Callers have been modified to use or save the
returned data from the new structure.

security_secid_to_secctx() and security_lsmproc_to_secctx()
will now return the length value on success instead of 0.

Cc: netdev@vger.kernel.org
Cc: audit@vger.kernel.org
Cc: netfilter-devel@vger.kernel.org
Cc: Todd Kjos <tkjos@google.com>
Signed-off-by: Casey Schaufler <casey@schaufler-ca.com>
[PM: subject tweak, kdoc fix, signedness fix from Dan Carpenter]
Signed-off-by: Paul Moore <paul@paul-moore.com>
This commit is contained in:
Casey Schaufler 2024-10-23 14:21:55 -07:00 committed by Paul Moore
parent 6fba89813c
commit 2d470c7781
17 changed files with 121 additions and 125 deletions

View File

@ -3296,9 +3296,8 @@ static void binder_transaction(struct binder_proc *proc,
size_t added_size; size_t added_size;
security_cred_getsecid(proc->cred, &secid); security_cred_getsecid(proc->cred, &secid);
ret = security_secid_to_secctx(secid, &lsmctx.context, ret = security_secid_to_secctx(secid, &lsmctx);
&lsmctx.len); if (ret < 0) {
if (ret) {
binder_txn_error("%d:%d failed to get security context\n", binder_txn_error("%d:%d failed to get security context\n",
thread->pid, proc->pid); thread->pid, proc->pid);
return_error = BR_FAILED_REPLY; return_error = BR_FAILED_REPLY;

View File

@ -295,10 +295,9 @@ LSM_HOOK(int, -EINVAL, getprocattr, struct task_struct *p, const char *name,
char **value) char **value)
LSM_HOOK(int, -EINVAL, setprocattr, const char *name, void *value, size_t size) LSM_HOOK(int, -EINVAL, setprocattr, const char *name, void *value, size_t size)
LSM_HOOK(int, 0, ismaclabel, const char *name) LSM_HOOK(int, 0, ismaclabel, const char *name)
LSM_HOOK(int, -EOPNOTSUPP, secid_to_secctx, u32 secid, char **secdata, LSM_HOOK(int, -EOPNOTSUPP, secid_to_secctx, u32 secid, struct lsm_context *cp)
u32 *seclen)
LSM_HOOK(int, -EOPNOTSUPP, lsmprop_to_secctx, struct lsm_prop *prop, LSM_HOOK(int, -EOPNOTSUPP, lsmprop_to_secctx, struct lsm_prop *prop,
char **secdata, u32 *seclen) struct lsm_context *cp)
LSM_HOOK(int, 0, secctx_to_secid, const char *secdata, u32 seclen, u32 *secid) LSM_HOOK(int, 0, secctx_to_secid, const char *secdata, u32 seclen, u32 *secid)
LSM_HOOK(void, LSM_RET_VOID, release_secctx, struct lsm_context *cp) LSM_HOOK(void, LSM_RET_VOID, release_secctx, struct lsm_context *cp)
LSM_HOOK(void, LSM_RET_VOID, inode_invalidate_secctx, struct inode *inode) LSM_HOOK(void, LSM_RET_VOID, inode_invalidate_secctx, struct inode *inode)

View File

@ -584,8 +584,8 @@ int security_getprocattr(struct task_struct *p, int lsmid, const char *name,
int security_setprocattr(int lsmid, const char *name, void *value, size_t size); int security_setprocattr(int lsmid, const char *name, void *value, size_t size);
int security_netlink_send(struct sock *sk, struct sk_buff *skb); int security_netlink_send(struct sock *sk, struct sk_buff *skb);
int security_ismaclabel(const char *name); int security_ismaclabel(const char *name);
int security_secid_to_secctx(u32 secid, char **secdata, u32 *seclen); int security_secid_to_secctx(u32 secid, struct lsm_context *cp);
int security_lsmprop_to_secctx(struct lsm_prop *prop, char **secdata, u32 *seclen); int security_lsmprop_to_secctx(struct lsm_prop *prop, struct lsm_context *cp);
int security_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid); int security_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid);
void security_release_secctx(struct lsm_context *cp); void security_release_secctx(struct lsm_context *cp);
void security_inode_invalidate_secctx(struct inode *inode); void security_inode_invalidate_secctx(struct inode *inode);
@ -1557,14 +1557,13 @@ static inline int security_ismaclabel(const char *name)
return 0; return 0;
} }
static inline int security_secid_to_secctx(u32 secid, char **secdata, static inline int security_secid_to_secctx(u32 secid, struct lsm_context *cp)
u32 *seclen)
{ {
return -EOPNOTSUPP; return -EOPNOTSUPP;
} }
static inline int security_lsmprop_to_secctx(struct lsm_prop *prop, static inline int security_lsmprop_to_secctx(struct lsm_prop *prop,
char **secdata, u32 *seclen) struct lsm_context *cp)
{ {
return -EOPNOTSUPP; return -EOPNOTSUPP;
} }

View File

@ -109,10 +109,9 @@ static inline void scm_passec(struct socket *sock, struct msghdr *msg, struct sc
int err; int err;
if (test_bit(SOCK_PASSSEC, &sock->flags)) { if (test_bit(SOCK_PASSSEC, &sock->flags)) {
err = security_secid_to_secctx(scm->secid, &ctx.context, err = security_secid_to_secctx(scm->secid, &ctx);
&ctx.len);
if (!err) { if (err >= 0) {
put_cmsg(msg, SOL_SOCKET, SCM_SECURITY, ctx.len, put_cmsg(msg, SOL_SOCKET, SCM_SECURITY, ctx.len,
ctx.context); ctx.context);
security_release_secctx(&ctx); security_release_secctx(&ctx);

View File

@ -1473,9 +1473,8 @@ static int audit_receive_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
case AUDIT_SIGNAL_INFO: case AUDIT_SIGNAL_INFO:
if (lsmprop_is_set(&audit_sig_lsm)) { if (lsmprop_is_set(&audit_sig_lsm)) {
err = security_lsmprop_to_secctx(&audit_sig_lsm, err = security_lsmprop_to_secctx(&audit_sig_lsm,
&lsmctx.context, &lsmctx);
&lsmctx.len); if (err < 0)
if (err)
return err; return err;
} }
sig_data = kmalloc(struct_size(sig_data, ctx, lsmctx.len), sig_data = kmalloc(struct_size(sig_data, ctx, lsmctx.len),
@ -2188,8 +2187,8 @@ int audit_log_task_context(struct audit_buffer *ab)
if (!lsmprop_is_set(&prop)) if (!lsmprop_is_set(&prop))
return 0; return 0;
error = security_lsmprop_to_secctx(&prop, &ctx.context, &ctx.len); error = security_lsmprop_to_secctx(&prop, &ctx);
if (error) { if (error < 0) {
if (error != -EINVAL) if (error != -EINVAL)
goto error_path; goto error_path;
return 0; return 0;

View File

@ -1109,7 +1109,7 @@ static int audit_log_pid_context(struct audit_context *context, pid_t pid,
from_kuid(&init_user_ns, auid), from_kuid(&init_user_ns, auid),
from_kuid(&init_user_ns, uid), sessionid); from_kuid(&init_user_ns, uid), sessionid);
if (lsmprop_is_set(prop)) { if (lsmprop_is_set(prop)) {
if (security_lsmprop_to_secctx(prop, &ctx.context, &ctx.len)) { if (security_lsmprop_to_secctx(prop, &ctx) < 0) {
audit_log_format(ab, " obj=(none)"); audit_log_format(ab, " obj=(none)");
rc = 1; rc = 1;
} else { } else {
@ -1370,7 +1370,6 @@ static void audit_log_time(struct audit_context *context, struct audit_buffer **
static void show_special(struct audit_context *context, int *call_panic) static void show_special(struct audit_context *context, int *call_panic)
{ {
struct lsm_context lsmcxt;
struct audit_buffer *ab; struct audit_buffer *ab;
int i; int i;
@ -1393,16 +1392,14 @@ static void show_special(struct audit_context *context, int *call_panic)
from_kgid(&init_user_ns, context->ipc.gid), from_kgid(&init_user_ns, context->ipc.gid),
context->ipc.mode); context->ipc.mode);
if (lsmprop_is_set(&context->ipc.oprop)) { if (lsmprop_is_set(&context->ipc.oprop)) {
char *ctx = NULL; struct lsm_context lsmctx;
u32 len;
if (security_lsmprop_to_secctx(&context->ipc.oprop, if (security_lsmprop_to_secctx(&context->ipc.oprop,
&ctx, &len)) { &lsmctx) < 0) {
*call_panic = 1; *call_panic = 1;
} else { } else {
audit_log_format(ab, " obj=%s", ctx); audit_log_format(ab, " obj=%s", lsmctx.context);
lsmcontext_init(&lsmcxt, ctx, len, 0); security_release_secctx(&lsmctx);
security_release_secctx(&lsmcxt);
} }
} }
if (context->ipc.has_perm) { if (context->ipc.has_perm) {
@ -1563,8 +1560,7 @@ static void audit_log_name(struct audit_context *context, struct audit_names *n,
if (lsmprop_is_set(&n->oprop)) { if (lsmprop_is_set(&n->oprop)) {
struct lsm_context ctx; struct lsm_context ctx;
if (security_lsmprop_to_secctx(&n->oprop, &ctx.context, if (security_lsmprop_to_secctx(&n->oprop, &ctx) < 0) {
&ctx.len)) {
if (call_panic) if (call_panic)
*call_panic = 2; *call_panic = 2;
} else { } else {

View File

@ -136,8 +136,8 @@ static void ip_cmsg_recv_security(struct msghdr *msg, struct sk_buff *skb)
if (err) if (err)
return; return;
err = security_secid_to_secctx(secid, &ctx.context, &ctx.len); err = security_secid_to_secctx(secid, &ctx);
if (err) if (err < 0)
return; return;
put_cmsg(msg, SOL_IP, SCM_SECURITY, ctx.len, ctx.context); put_cmsg(msg, SOL_IP, SCM_SECURITY, ctx.len, ctx.context);

View File

@ -360,8 +360,8 @@ static int ctnetlink_dump_secctx(struct sk_buff *skb, const struct nf_conn *ct)
struct lsm_context ctx; struct lsm_context ctx;
int ret; int ret;
ret = security_secid_to_secctx(ct->secmark, &ctx.context, &ctx.len); ret = security_secid_to_secctx(ct->secmark, &ctx);
if (ret) if (ret < 0)
return 0; return 0;
ret = -1; ret = -1;
@ -663,14 +663,14 @@ static inline size_t ctnetlink_acct_size(const struct nf_conn *ct)
static inline int ctnetlink_secctx_size(const struct nf_conn *ct) static inline int ctnetlink_secctx_size(const struct nf_conn *ct)
{ {
#ifdef CONFIG_NF_CONNTRACK_SECMARK #ifdef CONFIG_NF_CONNTRACK_SECMARK
int len, ret; int ret;
ret = security_secid_to_secctx(ct->secmark, NULL, &len); ret = security_secid_to_secctx(ct->secmark, NULL);
if (ret) if (ret < 0)
return 0; return 0;
return nla_total_size(0) /* CTA_SECCTX */ return nla_total_size(0) /* CTA_SECCTX */
+ nla_total_size(sizeof(char) * len); /* CTA_SECCTX_NAME */ + nla_total_size(sizeof(char) * ret); /* CTA_SECCTX_NAME */
#else #else
return 0; return 0;
#endif #endif

View File

@ -175,8 +175,8 @@ static void ct_show_secctx(struct seq_file *s, const struct nf_conn *ct)
struct lsm_context ctx; struct lsm_context ctx;
int ret; int ret;
ret = security_secid_to_secctx(ct->secmark, &ctx.context, &ctx.len); ret = security_secid_to_secctx(ct->secmark, &ctx);
if (ret) if (ret < 0)
return; return;
seq_printf(s, "secctx=%s ", ctx.context); seq_printf(s, "secctx=%s ", ctx.context);

View File

@ -470,18 +470,18 @@ static int nfqnl_put_sk_classid(struct sk_buff *skb, struct sock *sk)
return 0; return 0;
} }
static u32 nfqnl_get_sk_secctx(struct sk_buff *skb, char **secdata) static u32 nfqnl_get_sk_secctx(struct sk_buff *skb, struct lsm_context *ctx)
{ {
u32 seclen = 0; u32 seclen = 0;
#if IS_ENABLED(CONFIG_NETWORK_SECMARK) #if IS_ENABLED(CONFIG_NETWORK_SECMARK)
if (!skb || !sk_fullsock(skb->sk)) if (!skb || !sk_fullsock(skb->sk))
return 0; return 0;
read_lock_bh(&skb->sk->sk_callback_lock); read_lock_bh(&skb->sk->sk_callback_lock);
if (skb->secmark) if (skb->secmark)
security_secid_to_secctx(skb->secmark, secdata, &seclen); seclen = security_secid_to_secctx(skb->secmark, ctx);
read_unlock_bh(&skb->sk->sk_callback_lock); read_unlock_bh(&skb->sk->sk_callback_lock);
#endif #endif
return seclen; return seclen;
@ -567,8 +567,7 @@ nfqnl_build_packet_message(struct net *net, struct nfqnl_instance *queue,
enum ip_conntrack_info ctinfo = 0; enum ip_conntrack_info ctinfo = 0;
const struct nfnl_ct_hook *nfnl_ct; const struct nfnl_ct_hook *nfnl_ct;
bool csum_verify; bool csum_verify;
struct lsm_context scaff; /* scaffolding */ struct lsm_context ctx;
char *secdata = NULL;
u32 seclen = 0; u32 seclen = 0;
ktime_t tstamp; ktime_t tstamp;
@ -643,8 +642,8 @@ nfqnl_build_packet_message(struct net *net, struct nfqnl_instance *queue,
} }
if ((queue->flags & NFQA_CFG_F_SECCTX) && entskb->sk) { if ((queue->flags & NFQA_CFG_F_SECCTX) && entskb->sk) {
seclen = nfqnl_get_sk_secctx(entskb, &secdata); seclen = nfqnl_get_sk_secctx(entskb, &ctx);
if (seclen) if (seclen >= 0)
size += nla_total_size(seclen); size += nla_total_size(seclen);
} }
@ -783,7 +782,7 @@ nfqnl_build_packet_message(struct net *net, struct nfqnl_instance *queue,
if (nfqnl_put_sk_classid(skb, entskb->sk) < 0) if (nfqnl_put_sk_classid(skb, entskb->sk) < 0)
goto nla_put_failure; goto nla_put_failure;
if (seclen && nla_put(skb, NFQA_SECCTX, seclen, secdata)) if (seclen && nla_put(skb, NFQA_SECCTX, ctx.len, ctx.context))
goto nla_put_failure; goto nla_put_failure;
if (ct && nfnl_ct->build(skb, ct, ctinfo, NFQA_CT, NFQA_CT_INFO) < 0) if (ct && nfnl_ct->build(skb, ct, ctinfo, NFQA_CT, NFQA_CT_INFO) < 0)
@ -811,10 +810,8 @@ nfqnl_build_packet_message(struct net *net, struct nfqnl_instance *queue,
} }
nlh->nlmsg_len = skb->len; nlh->nlmsg_len = skb->len;
if (seclen) { if (seclen >= 0)
lsmcontext_init(&scaff, secdata, seclen, 0); security_release_secctx(&ctx);
security_release_secctx(&scaff);
}
return skb; return skb;
nla_put_failure: nla_put_failure:
@ -822,10 +819,8 @@ nfqnl_build_packet_message(struct net *net, struct nfqnl_instance *queue,
kfree_skb(skb); kfree_skb(skb);
net_err_ratelimited("nf_queue: error creating packet message\n"); net_err_ratelimited("nf_queue: error creating packet message\n");
nlmsg_failure: nlmsg_failure:
if (seclen) { if (seclen >= 0)
lsmcontext_init(&scaff, secdata, seclen, 0); security_release_secctx(&ctx);
security_release_secctx(&scaff);
}
return NULL; return NULL;
} }

View File

@ -437,9 +437,7 @@ int netlbl_unlhsh_add(struct net *net,
unlhsh_add_return: unlhsh_add_return:
rcu_read_unlock(); rcu_read_unlock();
if (audit_buf != NULL) { if (audit_buf != NULL) {
if (security_secid_to_secctx(secid, if (security_secid_to_secctx(secid, &ctx) == 0) {
&ctx.context,
&ctx.len) == 0) {
audit_log_format(audit_buf, " sec_obj=%s", ctx.context); audit_log_format(audit_buf, " sec_obj=%s", ctx.context);
security_release_secctx(&ctx); security_release_secctx(&ctx);
} }
@ -492,8 +490,7 @@ static int netlbl_unlhsh_remove_addr4(struct net *net,
addr->s_addr, mask->s_addr); addr->s_addr, mask->s_addr);
dev_put(dev); dev_put(dev);
if (entry != NULL && if (entry != NULL &&
security_secid_to_secctx(entry->secid, security_secid_to_secctx(entry->secid, &ctx) == 0) {
&ctx.context, &ctx.len) == 0) {
audit_log_format(audit_buf, " sec_obj=%s", ctx.context); audit_log_format(audit_buf, " sec_obj=%s", ctx.context);
security_release_secctx(&ctx); security_release_secctx(&ctx);
} }
@ -551,8 +548,7 @@ static int netlbl_unlhsh_remove_addr6(struct net *net,
addr, mask); addr, mask);
dev_put(dev); dev_put(dev);
if (entry != NULL && if (entry != NULL &&
security_secid_to_secctx(entry->secid, security_secid_to_secctx(entry->secid, &ctx) == 0) {
&ctx.context, &ctx.len) == 0) {
audit_log_format(audit_buf, " sec_obj=%s", ctx.context); audit_log_format(audit_buf, " sec_obj=%s", ctx.context);
security_release_secctx(&ctx); security_release_secctx(&ctx);
} }
@ -1123,8 +1119,8 @@ static int netlbl_unlabel_staticlist_gen(u32 cmd,
secid = addr6->secid; secid = addr6->secid;
} }
ret_val = security_secid_to_secctx(secid, &ctx.context, &ctx.len); ret_val = security_secid_to_secctx(secid, &ctx);
if (ret_val != 0) if (ret_val < 0)
goto list_cb_failure; goto list_cb_failure;
ret_val = nla_put(cb_arg->skb, ret_val = nla_put(cb_arg->skb,
NLBL_UNLABEL_A_SECCTX, NLBL_UNLABEL_A_SECCTX,

View File

@ -98,8 +98,7 @@ struct audit_buffer *netlbl_audit_start_common(int type,
audit_info->sessionid); audit_info->sessionid);
if (lsmprop_is_set(&audit_info->prop) && if (lsmprop_is_set(&audit_info->prop) &&
security_lsmprop_to_secctx(&audit_info->prop, &ctx.context, security_lsmprop_to_secctx(&audit_info->prop, &ctx) > 0) {
&ctx.len) == 0) {
audit_log_format(audit_buf, " subj=%s", ctx.context); audit_log_format(audit_buf, " subj=%s", ctx.context);
security_release_secctx(&ctx); security_release_secctx(&ctx);
} }

View File

@ -25,9 +25,8 @@ struct aa_label;
extern int apparmor_display_secid_mode; extern int apparmor_display_secid_mode;
struct aa_label *aa_secid_to_label(u32 secid); struct aa_label *aa_secid_to_label(u32 secid);
int apparmor_secid_to_secctx(u32 secid, char **secdata, u32 *seclen); int apparmor_secid_to_secctx(u32 secid, struct lsm_context *cp);
int apparmor_lsmprop_to_secctx(struct lsm_prop *prop, char **secdata, int apparmor_lsmprop_to_secctx(struct lsm_prop *prop, struct lsm_context *cp);
u32 *seclen);
int apparmor_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid); int apparmor_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid);
void apparmor_release_secctx(struct lsm_context *cp); void apparmor_release_secctx(struct lsm_context *cp);

View File

@ -47,23 +47,21 @@ struct aa_label *aa_secid_to_label(u32 secid)
return xa_load(&aa_secids, secid); return xa_load(&aa_secids, secid);
} }
static int apparmor_label_to_secctx(struct aa_label *label, char **secdata, static int apparmor_label_to_secctx(struct aa_label *label,
u32 *seclen) struct lsm_context *cp)
{ {
/* TODO: cache secctx and ref count so we don't have to recreate */ /* TODO: cache secctx and ref count so we don't have to recreate */
int flags = FLAG_VIEW_SUBNS | FLAG_HIDDEN_UNCONFINED | FLAG_ABS_ROOT; int flags = FLAG_VIEW_SUBNS | FLAG_HIDDEN_UNCONFINED | FLAG_ABS_ROOT;
int len; int len;
AA_BUG(!seclen);
if (!label) if (!label)
return -EINVAL; return -EINVAL;
if (apparmor_display_secid_mode) if (apparmor_display_secid_mode)
flags |= FLAG_SHOW_MODE; flags |= FLAG_SHOW_MODE;
if (secdata) if (cp)
len = aa_label_asxprint(secdata, root_ns, label, len = aa_label_asxprint(&cp->context, root_ns, label,
flags, GFP_ATOMIC); flags, GFP_ATOMIC);
else else
len = aa_label_snxprint(NULL, 0, root_ns, label, flags); len = aa_label_snxprint(NULL, 0, root_ns, label, flags);
@ -71,26 +69,28 @@ static int apparmor_label_to_secctx(struct aa_label *label, char **secdata,
if (len < 0) if (len < 0)
return -ENOMEM; return -ENOMEM;
*seclen = len; if (cp) {
cp->len = len;
cp->id = LSM_ID_APPARMOR;
}
return 0; return len;
} }
int apparmor_secid_to_secctx(u32 secid, char **secdata, u32 *seclen) int apparmor_secid_to_secctx(u32 secid, struct lsm_context *cp)
{ {
struct aa_label *label = aa_secid_to_label(secid); struct aa_label *label = aa_secid_to_label(secid);
return apparmor_label_to_secctx(label, secdata, seclen); return apparmor_label_to_secctx(label, cp);
} }
int apparmor_lsmprop_to_secctx(struct lsm_prop *prop, char **secdata, int apparmor_lsmprop_to_secctx(struct lsm_prop *prop, struct lsm_context *cp)
u32 *seclen)
{ {
struct aa_label *label; struct aa_label *label;
label = prop->apparmor.label; label = prop->apparmor.label;
return apparmor_label_to_secctx(label, secdata, seclen); return apparmor_label_to_secctx(label, cp);
} }
int apparmor_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid) int apparmor_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)

View File

@ -4304,40 +4304,36 @@ EXPORT_SYMBOL(security_ismaclabel);
/** /**
* security_secid_to_secctx() - Convert a secid to a secctx * security_secid_to_secctx() - Convert a secid to a secctx
* @secid: secid * @secid: secid
* @secdata: secctx * @cp: the LSM context
* @seclen: secctx length
* *
* Convert secid to security context. If @secdata is NULL the length of the * Convert secid to security context. If @cp is NULL the length of the
* result will be returned in @seclen, but no @secdata will be returned. This * result will be returned, but no data will be returned. This
* does mean that the length could change between calls to check the length and * does mean that the length could change between calls to check the length and
* the next call which actually allocates and returns the @secdata. * the next call which actually allocates and returns the data.
* *
* Return: Return 0 on success, error on failure. * Return: Return length of data on success, error on failure.
*/ */
int security_secid_to_secctx(u32 secid, char **secdata, u32 *seclen) int security_secid_to_secctx(u32 secid, struct lsm_context *cp)
{ {
return call_int_hook(secid_to_secctx, secid, secdata, seclen); return call_int_hook(secid_to_secctx, secid, cp);
} }
EXPORT_SYMBOL(security_secid_to_secctx); EXPORT_SYMBOL(security_secid_to_secctx);
/** /**
* security_lsmprop_to_secctx() - Convert a lsm_prop to a secctx * security_lsmprop_to_secctx() - Convert a lsm_prop to a secctx
* @prop: lsm specific information * @prop: lsm specific information
* @secdata: secctx * @cp: the LSM context
* @seclen: secctx length
* *
* Convert a @prop entry to security context. If @secdata is NULL the * Convert a @prop entry to security context. If @cp is NULL the
* length of the result will be returned in @seclen, but no @secdata * length of the result will be returned. This does mean that the
* will be returned. This does mean that the length could change between * length could change between calls to check the length and the
* calls to check the length and the next call which actually allocates * next call which actually allocates and returns the @cp.
* and returns the @secdata.
* *
* Return: Return 0 on success, error on failure. * Return: Return length of data on success, error on failure.
*/ */
int security_lsmprop_to_secctx(struct lsm_prop *prop, char **secdata, int security_lsmprop_to_secctx(struct lsm_prop *prop, struct lsm_context *cp)
u32 *seclen)
{ {
return call_int_hook(lsmprop_to_secctx, prop, secdata, seclen); return call_int_hook(lsmprop_to_secctx, prop, cp);
} }
EXPORT_SYMBOL(security_lsmprop_to_secctx); EXPORT_SYMBOL(security_lsmprop_to_secctx);

View File

@ -6640,15 +6640,28 @@ static int selinux_ismaclabel(const char *name)
return (strcmp(name, XATTR_SELINUX_SUFFIX) == 0); return (strcmp(name, XATTR_SELINUX_SUFFIX) == 0);
} }
static int selinux_secid_to_secctx(u32 secid, char **secdata, u32 *seclen) static int selinux_secid_to_secctx(u32 secid, struct lsm_context *cp)
{ {
return security_sid_to_context(secid, secdata, seclen); u32 seclen;
int ret;
if (cp) {
cp->id = LSM_ID_SELINUX;
ret = security_sid_to_context(secid, &cp->context, &cp->len);
if (ret < 0)
return ret;
return cp->len;
}
ret = security_sid_to_context(secid, NULL, &seclen);
if (ret < 0)
return ret;
return seclen;
} }
static int selinux_lsmprop_to_secctx(struct lsm_prop *prop, char **secdata, static int selinux_lsmprop_to_secctx(struct lsm_prop *prop,
u32 *seclen) struct lsm_context *cp)
{ {
return selinux_secid_to_secctx(prop->selinux.secid, secdata, seclen); return selinux_secid_to_secctx(prop->selinux.secid, cp);
} }
static int selinux_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid) static int selinux_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)

View File

@ -4817,41 +4817,48 @@ static int smack_ismaclabel(const char *name)
return (strcmp(name, XATTR_SMACK_SUFFIX) == 0); return (strcmp(name, XATTR_SMACK_SUFFIX) == 0);
} }
/**
* smack_to_secctx - fill a lsm_context
* @skp: Smack label
* @cp: destination
*
* Fill the passed @cp and return the length of the string
*/
static int smack_to_secctx(struct smack_known *skp, struct lsm_context *cp)
{
int len = strlen(skp->smk_known);
if (cp) {
cp->context = skp->smk_known;
cp->len = len;
cp->id = LSM_ID_SMACK;
}
return len;
}
/** /**
* smack_secid_to_secctx - return the smack label for a secid * smack_secid_to_secctx - return the smack label for a secid
* @secid: incoming integer * @secid: incoming integer
* @secdata: destination * @cp: destination
* @seclen: how long it is
* *
* Exists for networking code. * Exists for networking code.
*/ */
static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen) static int smack_secid_to_secctx(u32 secid, struct lsm_context *cp)
{ {
struct smack_known *skp = smack_from_secid(secid); return smack_to_secctx(smack_from_secid(secid), cp);
if (secdata)
*secdata = skp->smk_known;
*seclen = strlen(skp->smk_known);
return 0;
} }
/** /**
* smack_lsmprop_to_secctx - return the smack label * smack_lsmprop_to_secctx - return the smack label
* @prop: includes incoming Smack data * @prop: includes incoming Smack data
* @secdata: destination * @cp: destination
* @seclen: how long it is
* *
* Exists for audit code. * Exists for audit code.
*/ */
static int smack_lsmprop_to_secctx(struct lsm_prop *prop, char **secdata, static int smack_lsmprop_to_secctx(struct lsm_prop *prop,
u32 *seclen) struct lsm_context *cp)
{ {
struct smack_known *skp = prop->smack.skp; return smack_to_secctx(prop->smack.skp, cp);
if (secdata)
*secdata = skp->smk_known;
*seclen = strlen(skp->smk_known);
return 0;
} }
/** /**