A few places in core use the following construct:
[...] WHERE nid IN(%s)', implode(',', $nodes));
Because %s is not surrounded by quotes, it can be interpreted as SQL (not just data).
db_result(db_query("SELECT n.nid FROM {node} n INNER JOIN {og_ancestry} oga ON n.nid = oga.nid WHERE n.uid = %d AND n.type = '%s' AND oga.group_nid IN (%s)", $uid, $typename, $groups));
The above code is from a real life example. $groups is a parameter passed to the function. When the caller changed to pass $_REQUEST['gid'] this became the site of an SQL injection vulnerability with such URLs as:
?gids[]=0) UNION SELECT s.sid FROM sessions s WHERE s.uid = 1 /*
In our case, $nodes is an array of nids and this does not constitute a vulnerability. It is very brittle however as this code relies on the caller to remain secure. It is therefore all to easy to introduce vulnerabilities.
The attached patch simply replaces IN (%s) with IN (%d, %d, ...)
| Comment | File | Size | Author |
|---|---|---|---|
| #14 | in-s-D6-167284-14.patch | 8.46 KB | pwolanin |
| #7 | in-s-hardening_2.patch | 6.95 KB | pwolanin |
| in-s-hardening.patch | 6.94 KB | heine |
Comments
Comment #1
heine commentedComment #2
pwolanin commentedAn important cleanup for 5.x
It would be nice to have a more robust solution for 6.x. For example, make %s wrap its return value with single quotes (as %b already does).
Perhaps also a "multiple integer" placeholder for 6.x for _db_query_callback to simplify this sort of common call? For example %dd:
Comment #3
pwolanin commentedComment #4
moshe weitzman@drupal.org commentedNice example :)
Could you add a helper function like below and use it throughout? Is easier IMO than remembering the incantation yourself.
Comment #5
pwolanin commentedsee: http://drupal.org/node/167649 for a D6 issue for Moshe's suggestion or a another fix. This issue is D5, so I think a new APi function is out of the scope.
Comment #6
moshe weitzman commentedthere is nothing wrong with *adding* a helper function to an existing drupal release. it is part of fixing this issue in a clean fashion.
Comment #7
pwolanin commentedthere is an error in the taxonomy module part of this:
revised patch attached.
Comment #8
pwolanin commentedComment #9
pwolanin commentedrechecked - should be good to go.
Comment #10
drummCommitted to 5.x
Comment #11
kkaefer commentedThis needs to go into 6 as well!
Comment #12
pwolanin commentedYes - I was holding off on 6.x until the various split patches were done. I think we can proceed now.
Comment #13
webchickComment #14
pwolanin commentedok, this patch adds a helper function as Moshe wanted, and catches the same instances of IN(%s) in D6.
Also, a little doxygen cleanup in database.inc to get a single line description for a couple related functions.
Comment #15
Zothos commentedSomeone has a hint for me, how to test this patch? I think its critical but and i wanne help to get this thing in, unfortunately i have no idea how to test this issue on my test domain. XD
Comment #16
pwolanin commented@Zothos - the security hole is a theoretical one, rather than one that's obviously exploitable. So, in terms of testing this patch - nothing should break or be different after applying the patch. Especially the node admin form actions at admin/content/node, per-role block permissions, and general functioning of taxonomy pages and user permissions.
Comment #17
Zothos commentedI am sorry. Then my thoughts about this issue were completely wrong.
Comment #18
drewish commentedLooks real good. Tested:
- that the admin/build/block and user/* blocks work correctly.
- publishing, un-publishing, promoting, demoting, making sticky and unsticking both a single node and multiple nodes.
- the taxonomy/term/* page select nodes correctly.
- did some basic tests on user_access.
Comment #19
gábor hojtsySounds good, looks good. Committed. Thanks!
Comment #20
gábor hojtsyActually, this needs to be documented in the upgrade docs, so leaving this CNW.
Comment #21
webchickComment #22
pwolanin commenteddone: http://drupal.org/node/114774#placeholder_helper
Comment #23
(not verified) commented