Issue 6461 - back-sql quote characters in query
Summary: back-sql quote characters in query
Status: VERIFIED DUPLICATE of issue 9815
Alias: None
Product: OpenLDAP
Classification: Unclassified
Component: backends (show other issues)
Version: 2.4.19
Hardware: All All
: --- normal
Target Milestone: 2.5.12
Assignee: OpenLDAP project
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-01-24 19:17 UTC by tolich.arz@gmail.com
Modified: 2022-06-07 09:06 UTC (History)
0 users

See Also:


Attachments
ITS-6461-escape-single-quotes-in-back-sql.patch (3.01 KB, patch)
2020-03-19 19:34 UTC, Quanah Gibson-Mount
Details

Note You need to log in before you can comment on or make changes to this issue.
Description tolich.arz@gmail.com 2010-01-24 19:17:11 UTC
Full_Name: Anatoly
Version: 2.4.19
OS: GNU/Linux
URL: 
Submission from: (NULL) (89.169.85.181)


I'm using openldap 2.4.19 with sql backend. I have a troubles with
queries that contains single-quote ( ' ) character.
For example, if I searching for (cn=Zool'man):

<==backsql_srch_query() returns SELECT DISTINCT
ldap_entries.id,phpbb_users.user_id,varchar_ci('phpbbUser') AS
objectClass,ldap_entries.dn AS dn FROM ldap_entries,phpbb_users WHERE
phpbb_users.user_id=ldap_entries.keyval AND ldap_entries.oc_map_id=?
AND 9=9 AND (varchar_ci(phpbb_users.username)='ZOOL'MAN')
Constructed query: SELECT DISTINCT
ldap_entries.id,phpbb_users.user_id,varchar_ci('phpbbUser') AS
objectClass,ldap_entries.dn AS dn FROM ldap_entries,phpbb_users WHERE
phpbb_users.user_id=ldap_entries.keyval AND ldap_entries.oc_map_id=?
AND 9=9 AND (varchar_ci(phpbb_users.username)='ZOOL'MAN')
id: '2'
backsql_oc_get_candidates(): error executing query
Return code: -1
  nativeErrCode=7 SQLengineState=S1000 msg="[unixODBC]ERROR:  syntax
error at or near "MAN" at character 271;

In this case query should be like
varchar_ci(phpbb_users.username)='ZOOL\'MAN' instead of 'ZOOL'MAN'

Additionally, I fear this opens a possibility of sql injection, depending on
RDBMS.
Comment 1 ando@openldap.org 2010-01-25 04:25:35 UTC
changed notes
Comment 2 ando@openldap.org 2010-04-17 16:02:59 UTC
changed notes
moved from Incoming to Software Enhancements
Comment 3 ando@openldap.org 2010-04-17 16:03:16 UTC
moved from Software Enhancements to Software Bugs
Comment 4 Matthias Hunstock 2011-03-08 21:29:57 UTC
Can confirm this with openldap 2.4.24.

Using ldap search filters like this:

(cn=blabla' or '1'='1)

is at least causing my postgres to eat all CPU cycles it can get (LDAP
data is based on complex view). I do not have write access enabled for
that particular openLDAP installation, but I also assume that SQL
Injection is possible. Beside being an obviuos malfunction, this should
be considered a security issue.

atze

Comment 5 Howard Chu 2011-03-09 17:37:58 UTC
atze_80@web.de wrote:
> Can confirm this with openldap 2.4.24.

Thanks, the bug was already confirmed.
>
> Using ldap search filters like this:
>
> (cn=blabla' or '1'='1)
>
> is at least causing my postgres to eat all CPU cycles it can get (LDAP
> data is based on complex view). I do not have write access enabled for
> that particular openLDAP installation, but I also assume that SQL
> Injection is possible. Beside being an obviuos malfunction, this should
> be considered a security issue.

As the bug status says, "patches welcome." back-sql is not a priority for any 
of the core developers.
-- 
   -- Howard Chu
   CTO, Symas Corp.           http://www.symas.com
   Director, Highland Sun     http://highlandsun.com/hyc/
   Chief Architect, OpenLDAP  http://www.openldap.org/project/

Comment 6 OpenLDAP project 2014-08-01 21:04:29 UTC
is escaping portable?
use prepare instead; patch welcome
Comment 7 akagisho 2015-03-10 01:58:14 UTC
I have made a patch for this problem.

https://gist.github.com/akagisho/0d0d148c94616b84a513

2011-03-10 2:37 GMT+09:00 Howard Chu <hyc@symas.com>:
> atze_80@web.de wrote:
>>
>> Can confirm this with openldap 2.4.24.
>
>
> Thanks, the bug was already confirmed.
>>
>>
>> Using ldap search filters like this:
>>
>> (cn=blabla' or '1'='1)
>>
>> is at least causing my postgres to eat all CPU cycles it can get (LDAP
>> data is based on complex view). I do not have write access enabled for
>> that particular openLDAP installation, but I also assume that SQL
>> Injection is possible. Beside being an obviuos malfunction, this should
>> be considered a security issue.
>
>
> As the bug status says, "patches welcome." back-sql is not a priority for
> any of the core developers.

Comment 8 Quanah Gibson-Mount 2020-03-19 19:34:15 UTC
Created attachment 617 [details]
ITS-6461-escape-single-quotes-in-back-sql.patch
Comment 9 Quanah Gibson-Mount 2020-03-19 19:36:13 UTC
(In reply to akagisho from comment #7)
> I have made a patch for this problem.
> 
> https://gist.github.com/akagisho/0d0d148c94616b84a513

Your work is missing the required IPR information as detailed at https://www.openldap.org/devel/contributing.html#notice

Please provide the appropriate IPR

Regards,
Quanah
Comment 10 Howard Chu 2022-03-28 15:49:09 UTC
Escaping with a backslash appears to be non-portable. All the major SQL implementations escape a single quote by doubling it, as done in the patch for ITS#9815.
Comment 11 Howard Chu 2022-03-28 15:51:07 UTC

*** This issue has been marked as a duplicate of issue 9815 ***