Skip to contentSkip to conversation

selinux prevents msgrcv on restore message queues?

DISCUSSION Nathan Lynch 3 participants 10 messages Mar 2, 2010 – Mar 4, 2010 8 messages not in the archive

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

Hi,

With ckpt-v19-rc3 the test-mq.sh testcase in cr_tests fails when SELinux is enabled on my test system (64-bit powerpc kernel). The testcase sleeps in do_msgrcv after restart and never wakes up. When SELinux is disabled, the messages are received and the testcase passes.

I've established that the messages are being restored during restart -- msgctl(IPC_STAT) shows one message in each queue before calling msgrcv. Adding the IPC_NOWAIT flag to the msgrcv calls gets ENOMSG, however.

I managed to narrow this down to security_msg_queue_msgrcv -> selinux_msg_queue_msgrcv. avc_has_perm(SECCLASS_MSG, MSG__RECEIVE) gets -EACCESS, so I guess something is going awry in selinux restore hooks?

Any ideas?

Thanks,
Nathan

#2 lore raw
In reply to a message not in the archive
Quoted 11 lines · Nathan Lynch

Hi,

With ckpt-v19-rc3 the test-mq.sh testcase in cr_tests fails when SELinux is enabled on my test system (64-bit powerpc kernel). The testcase sleeps in do_msgrcv after restart and never wakes up. When SELinux is disabled, the messages are received and the testcase passes.

I've established that the messages are being restored during restart -- msgctl(IPC_STAT) shows one message in each queue before calling msgrcv. Adding the IPC_NOWAIT flag to the msgrcv calls gets ENOMSG, however.

Quoted 5 lines

I managed to narrow this down to security_msg_queue_msgrcv -> selinux_msg_queue_msgrcv. avc_has_perm(SECCLASS_MSG, MSG__RECEIVE) gets -EACCESS, so I guess something is going awry in selinux restore hooks?

Any ideas?

Hmm, wait. security_msg_msg_alloc() is being called after security_msg_msg_restore. That may not be what is causing your troubles, but it's certainly not right.

-serge

#4 lore raw
In reply to a message not in the archive
Quoted 16 lines · Serge E. Hallyn
Quoted 15 lines · Nathan Lynch

Hi,

With ckpt-v19-rc3 the test-mq.sh testcase in cr_tests fails when SELinux is enabled on my test system (64-bit powerpc kernel). The testcase sleeps in do_msgrcv after restart and never wakes up. When SELinux is disabled, the messages are received and the testcase passes.

I've established that the messages are being restored during restart -- msgctl(IPC_STAT) shows one message in each queue before calling msgrcv. Adding the IPC_NOWAIT flag to the msgrcv calls gets ENOMSG, however.

I managed to narrow this down to security_msg_queue_msgrcv -> selinux_msg_queue_msgrcv. avc_has_perm(SECCLASS_MSG, MSG__RECEIVE) gets -EACCESS, so I guess something is going awry in selinux restore hooks?

Quoted 5 lines
Quoted 1 line

Any ideas?

Hmm, wait. security_msg_msg_alloc() is being called after security_msg_msg_restore. That may not be what is causing your troubles, but it's certainly not right.

Can you try the following patch?

Also, to actually restore the LSM labels you need to add -k to your restart flags, but without the -k you should get a sane default security label.

From 6609b9d71312c6641bdceaaa97bbbf2f809b6ade Mon Sep 17 00:00:00 2001
From: Serge E. Hallyn <serue-r/[email protected]>
Date: Tue, 2 Mar 2010 13:13:36 -0600
Subject: [PATCH 1/1] always security_msg_alloc before security_msg_restore

---
 ipc/checkpoint_msg.c |    7 ++++++-
 1 files changed, 6 insertions(+), 1 deletions(-)
ipc/checkpoint_msg.c +6 -1
index 0155c20..594580f 100644
@@ -231,6 +231,12 @@ static struct msg_msg *restore_msg_contents_one(struct ckpt_ctx *ctx, int *clen)
 	msg->next = NULL;
 	pseg = &msg->next;
 
+	/* set default MAC attributes */
+	ret = security_msg_msg_alloc(msg);
+	if (ret < 0)
+		goto out;
+
+	/* if requested and allowed, reset checkpointed MAC attributes */
 	ret = security_restore_obj(ctx, (void *) msg, CKPT_SECURITY_MSG_MSG,
 				   h->sec_ref);
 	if (ret < 0)
@@ -261,7 +267,6 @@ static struct msg_msg *restore_msg_contents_one(struct ckpt_ctx *ctx, int *clen)
 	msg->m_type = h->m_type;
 	msg->m_ts = h->m_ts;
 	*clen = h->m_ts;
-	ret = security_msg_msg_alloc(msg);
  out:
 	if (ret < 0 && msg) {
 		free_msg(msg);
Signature · 1 line
1.6.0.6
#6 lore raw
In reply to a message not in the archive
Quoted 23 lines · Serge E. Hallyn
Quoted 16 lines · Serge E. Hallyn
Quoted 15 lines · Nathan Lynch

Hi,

With ckpt-v19-rc3 the test-mq.sh testcase in cr_tests fails when SELinux is enabled on my test system (64-bit powerpc kernel). The testcase sleeps in do_msgrcv after restart and never wakes up. When SELinux is disabled, the messages are received and the testcase passes.

I've established that the messages are being restored during restart -- msgctl(IPC_STAT) shows one message in each queue before calling msgrcv. Adding the IPC_NOWAIT flag to the msgrcv calls gets ENOMSG, however.

I managed to narrow this down to security_msg_queue_msgrcv -> selinux_msg_queue_msgrcv. avc_has_perm(SECCLASS_MSG, MSG__RECEIVE) gets -EACCESS, so I guess something is going awry in selinux restore hooks?

Quoted 5 lines
Quoted 1 line

Any ideas?

Hmm, wait. security_msg_msg_alloc() is being called after security_msg_msg_restore. That may not be what is causing your troubles, but it's certainly not right.

Quoted 5 lines

Can you try the following patch?

Also, to actually restore the LSM labels you need to add -k to your restart flags, but without the -k you should get a sane default security label.

Thanks, the ipc/mq tests pass with this patch and restart -k. Without -k the tests still fail in the same manner (msgrcv fails). Is that the behavior you'd expect?

#8 lore raw
In reply to a message not in the archive
Quoted 26 lines · Nathan Lynch
Quoted 25 lines · Serge E. Hallyn
Quoted 16 lines · Serge E. Hallyn
Quoted 15 lines · Nathan Lynch

Hi,

With ckpt-v19-rc3 the test-mq.sh testcase in cr_tests fails when SELinux is enabled on my test system (64-bit powerpc kernel). The testcase sleeps in do_msgrcv after restart and never wakes up. When SELinux is disabled, the messages are received and the testcase passes.

I've established that the messages are being restored during restart -- msgctl(IPC_STAT) shows one message in each queue before calling msgrcv. Adding the IPC_NOWAIT flag to the msgrcv calls gets ENOMSG, however.

I managed to narrow this down to security_msg_queue_msgrcv -> selinux_msg_queue_msgrcv. avc_has_perm(SECCLASS_MSG, MSG__RECEIVE) gets -EACCESS, so I guess something is going awry in selinux restore hooks?

Quoted 5 lines
Quoted 1 line

Any ideas?

Hmm, wait. security_msg_msg_alloc() is being called after security_msg_msg_restore. That may not be what is causing your troubles, but it's certainly not right.

Can you try the following patch?

Quoted 7 lines
Quoted 3 lines

Also, to actually restore the LSM labels you need to add -k to your restart flags, but without the -k you should get a sane default security label.

Thanks, the ipc/mq tests pass with this patch and restart -k. Without -k the tests still fail in the same manner (msgrcv fails). Is that the behavior you'd expect?

Not really - the test runs as unconfined_u right? I'd expect the msg to get created with the same type both when you just run the original program and when you restart it. If you just run the checkpointed program and let it complete, doe sit also get a denial at msgrcv?

-serge

#10 lore raw
In reply to a message not in the archive
Quoted 8 lines · Serge E. Hallyn
Quoted 7 lines · Nathan Lynch
Quoted 5 lines · Serge E. Hallyn

Can you try the following patch?

Also, to actually restore the LSM labels you need to add -k to your restart flags, but without the -k you should get a sane default security label.

Quoted 5 lines
Quoted 3 lines

Thanks, the ipc/mq tests pass with this patch and restart -k. Without -k the tests still fail in the same manner (msgrcv fails). Is that the behavior you'd expect?

Not really - the test runs as unconfined_u right?

I added a ps -Z to test-mq.sh before thawing:

# PATH=/root/cr/user-cr.git:$PATH bash test-mq.sh
Using output dir ./cr_mq_6T8KIG6
XXX Test 1: simple restart with SYSVIPC msq
check-mq: no process killed
../common.sh: line 45: 5173 Killed ( sleep $1; kill -s USR1 $$ )
LABEL PID TTY TIME CMD
unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 4358 pts/1 00:00:00 bash unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 5151 pts/1 00:00:00 bash unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 5172 pts/1 00:00:00 nsexec unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 5175 pts/1 00:00:00 sleep unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 5178 pts/1 00:00:00 check- unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 5183 pts/1 00:00:00 ps PASS

Quoted 5 lines

I'd expect the
msg to get created with the same type both when you just run the original program and when you restart it. If you just run the checkpointed program and let it complete, doe sit also get a denial at msgrcv?

No, the msgrcv calls succeed if I alter the testcase to resume the original process instead of killing and restarting it.

#12 lore raw
In reply to a message not in the archive
Quoted 17 lines · Nathan Lynch
Quoted 8 lines · Serge E. Hallyn
Quoted 7 lines · Nathan Lynch
Quoted 5 lines · Serge E. Hallyn

Can you try the following patch?

Also, to actually restore the LSM labels you need to add -k to your restart flags, but without the -k you should get a sane default security label.

Quoted 5 lines
Quoted 3 lines

Thanks, the ipc/mq tests pass with this patch and restart -k. Without -k the tests still fail in the same manner (msgrcv fails). Is that the behavior you'd expect?

Not really - the test runs as unconfined_u right?

I added a ps -Z to test-mq.sh before thawing:

Quoted 13 lines

# PATH=/root/cr/user-cr.git:$PATH bash test-mq.sh
Using output dir ./cr_mq_6T8KIG6
XXX Test 1: simple restart with SYSVIPC msq
check-mq: no process killed
../common.sh: line 45: 5173 Killed ( sleep $1; kill -s USR1 $$ )
LABEL PID TTY TIME CMD
unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 4358 pts/1 00:00:00 bash unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 5151 pts/1 00:00:00 bash unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 5172 pts/1 00:00:00 nsexec unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 5175 pts/1 00:00:00 sleep unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 5178 pts/1 00:00:00 check- unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 5183 pts/1 00:00:00 ps PASS

Can you try the following patch?

(this is on top of the last one - I'd sent)

The problem is that selinux does not assign a label to a msg_msg until you do msgsnd. So it may be best to special-case the msg_msg object type and always have it restore the msgtype. One reason *NOT* to do that woudl be that the restarter might not have msg_msg:restore permission... But pls let me know if this patch fixes your problem.

thanks,
-serge

From d20ab718b6ebe21a034801c461772e588b92432a Mon Sep 17 00:00:00 2001
From: Serge E. Hallyn <serue-r/[email protected]>
Date: Wed, 3 Mar 2010 11:31:33 -0600
Subject: [PATCH 1/1] always restore msg_msg label

---
 security/security.c |    4 +++-
 1 files changed, 3 insertions(+), 1 deletions(-)
security/security.c +3 -1
index 28db976..2b147cf 100644
@@ -1524,7 +1524,9 @@ int security_restore_obj(struct ckpt_ctx *ctx, void *v, int sectype,
 
 	/* return if caller didn't want to restore checkpointed labels */
 	if (!(ctx->uflags & RESTART_KEEP_LSM))
-		return 0;
+		/* though msg_msg label must always be restored */
+		if (sectype != CKPT_SECURITY_MSG_MSG)
+			return 0;
 
 	l = ckpt_obj_fetch(ctx, secref, CKPT_OBJ_SECURITY);
 	if (IS_ERR(l))
Signature · 1 line
1.6.0.6
#14 lore raw
In reply to a message not in the archive
Quoted 34 lines · Serge E. Hallyn
Quoted 17 lines · Nathan Lynch
Quoted 8 lines · Serge E. Hallyn
Quoted 7 lines · Nathan Lynch
Quoted 5 lines · Serge E. Hallyn

Can you try the following patch?

Also, to actually restore the LSM labels you need to add -k to your restart flags, but without the -k you should get a sane default security label.

Quoted 5 lines
Quoted 3 lines

Thanks, the ipc/mq tests pass with this patch and restart -k. Without -k the tests still fail in the same manner (msgrcv fails). Is that the behavior you'd expect?

Not really - the test runs as unconfined_u right?

I added a ps -Z to test-mq.sh before thawing:

Quoted 13 lines

# PATH=/root/cr/user-cr.git:$PATH bash test-mq.sh
Using output dir ./cr_mq_6T8KIG6
XXX Test 1: simple restart with SYSVIPC msq
check-mq: no process killed
../common.sh: line 45: 5173 Killed ( sleep $1; kill -s USR1 $$ )
LABEL PID TTY TIME CMD
unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 4358 pts/1 00:00:00 bash unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 5151 pts/1 00:00:00 bash unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 5172 pts/1 00:00:00 nsexec unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 5175 pts/1 00:00:00 sleep unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 5178 pts/1 00:00:00 check- unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 5183 pts/1 00:00:00 ps PASS

Can you try the following patch?

Quoted 8 lines

(this is on top of the last one - I'd sent)

The problem is that selinux does not assign a label to a msg_msg until you do msgsnd. So it may be best to special-case the msg_msg object type and always have it restore the msgtype. One reason *NOT* to do that woudl be that the restarter might not have msg_msg:restore permission... But pls let me know if this patch fixes your problem.

Yes, with both patches applied to ckpt-v19-dev (261322990a4ed23c8475c232423845f998dd4f89) the tests pass with and without the -k flag.

#16 lore raw
In reply to a message not in the archive
Quoted 44 lines · Nathan Lynch
Quoted 34 lines · Serge E. Hallyn
Quoted 17 lines · Nathan Lynch
Quoted 8 lines · Serge E. Hallyn
Quoted 7 lines · Nathan Lynch
Quoted 5 lines · Serge E. Hallyn

Can you try the following patch?

Also, to actually restore the LSM labels you need to add -k to your restart flags, but without the -k you should get a sane default security label.

Quoted 5 lines
Quoted 3 lines

Thanks, the ipc/mq tests pass with this patch and restart -k. Without -k the tests still fail in the same manner (msgrcv fails). Is that the behavior you'd expect?

Not really - the test runs as unconfined_u right?

I added a ps -Z to test-mq.sh before thawing:

Quoted 13 lines

# PATH=/root/cr/user-cr.git:$PATH bash test-mq.sh
Using output dir ./cr_mq_6T8KIG6
XXX Test 1: simple restart with SYSVIPC msq
check-mq: no process killed
../common.sh: line 45: 5173 Killed ( sleep $1; kill -s USR1 $$ )
LABEL PID TTY TIME CMD
unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 4358 pts/1 00:00:00 bash unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 5151 pts/1 00:00:00 bash unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 5172 pts/1 00:00:00 nsexec unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 5175 pts/1 00:00:00 sleep unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 5178 pts/1 00:00:00 check- unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 5183 pts/1 00:00:00 ps PASS

Can you try the following patch?

Quoted 8 lines

(this is on top of the last one - I'd sent)

The problem is that selinux does not assign a label to a msg_msg until you do msgsnd. So it may be best to special-case the msg_msg object type and always have it restore the msgtype. One reason *NOT* to do that woudl be that the restarter might not have msg_msg:restore permission... But pls let me know if this patch fixes your problem.

Quoted 3 lines

Yes, with both patches applied to ckpt-v19-dev (261322990a4ed23c8475c232423845f998dd4f89) the tests pass with and without the -k flag.

Cool. The main alternative would be to rip the core of ipc/msg.c:do_msgsnd() out and re-use that in place of the bulk of restore_msg_contents_one().

Since a user can't specify a security context on an msg_msg (but can on msgq) I think the end-result would be fine. It's a lot more work and may cause some other problems, but OTOH we'd be using one code path for msgsnd and its restore which would be a win.

thanks,
-serge

maintainer
#17 lore raw

[
cc:ing SELinux and LSM lists bc this somewhat changes the ipc msg_msg restore behavior - which was previously broken, and now is hopefully merely dubious. The original email in this thread can be seen at
https://lists.linux-foundation.org/pipermail/containers/2010-March/023266.html
]

Quoted 49 lines · Serge E. Hallyn
Quoted 44 lines · Nathan Lynch
Quoted 34 lines · Serge E. Hallyn
Quoted 17 lines · Nathan Lynch
Quoted 8 lines · Serge E. Hallyn
Quoted 7 lines · Nathan Lynch
Quoted 5 lines · Serge E. Hallyn

Can you try the following patch?

Also, to actually restore the LSM labels you need to add -k to your restart flags, but without the -k you should get a sane default security label.

Quoted 5 lines
Quoted 3 lines

Thanks, the ipc/mq tests pass with this patch and restart -k. Without -k the tests still fail in the same manner (msgrcv fails). Is that the behavior you'd expect?

Not really - the test runs as unconfined_u right?

I added a ps -Z to test-mq.sh before thawing:

Quoted 13 lines

# PATH=/root/cr/user-cr.git:$PATH bash test-mq.sh
Using output dir ./cr_mq_6T8KIG6
XXX Test 1: simple restart with SYSVIPC msq
check-mq: no process killed
../common.sh: line 45: 5173 Killed ( sleep $1; kill -s USR1 $$ )
LABEL PID TTY TIME CMD
unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 4358 pts/1 00:00:00 bash unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 5151 pts/1 00:00:00 bash unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 5172 pts/1 00:00:00 nsexec unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 5175 pts/1 00:00:00 sleep unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 5178 pts/1 00:00:00 check- unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 5183 pts/1 00:00:00 ps PASS

Can you try the following patch?

Quoted 8 lines

(this is on top of the last one - I'd sent)

The problem is that selinux does not assign a label to a msg_msg until you do msgsnd. So it may be best to special-case the msg_msg object type and always have it restore the msgtype. One reason *NOT* to do that woudl be that the restarter might not have msg_msg:restore permission... But pls let me know if this patch fixes your problem.

Quoted 3 lines

Yes, with both patches applied to ckpt-v19-dev (261322990a4ed23c8475c232423845f998dd4f89) the tests pass with and without the -k flag.

Quoted 6 lines

Cool. The main alternative would be to rip the core of ipc/msg.c:do_msgsnd() out and re-use that in place of the bulk of restore_msg_contents_one().

Since a user can't specify a security context on an msg_msg (but can on msgq) I think the end-result would be fine. It's a lot more

Except it wouldn't be fine, because SELinux chooses a label for the new msg_msg based on both the task's and the queue's contexts. And we have no idea whether the task's context has changed since the msg was sent.

Now, it seems like letting restart choose the labels may promote bypassing of assured pipelines, but then you can't guarantee original security guarantees anyway unless you have an assured pipeline from checkpoint->restart, whether enforced through SELinux policy or through TPM. And if you have that, then the msg_msg context can't be surruptitiously changed before restart.

So there is no ideal solution for SELinux, but always doing label restore for msg_msg, with the requirement that the restarter be allowed msg_msg:restore permission to the restored msg_msg type, seems the best answer.

thanks,
-serge