pg.ddx.io pgsql-bugs@postgresql.org mailing list archive
help / color / mirror / Atom feedA potential memory access violation in ecpg when using EXEC SQL INCLUDE
2+ messages / 2 participants
[nested] [flat]
* A potential memory access violation in ecpg when using EXEC SQL INCLUDE
@ 2019-03-08 02:18 Wu, Fei <wufei.fnst@cn.fujitsu.com>
0 siblings, 1 reply; 2+ messages in thread
From: Wu, Fei @ 2019-03-08 02:18 UTC (permalink / raw)
To: pgsql-bugs@lists.postgresql.org <pgsql-bugs@lists.postgresql.org>
Hi, everyone.
I have found a potential memory access violation in ecpg module. And I found that this problem happens in all postgres version.
Here is:
https://github.com/postgres/postgres/blob/REL9_5_16/src/interfaces/ecpg/preproc/pgc.l
----------------------------------------------------------------------------------------------------------------------------
1385 /* If file name is enclosed in '"' remove these and look only in '.' */
1386 /* Informix does look into all include paths though, except filename starts with '/' */
1387 if (yytext[0] == '"' && yytext[i] == '"' &&
1388 ((compat != ECPG_COMPAT_INFORMIX && compat != ECPG_COMPAT_INFORMIX_SE) || yytext[1] == '/'))
1389 {
1390 yytext[i] = '\0';
1391 memmove(yytext, yytext+1, strlen(yytext));
1392
1393 strlcpy(inc_file, yytext, sizeof(inc_file));
1394 yyin = fopen(inc_file, "r");
1395 if (!yyin)
1396 {
1397 if (strcmp(inc_file + strlen(inc_file) - 2, ".h") != 0) ★
1398 {
1399 strcat(inc_file, ".h");
1400 yyin = fopen(inc_file, "r");
1401 }
1402 }
1403
1404 }
----------------------------------------------------------------------------------------------------------------------------
When precompile ecpg program (running “ecpg xxx.pgc” is enough )which has below statement:
------------------------------------
EXEC SQL INCLUDE “a”
------------------------------------
(Here, “a” is short for “a.h” , this feature is documented at https://www.postgresql.org/docs/9.5/ecpg-preproc.html#ECPG-INCLUDE )
The ecpg command runs into above program fragment and inc_file’s value is string “a” which strlen(inc_file) is 1.
Here, ecpg first try to open head file named “a”,which does not exists. Obviously, failed.
Then,ecpg try to find out that if the given filename “a” has suffix “.h” in code line marked as ★ above.
Here, strlen(inc_file) is 1 ,so ecpg access the address inc_file �C 1 . That means access the address out of inc_file.
It obviously is a potential problem which may does not lead to error or crash in most time.But it is a hidden danger which should be fixed.
Last, it is easy to fix, here is a minimum reproduction case and a solution patch.
--
Best Regards
-----------------------------------------------------
Wu Fei
DX3
Nanjing Fujitsu Nanda Software Tech. Co., Ltd.(FNST)
ADDR.: No.6 Wenzhu Road, Software Avenue,
Nanjing, 210012, China
TEL : +86+25-86630566-9356
COINS: 7998-9356
FAX: +86+25-83317685
MAIL:wufei.fnst@cn.fujitsu.com
http://www.fujitsu.com/cn/fnst/
---------------------------------------------------
Attachments:
minimum_reproduction_case.pgc (application/octet-stream, 114B) download
ecpg_include_memory_access_violation.patch (application/octet-stream, 418B) download
diff -Naur old/pgc.l new/pgc.l
--- old/pgc.l 2019-03-07 21:10:29.935001732 -0500
+++ new/pgc.l 2019-03-07 21:13:05.972001467 -0500
@@ -1394,7 +1394,7 @@
yyin = fopen(inc_file, "r");
if (!yyin)
{
- if (strcmp(inc_file + strlen(inc_file) - 2, ".h") != 0)
+ if (strlen(inc_file) <= 2 || strcmp(inc_file + strlen(inc_file) - 2, ".h") != 0)
{
strcat(inc_file, ".h");
yyin = fopen(inc_file, "r");
^ permalink raw reply [nested|flat] 2+ messages in thread
* Re: A potential memory access violation in ecpg when using EXEC SQL INCLUDE
@ 2019-03-11 15:21 Michael Meskes <meskes@postgresql.org>
parent: Wu, Fei <wufei.fnst@cn.fujitsu.com>
0 siblings, 0 replies; 2+ messages in thread
From: Michael Meskes @ 2019-03-11 15:21 UTC (permalink / raw)
To: Wu, Fei <wufei.fnst@cn.fujitsu.com>; pgsql-bugs@lists.postgresql.org <pgsql-bugs@lists.postgresql.org>
Hi all,
> I have found a potential memory access violation in ecpg module. And
> I found that this problem happens in all postgres version.
> ...
Thanks for finding and fixing. Committed.
Michael
--
Michael Meskes
Michael at Fam-Meskes dot De, Michael at Meskes dot (De|Com|Net|Org)
Meskes at (Debian|Postgresql) dot Org
Jabber: michael at xmpp dot meskes dot org
VfL Borussia! Força Barça! SF 49ers! Use Debian GNU/Linux, PostgreSQL
^ permalink raw reply [nested|flat] 2+ messages in thread
end of thread, other threads:[~2019-03-11 15:21 UTC | newest]
Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2019-03-08 02:18 A potential memory access violation in ecpg when using EXEC SQL INCLUDE Wu, Fei <wufei.fnst@cn.fujitsu.com>
2019-03-11 15:21 ` Michael Meskes <meskes@postgresql.org>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox