Skip to content

Commit e4e8536

Browse files
pcmooredavem330
authored andcommitted
selinux: fix the labeled xfrm/IPsec reference count handling
The SELinux labeled IPsec code was improperly handling its reference counting, dropping a reference on a delete operation instead of on a free/release operation. Reported-by: Ondrej Moris <omoris@redhat.com> Signed-off-by: Paul Moore <pmoore@redhat.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent e4c1721 commit e4e8536

File tree

1 file changed

+14
-20
lines changed

1 file changed

+14
-20
lines changed

security/selinux/xfrm.c

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,7 @@ int selinux_xfrm_policy_clone(struct xfrm_sec_ctx *old_ctx,
316316

317317
memcpy(new_ctx, old_ctx, sizeof(*new_ctx));
318318
memcpy(new_ctx->ctx_str, old_ctx->ctx_str, new_ctx->ctx_len);
319+
atomic_inc(&selinux_xfrm_refcount);
319320
*new_ctxp = new_ctx;
320321
}
321322
return 0;
@@ -326,6 +327,7 @@ int selinux_xfrm_policy_clone(struct xfrm_sec_ctx *old_ctx,
326327
*/
327328
void selinux_xfrm_policy_free(struct xfrm_sec_ctx *ctx)
328329
{
330+
atomic_dec(&selinux_xfrm_refcount);
329331
kfree(ctx);
330332
}
331333

@@ -335,17 +337,13 @@ void selinux_xfrm_policy_free(struct xfrm_sec_ctx *ctx)
335337
int selinux_xfrm_policy_delete(struct xfrm_sec_ctx *ctx)
336338
{
337339
const struct task_security_struct *tsec = current_security();
338-
int rc = 0;
339340

340-
if (ctx) {
341-
rc = avc_has_perm(tsec->sid, ctx->ctx_sid,
342-
SECCLASS_ASSOCIATION,
343-
ASSOCIATION__SETCONTEXT, NULL);
344-
if (rc == 0)
345-
atomic_dec(&selinux_xfrm_refcount);
346-
}
341+
if (!ctx)
342+
return 0;
347343

348-
return rc;
344+
return avc_has_perm(tsec->sid, ctx->ctx_sid,
345+
SECCLASS_ASSOCIATION, ASSOCIATION__SETCONTEXT,
346+
NULL);
349347
}
350348

351349
/*
@@ -370,8 +368,8 @@ int selinux_xfrm_state_alloc(struct xfrm_state *x, struct xfrm_user_sec_ctx *uct
370368
*/
371369
void selinux_xfrm_state_free(struct xfrm_state *x)
372370
{
373-
struct xfrm_sec_ctx *ctx = x->security;
374-
kfree(ctx);
371+
atomic_dec(&selinux_xfrm_refcount);
372+
kfree(x->security);
375373
}
376374

377375
/*
@@ -381,17 +379,13 @@ int selinux_xfrm_state_delete(struct xfrm_state *x)
381379
{
382380
const struct task_security_struct *tsec = current_security();
383381
struct xfrm_sec_ctx *ctx = x->security;
384-
int rc = 0;
385382

386-
if (ctx) {
387-
rc = avc_has_perm(tsec->sid, ctx->ctx_sid,
388-
SECCLASS_ASSOCIATION,
389-
ASSOCIATION__SETCONTEXT, NULL);
390-
if (rc == 0)
391-
atomic_dec(&selinux_xfrm_refcount);
392-
}
383+
if (!ctx)
384+
return 0;
393385

394-
return rc;
386+
return avc_has_perm(tsec->sid, ctx->ctx_sid,
387+
SECCLASS_ASSOCIATION, ASSOCIATION__SETCONTEXT,
388+
NULL);
395389
}
396390

397391
/*

0 commit comments

Comments
 (0)