SA-CORE-2009-001: possible SQL injection risk in node_access()

pwolanin - July 4, 2008 - 20:58
Project:Drupal
Version:7.x-dev
Component:node.module
Category:bug report
Priority:critical
Assigned:Unassigned
Status:closed
Issue tags:security.drupal.org
Description

reported by: Giles Kennedy

this code caught my eye:

$sql = "SELECT COUNT(*) FROM {node_access} WHERE (nid = 0 OR nid = %d) $grants_sql AND grant_$op >= 1";

(from http://api.drupal.org/api/function/node_access/6).

$op *should* be one of "view", "update", "create" or "delete" and so *should* be safe ... if a variable is used (e.g. in a custom/contrib module) to pass $op to node_access(), and if that variable is derived from user input, then we are relying on the module to sanitise $op correctly to prevent SQL injection.

it should actually use http://api.drupal.org/api/function/db_escape_table/6 to make sure that the constructed table name is well-formed.

#1

pwolanin - July 4, 2008 - 21:03
Status:active» needs review

Trivial patch, but not tested.

AttachmentSize
grant-op-278675-1.patch 842 bytes
Testbed results
grant-op-278675-1.patchpassedPassed: 8994 passes, 0 fails, 0 exceptions Detailed results

#2

pwolanin - July 4, 2008 - 21:06

here's the 6.x backport

AttachmentSize
grant-op-278675-6x-2.patch 854 bytes
Testbed results
grant-op-278675-6x-2.patchpassedPassed: 8994 passes, 0 fails, 0 exceptions Detailed results

#3

robertDouglass - July 6, 2008 - 14:34

I wonder if we shouldn't somehow validate $op? Perhaps both changes are advisable, but here's another patch to consider. Taking the time to validate $op seems like a better way to handle the issue to me. If we had the concept of error handling in Drupal we'd actually throw an error if $op were not one of the allowed values. I think it's rather critical to not only ensure no SQL injection can occur (like the suggested patch), but that the allowed values actually match the expected columns of the database.

The question remains whether anyone extends the {node_access} table with other grants, in which case this patch is undesirable, but other than that, I prefer it.

AttachmentSize
node_access.patch 736 bytes
Testbed results
node_access.patchpassedPassed: 8975 passes, 0 fails, 0 exceptions Detailed results

#4

pwolanin - July 6, 2008 - 14:41

I'd rather not fix the op values - is there anything that says another module can't define another op? I guess new columns would have to be added? But hook_schema_alter is available for just such a reason.

#5

robertDouglass - July 6, 2008 - 14:45

Ok, I'm convinced. Best to go with #2 for now.

#6

paul.lovvik - July 7, 2008 - 20:49
Status:needs review» reviewed & tested by the community

Reviewed both patches (#1 and #2) and verified that they work on their respective branches. Both look good.

#7

catch - August 14, 2008 - 17:02

#1 and #2 still apply.

#8

catch - October 2, 2008 - 16:00
Status:reviewed & tested by the community» needs work

Needs a re-roll for dbtng.

#9

drewish - November 24, 2008 - 09:41
Status:needs work» needs review

so here's the minimal DBTNG udpate. looking at the conditionals code I'm not seeing a good way to ensure the order of operations on nested OR and AND operators. i'm not convinced that the conditions framework is expressive enough at this point. maybe we can get this committed then revisit the full query.

i started looking for the unit tests for node_access and it doesn't look like there are any... that's really freaking scary... i'll see if there's an issue for that some place.

AttachmentSize
node_278675.patch 1.11 KB
Testbed results
node_278675.patchpassedPassed: 8994 passes, 0 fails, 0 exceptions a href=http://testing.drupal.org/pifr/file/1/node_278675.patchDetailed results/a

#10

drewish - November 25, 2008 - 00:12

Crell explained to me how to get the conditions to do what they'd need to do but I'm not going to say we should hold up the patch over it. If i get a chance I'll pick this back up and work on converting it to a full DBTNG dynamic query.

And look at adding some unit tests... eek.

#11

catch - January 17, 2009 - 21:21
Status:needs review» needs work

This is already in 6.x as of http://drupal.org/node/358957 so needs forward porting from there.

#12

pwolanin - January 20, 2009 - 21:32
Title:Column name in node access query is not checked for corr» SA-CORE-2009-001: possible SQL injection risk in node_access()
Priority:normal» critical

here's the patch that went into D6

AttachmentSize
node-access-sdo590-D6-7.patch 784 bytes
Testbed results
node-access-sdo590-D6-7.patchpassedPassed: 8994 passes, 0 fails, 0 exceptions Detailed results

#13

Dave Reid - January 20, 2009 - 21:54
Status:needs work» needs review

Rerolled along with some trivial whitespace changes in node.module picked up by my IDE.

AttachmentSize
278675-SA-2009-01-D7.patch 1.52 KB
Testbed results
278675-SA-2009-01-D7.patchpassedPassed: 8994 passes, 0 fails, 0 exceptions Detailed results

#14

Dries - January 21, 2009 - 14:51
Status:needs review» fixed

Committed to CVS HEAD. Thanks!

#15

System Message - February 4, 2009 - 15:00
Status:fixed» closed

Automatically closed -- issue fixed for 2 weeks with no activity.

#16

pwolanin - March 9, 2009 - 14:31

tagging

 
 

Drupal is a registered trademark of Dries Buytaert.