selinux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] libselpol: Sort portcon rules consistently
@ 2020-05-28 18:40 James Carter
  2020-05-29 14:57 ` Stephen Smalley
  0 siblings, 1 reply; 4+ messages in thread
From: James Carter @ 2020-05-28 18:40 UTC (permalink / raw)
  To: selinux; +Cc: James Carter, Stephen Smalley

The comparison function, portcon_data_cmp(), only made use of the
protocol to put tcp before udp, dccp, and sctp. Rules that have
the same port range, but with different protocols would be considered
equal unless one of the protocols was tcp. When generating a CIL or
conf source policy from a binary or using the "-S" option in
checkpolicy the non-tcp portcon rules with the same port range would
not be consistently sorted.

Changed portcon_data_cmp() to sort portcon rules like the CIL function
cil_post_portcon_compare().

Reported-by: Stephen Smalley <stephen.smalley.work@gmail.com>
Signed-off-by: James Carter <jwcart2@gmail.com>
---
 libsepol/src/kernel_to_common.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)

diff --git a/libsepol/src/kernel_to_common.c b/libsepol/src/kernel_to_common.c
index 7b53c92f..a7453d3c 100644
--- a/libsepol/src/kernel_to_common.c
+++ b/libsepol/src/kernel_to_common.c
@@ -470,11 +470,9 @@ static int portcon_data_cmp(const void *a, const void *b)
 	rc = compare_ranges((*aa)->u.port.low_port, (*aa)->u.port.high_port,
 			    (*bb)->u.port.low_port, (*bb)->u.port.high_port);
 	if (rc == 0) {
-		if ((*aa)->u.port.protocol == (*bb)->u.port.protocol) {
-			rc = 0;
-		} else if ((*aa)->u.port.protocol == IPPROTO_TCP) {
+		if ((*aa)->u.port.protocol < (*bb)->u.port.protocol) {
 			rc = -1;
-		} else {
+		} else if ((*aa)->u.port.protocol > (*bb)->u.port.protocol) {
 			rc = 1;
 		}
 	}
-- 
2.25.4


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] libselpol: Sort portcon rules consistently
  2020-05-28 18:40 [PATCH] libselpol: Sort portcon rules consistently James Carter
@ 2020-05-29 14:57 ` Stephen Smalley
  2020-05-29 15:34   ` James Carter
  0 siblings, 1 reply; 4+ messages in thread
From: Stephen Smalley @ 2020-05-29 14:57 UTC (permalink / raw)
  To: James Carter; +Cc: SElinux list

On Thu, May 28, 2020 at 2:41 PM James Carter <jwcart2@gmail.com> wrote:
>
> The comparison function, portcon_data_cmp(), only made use of the
> protocol to put tcp before udp, dccp, and sctp. Rules that have
> the same port range, but with different protocols would be considered
> equal unless one of the protocols was tcp. When generating a CIL or
> conf source policy from a binary or using the "-S" option in
> checkpolicy the non-tcp portcon rules with the same port range would
> not be consistently sorted.
>
> Changed portcon_data_cmp() to sort portcon rules like the CIL function
> cil_post_portcon_compare().
>
> Reported-by: Stephen Smalley <stephen.smalley.work@gmail.com>
> Signed-off-by: James Carter <jwcart2@gmail.com>

Any idea why it used that logic previously?  And how does this compare
with sepol_port_compare/compare2() used by libsemanage?
Regardless,
Acked-by: Stephen Smalley <stephen.smalley.work@gmail.com>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] libselpol: Sort portcon rules consistently
  2020-05-29 14:57 ` Stephen Smalley
@ 2020-05-29 15:34   ` James Carter
  2020-06-02 18:09     ` Stephen Smalley
  0 siblings, 1 reply; 4+ messages in thread
From: James Carter @ 2020-05-29 15:34 UTC (permalink / raw)
  To: Stephen Smalley; +Cc: SElinux list, slawrence

On Fri, May 29, 2020 at 10:57 AM Stephen Smalley
<stephen.smalley.work@gmail.com> wrote:
>
> On Thu, May 28, 2020 at 2:41 PM James Carter <jwcart2@gmail.com> wrote:
> >
> > The comparison function, portcon_data_cmp(), only made use of the
> > protocol to put tcp before udp, dccp, and sctp. Rules that have
> > the same port range, but with different protocols would be considered
> > equal unless one of the protocols was tcp. When generating a CIL or
> > conf source policy from a binary or using the "-S" option in
> > checkpolicy the non-tcp portcon rules with the same port range would
> > not be consistently sorted.
> >
> > Changed portcon_data_cmp() to sort portcon rules like the CIL function
> > cil_post_portcon_compare().
> >
> > Reported-by: Stephen Smalley <stephen.smalley.work@gmail.com>
> > Signed-off-by: James Carter <jwcart2@gmail.com>
>
> Any idea why it used that logic previously?  And how does this compare
> with sepol_port_compare/compare2() used by libsemanage?

It originally followed the logic in CIL. I updated the CIL logic in
2018 (see commit 4ba19b541 ("libsepol/cil: Improve processing of
context rules"), but failed to update the logic in kernel_to_common.

The logic is similar, but slightly different. The logic in CIL and in
kernel_to_common puts smaller port ranges before larger ones, but
sepol_port_compare/compare2() do not take into account the port range.
Other than that they are the same (with this patch). I am not sure
where the CIL ordering logic came from, Steve Lawrence might remember.

Jim

> Regardless,
> Acked-by: Stephen Smalley <stephen.smalley.work@gmail.com>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] libselpol: Sort portcon rules consistently
  2020-05-29 15:34   ` James Carter
@ 2020-06-02 18:09     ` Stephen Smalley
  0 siblings, 0 replies; 4+ messages in thread
From: Stephen Smalley @ 2020-06-02 18:09 UTC (permalink / raw)
  To: James Carter; +Cc: SElinux list, Steve Lawrence

On Fri, May 29, 2020 at 11:35 AM James Carter <jwcart2@gmail.com> wrote:
>
> On Fri, May 29, 2020 at 10:57 AM Stephen Smalley
> <stephen.smalley.work@gmail.com> wrote:
> >
> > On Thu, May 28, 2020 at 2:41 PM James Carter <jwcart2@gmail.com> wrote:
> > >
> > > The comparison function, portcon_data_cmp(), only made use of the
> > > protocol to put tcp before udp, dccp, and sctp. Rules that have
> > > the same port range, but with different protocols would be considered
> > > equal unless one of the protocols was tcp. When generating a CIL or
> > > conf source policy from a binary or using the "-S" option in
> > > checkpolicy the non-tcp portcon rules with the same port range would
> > > not be consistently sorted.
> > >
> > > Changed portcon_data_cmp() to sort portcon rules like the CIL function
> > > cil_post_portcon_compare().
> > >
> > > Reported-by: Stephen Smalley <stephen.smalley.work@gmail.com>
> > > Signed-off-by: James Carter <jwcart2@gmail.com>
> >
> > Any idea why it used that logic previously?  And how does this compare
> > with sepol_port_compare/compare2() used by libsemanage?
>
> It originally followed the logic in CIL. I updated the CIL logic in
> 2018 (see commit 4ba19b541 ("libsepol/cil: Improve processing of
> context rules"), but failed to update the logic in kernel_to_common.
>
> The logic is similar, but slightly different. The logic in CIL and in
> kernel_to_common puts smaller port ranges before larger ones, but
> sepol_port_compare/compare2() do not take into account the port range.
> Other than that they are the same (with this patch). I am not sure
> where the CIL ordering logic came from, Steve Lawrence might remember.
>
> Jim
>
> > Regardless,
> > Acked-by: Stephen Smalley <stephen.smalley.work@gmail.com>

Applied.

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2020-06-02 18:10 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-05-28 18:40 [PATCH] libselpol: Sort portcon rules consistently James Carter
2020-05-29 14:57 ` Stephen Smalley
2020-05-29 15:34   ` James Carter
2020-06-02 18:09     ` Stephen Smalley

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).