Skip to contentSkip to conversation

flow: Cache negative results

IPSEC Herbert Xu 5 participants 12 messages Jan 10, 2007 – Jan 11, 2007

Subsystems (2)
lore
Share & tools
lore

Herbert Xu

author
#0 lore raw

Hi:

[IPSEC] flow: Cache negative security checks

This patch causes security policy denials to be cached instead of causing a relookup every time. This is OK because we already cache positive security policy results which is strictly worse as far as security is concerned. In particular, if the security system (not IPsec policies but the rules under security/) changes such that a positive result turns negative (denial), we will ignore it and continue to allow traffic through based on the cached policy.

So if the security folks actually care about this, they'd need to flush the flow cache whenever a relevant change is made to the security database. Whether this is done or not does not affect this patch.

Given that we do want to cache positive results even in the presence of SELinux (otherwise we might as well disable flow.c entirely), it is natural to cache negative results too.

This patch also happens to fix a nasty bug where if an expiring flow entry that's not at the head happens to trigger a security denial, all entries before it are removed from the cache and leaked.

Cheers,

Signature · 5 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
--
net/core/flow.c +12 -16
index d137f97..b0d1777 100644
@@ -197,12 +197,16 @@ void *flow_cache_lookup(struct flowi *key, u16 family, u8 dir,
 			if (fle->genid == atomic_read(&flow_cache_genid)) {
 				void *ret = fle->object;
 
-				if (ret)
+				if (fle->object_ref)
 					atomic_inc(fle->object_ref);
 				local_bh_enable();
 
 				return ret;
 			}
+
+			if (fle->object_ref)
+				atomic_dec(fle->object_ref);
+			fle->object_ref = NULL;
 			break;
 		}
 	}
@@ -218,7 +222,7 @@ void *flow_cache_lookup(struct flowi *key, u16 family, u8 dir,
 			fle->family = family;
 			fle->dir = dir;
 			memcpy(&fle->key, key, sizeof(*key));
-			fle->object = NULL;
+			fle->object_ref = NULL;
 			flow_count(cpu)++;
 		}
 	}
@@ -230,28 +234,20 @@ nocache:
 		atomic_t *obj_ref;
 
 		err = resolver(key, family, dir, &obj, &obj_ref);
+		if (err)
+			obj = ERR_PTR(err);
 
 		if (fle) {
-			if (err) {
-				/* Force security policy check on next lookup */
-				*head = fle->next;
-				flow_entry_kill(cpu, fle);
-			} else {
-				fle->genid = atomic_read(&flow_cache_genid);
+			fle->object = obj;
+			fle->genid = atomic_read(&flow_cache_genid);
 
-				if (fle->object)
-					atomic_dec(fle->object_ref);
-
-				fle->object = obj;
+			if (!err && obj) {
 				fle->object_ref = obj_ref;
-				if (obj)
-					atomic_inc(fle->object_ref);
+				atomic_inc(obj_ref);
 			}
 		}
 		local_bh_enable();
 
-		if (err)
-			obj = ERR_PTR(err);
 		return obj;
 	}
 }
maintainer
#1 lore raw

From: Herbert Xu <[email protected]>
Date: Wed, 10 Jan 2007 18:22:51 +1100

Quoted 4 lines

This patch also happens to fix a nasty bug where if an expiring flow entry that's not at the head happens to trigger a security denial, all entries before it are removed from the cache and leaked.

Nasty, this is because "fle != NULL" can occur in the 'nocache' path for two reasons. New entry, or the case that's buggy here, existing entry with out-of-date generation ID.

Thanks Herbert, I'll look this over some more and apply it some time tomorrow.

#2 lore raw
In reply to Herbert Xu
Quoted 3 lines · Herbert Xu

Hi:

[IPSEC] flow: Cache negative security checks

We did some work in this part of the code a few months back -- IIRC it was resolved correctly from a security point of view.

(cc'ing Venkat & Paul for review).

Quoted 27 lines

This patch causes security policy denials to be cached instead of causing a relookup every time. This is OK because we already cache positive security policy results which is strictly worse as far as security is concerned. In particular, if the security system (not IPsec policies but the rules under security/) changes such that a positive result turns negative (denial), we will ignore it and continue to allow traffic through based on the cached policy.

So if the security folks actually care about this, they'd need to flush the flow cache whenever a relevant change is made to the security database. Whether this is done or not does not affect this patch.

Given that we do want to cache positive results even in the presence of SELinux (otherwise we might as well disable flow.c entirely), it is natural to cache negative results too.

This patch also happens to fix a nasty bug where if an expiring flow entry that's not at the head happens to trigger a security denial, all entries before it are removed from the cache and leaked.

Cheers,

Signature · 2 lines
James Morris
<[email protected]>
#3 lore raw
Quoted 3 lines
Quoted 3 lines

So if the security folks actually care about this, they'd need to flush the flow cache whenever a relevant change is made to the security database.

I do not believe we are doing this. I will look into this ASAP.

#10 lore raw
In reply to Herbert Xu
Quoted 3 lines · Herbert Xu

Hi:

[IPSEC] flow: Cache negative security checks

Hi Herbert,

I'm far from a flow cache expert (David, James, and Venkat will probably be able to give you much better feedback) I did notice a few things which may or may not be issues ... comments below. FWIW, I believe Venkat is right in that it shouldn't have any effect on the LSM code.

However, there is one other thing that came up when I was looking at your patch, unrelated to your changes, that I wanted to ask about: in net/core/flow.c:flow_entry_kill() ...

Quoted 3 lines

static void flow_entry_kill(int cpu, struct flow_cache_entry *fle)

{
       if (fle->object)

Should this be "if (fle->object_ref)"?

Quoted 4 lines
               atomic_dec(fle->object_ref);
       kmem_cache_free(flow_cachep, fle);
       flow_count(cpu)--; 
}

Your changes:

Quoted 5 lines

Index: net-2.6_review/net/core/flow.c
===================================================================

net/core/flow.c void *flow_cache_lookup(struct flowi *ke · line 205

Quoted 10 lines
net/core/flow.c +1 -1
@@ -197,12 +197,16 @@ void *flow_cache_lookup(struct flowi *ke
                        if (fle->genid == atomic_read(&flow_cache_genid)) {
                                void *ret = fle->object;
 
-                               if (ret)
+                               if (fle->object_ref)
                                        atomic_inc(fle->object_ref);
                                local_bh_enable();
 
                                return ret;
                        }

This is a real nit, but would it be better to get rid of the "ret" variable entirely and simply "return (void *)fle->object" while you're at it?

net/core/flow.c void *flow_cache_lookup(struct flowi *key, u16 family, u8 dir, · line 210

Quoted 5 lines
net/core/flow.c +4 -0
+
+                       if (fle->object_ref)
+                               atomic_dec(fle->object_ref);
+                       fle->object_ref = NULL;
                        break;

It seems that we could potentially save an operation in some cases by moving the "fle->object_ref = NULL" statement inside the if block, for example:

 if (fle->object_ref) {
         atomic_dec(fle->object_ref);
         fle->object_ref = NULL;

}

Quoted 12 lines
net/core/flow.c +2 -4
@@ -230,28 +234,20 @@ nocache:
                atomic_t *obj_ref;
 
                err = resolver(key, family, dir, &obj, &obj_ref);
+               if (err)
+                       obj = ERR_PTR(err);
 
                if (fle) {
-                       if (err) {
-                               /* Force security policy check on next ...
-                               *head = fle->next;
-                               flow_entry_kill(cpu, fle);

net/core/flow.c nocache: · line 244

Quoted 10 lines
net/core/flow.c +3 -6
-                       } else {
-                               fle->genid = atomic_read(&flow_cache_genid);
+                       fle->object = obj;
+                       fle->genid = atomic_read(&flow_cache_genid);
 
-                               if (fle->object)
-                                       atomic_dec(fle->object_ref);
-
-                               fle->object = obj;
+                       if (!err && obj) {

Should this if statement be changed to "if (!err && obj_ref) {"?

Quoted 13 lines
net/core/flow.c +1 -4
                                fle->object_ref = obj_ref;
-                               if (obj)
-                                       atomic_inc(fle->object_ref);
+                               atomic_inc(obj_ref);
                        }
                }
                local_bh_enable();
 
-               if (err)
-                       obj = ERR_PTR(err);
                return obj;
        }
 }
Signature · 2 lines
paul moore
linux security @ hp
#4 lore raw
In reply to James Morris
Quoted 2 lines
Quoted 2 lines

This patch causes security policy denials to be cached instead of causing a relookup every time.

Only, on a security policy denial (-ESRCH from the LSM hook), a 0 is returned by the resolver to signify no applicable policy since a negative result is akin to no policy. And I see the "no policy" case is already cached.

I think what may have gotten us here is the comment:

                       if (err) {
                                /* Force security policy check on next
lookup */
                                *head = fle->next;
                                flow_entry_kill(cpu, fle);
                        } else {

But the error we would be looking at here would be a non-denial related error (neither positive nor negative) that the security server may have run into, in which case we would in fact want to attempt a full-lookup again the next time.

<snip>

Quoted 3 lines
Quoted 3 lines

So if the security folks actually care about this, they'd need to flush the flow cache whenever a relevant change is made to the security database.

Sure. Will look into this.

Quoted 5 lines
Quoted 5 lines

This patch also happens to fix a nasty bug where if an expiring flow entry that's not at the head happens to trigger a security denial, all entries before it are removed from the cache and leaked.

I think just leaving the flow_entry as it is will take care of it.

IOW, no entry_killing nor messing with it's object/reference. This should naturally cause us to invoke the resolver again the next time.

author
#5 lore raw
Quoted 5 lines · Venkat Yekkirala

Only, on a security policy denial (-ESRCH from the LSM hook), a 0 is returned by the resolver to signify no applicable policy since a negative result is akin to no policy. And I see the "no policy" case is already cached.

I'm not talking about an xfrm policy lookup failure, that exists with or without SELinux. I'm talking about an error returned from security_xfrm_policy_lookup(), i.e., whether a policy can be used or not. For that case, we only cache positive results currently.

Cheers,

Signature · 4 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
#6 lore raw
Quoted 9 lines
Quoted 4 lines

Only, on a security policy denial (-ESRCH from the LSM hook), a 0 is returned by the resolver to signify no applicable policy since a negative result is akin to no policy. And I see the "no policy" case is already cached.

I'm not talking about an xfrm policy lookup failure, that exists with or without SELinux. I'm talking about an error returned from security_xfrm_policy_lookup(), i.e., whether a policy can be used or not.

I was talking about this (the latter) as well. Currently, on a proper "negative", -ESRCH is returned by security_xfrm_policy_lookup(), and this comes back up as a 0 from resolver(), correctly indicating NO applicable
xfrm policy (after taking security into account). But if security_xfrm_policy_lookup()
were to return anything other than a zero or -ESRCH, such as -ENOMEM, you will see it come back up as such (as -ENOMEM) from resolver(), and in this case, it's neither a positive nor a negative, just an error. Hence a full lookup would be in order, the next time round.

Quoted 1 line

For that case, we only cache positive results currently.

Negatives are currently properly cached as NULL. Any errors returned from resolver() are true errors, not negatives. Hence, they needn't be cached.

Also, I would fix the other bug you had noted, by something like:

@@ -232,11 +232,7 @@ nocache:

                err = resolver(key, family, dir, &obj, &obj_ref);

                if (fle) {

- if (err) {
- /* Force security policy check on next
lookup */
- *head = fle->next;
- flow_entry_kill(cpu, fle);
- } else {

+                       if (!err) {
                                fle->genid = atomic_read(&flow_cache_genid);

                                if (fle->object)

I am planning to test and submit a patch to SELinux to invoke flow_cache_flush() on policy reloads tomorrow.

I believe changes to labels on SPD rules are already taken care of by checks involving flow_cache_genid.

author
#7 lore raw
Quoted 11 lines · Venkat Yekkirala

I was talking about this (the latter) as well. Currently, on a proper "negative", -ESRCH is returned by security_xfrm_policy_lookup(), and this comes back up as a 0 from resolver(), correctly indicating NO applicable
xfrm policy (after taking security into account). But if security_xfrm_policy_lookup()
were to return anything other than a zero or -ESRCH, such as -ENOMEM, you will see it come back up as such (as -ENOMEM) from resolver(), and in this case, it's neither a positive nor a negative, just an error. Hence a full lookup would be in order, the next time round.

OK, I see that now. The EACCES error is translated to ESRCH in the security layer.

Quoted 1 line

Also, I would fix the other bug you had noted, by something like:

I agree.

[IPSEC] flow: Fix potential memory leak

When old flow cache entries that are not at the head of their chain trigger a transient security error they get unlinked along with all the entries preceding them in the chain. The preceding entries are not freed correctly.

This patch fixes this by simply leaving the entry around. It's based on a suggestion by Venkat Yekkirala.

Quoted 2 lines

I am planning to test and submit a patch to SELinux to invoke flow_cache_flush() on policy reloads tomorrow.

flow_cache_flush() shouldn't be used by SELinux. You probably want to increase the genid instead.

BTW, we should probably add some code to deal with overflows in genid. With the recent improvements in policy update speeds, it could be conceivable for an overflow to occur.

Cheers,

Signature · 5 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
--
net/core/flow.c +10 -16
index d137f97..5d25697 100644
@@ -231,22 +231,16 @@ nocache:
 
 		err = resolver(key, family, dir, &obj, &obj_ref);
 
-		if (fle) {
-			if (err) {
-				/* Force security policy check on next lookup */
-				*head = fle->next;
-				flow_entry_kill(cpu, fle);
-			} else {
-				fle->genid = atomic_read(&flow_cache_genid);
-
-				if (fle->object)
-					atomic_dec(fle->object_ref);
-
-				fle->object = obj;
-				fle->object_ref = obj_ref;
-				if (obj)
-					atomic_inc(fle->object_ref);
-			}
+		if (fle && !err) {
+			fle->genid = atomic_read(&flow_cache_genid);
+
+			if (fle->object)
+				atomic_dec(fle->object_ref);
+
+			fle->object = obj;
+			fle->object_ref = obj_ref;
+			if (obj)
+				atomic_inc(fle->object_ref);
 		}
 		local_bh_enable();
 
author
#11 lore raw
In reply to Paul Moore

Hi Paul:

Quoted 10 lines · Paul Moore

I'm far from a flow cache expert (David, James, and Venkat will probably be able to give you much better feedback) I did notice a few things which may or may not be issues ... comments below. FWIW, I believe Venkat is right in that it shouldn't have any effect on the LSM code.

However, there is one other thing that came up when I was looking at your patch, unrelated to your changes, that I wanted to ask about: in net/core/flow.c:flow_entry_kill() ...

Quoted 10 lines
Quoted 3 lines

static void flow_entry_kill(int cpu, struct flow_cache_entry *fle)

{
       if (fle->object)

Should this be "if (fle->object_ref)"?

Quoted 4 lines
               atomic_dec(fle->object_ref);
       kmem_cache_free(flow_cachep, fle);
       flow_count(cpu)--; 
}

Yep this is a real bug. Thanks for spotting it!

Although with Venkat's suggestion this change is no longer needed.

Cheers,

Signature · 4 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
maintainer
#8 lore raw
In reply to Herbert Xu

From: Herbert Xu <[email protected]>
Date: Thu, 11 Jan 2007 10:27:13 +1100

Quoted 27 lines · Venkat Yekkirala
Quoted 11 lines

I was talking about this (the latter) as well. Currently, on a proper "negative", -ESRCH is returned by security_xfrm_policy_lookup(), and this comes back up as a 0 from resolver(), correctly indicating NO applicable
xfrm policy (after taking security into account). But if security_xfrm_policy_lookup()
were to return anything other than a zero or -ESRCH, such as -ENOMEM, you will see it come back up as such (as -ENOMEM) from resolver(), and in this case, it's neither a positive nor a negative, just an error. Hence a full lookup would be in order, the next time round.

OK, I see that now. The EACCES error is translated to ESRCH in the security layer.

Quoted 1 line

Also, I would fix the other bug you had noted, by something like:

I agree.

[IPSEC] flow: Fix potential memory leak

When old flow cache entries that are not at the head of their chain trigger a transient security error they get unlinked along with all the entries preceding them in the chain. The preceding entries are not freed correctly.

Quoted 4 lines

This patch fixes this by simply leaving the entry around. It's based on a suggestion by Venkat Yekkirala.

This version of the fix looks good, I'll apply it, thanks Herbert.

Quoted 6 lines
Quoted 3 lines

I am planning to test and submit a patch to SELinux to invoke flow_cache_flush() on policy reloads tomorrow.

flow_cache_flush() shouldn't be used by SELinux. You probably want to increase the genid instead.

That's what I recommend too. Active flushes are very bad for performance, passive garbage collection of stale entries should be used for coherency whenever possible.

Quoted 3 lines

BTW, we should probably add some code to deal with overflows in genid. With the recent improvements in policy update speeds, it could be conceivable for an overflow to occur.

Even if you had a very powerful computer, doing nothing but inserting and deleting policies, it'd take around 90 days for the generation count to overflow.

But of course it could happen.

One thing we could do is force a full flow cache flush when some percentage before the generation count would overflow. Say every %80 of the generation ID space? That gives enough space to ensure the flush tasklet runs before the ID really does wrap.

author
#9 lore raw
Quoted 5 lines · David Miller

One thing we could do is force a full flow cache flush when some percentage before the generation count would overflow. Say every %80 of the generation ID space? That gives enough space to ensure the flush tasklet runs before the ID really does wrap.

Yes that would be a good solution. It'd prevent overflows from causing any damage and is also pretty light weight (just a single comparison every time the genid changes).

Cheers,

Signature · 4 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt