Buffer overrun in dbpr with long INP field

Bug #1776141 reported by Martin Konrad on 2018-06-11
This bug affects 1 person
Affects Status Importance Assigned to Milestone
Status tracked in 7.0
Andrew Johnson
Andrew Johnson

Bug Description

softIoc crashes with a buffer overflow in dbTest.c:1152 when running "dbpr A 4" on the attached database file (test.db).

Root cause: pmsg points to msgBuff->message which has a fixed size of 128 but the output of sprintf can be longer.

I can see two potential solutions here:

1. Use snprintf() to prevent the buffer overflow.
2. Convert the file to C++ and use strings.

Note: dbTest.c contains a total of 23 sprintf() calls so there might be potential for more issues...

Changed in epics-base:
assignee: nobody → Martin Konrad (info-martin-konrad)
mdavidsaver (mdavidsaver) wrote :

I'm not able to replicate with current 3.16 branch (plus some extra changes, but I don't think relevant). I see an explicit error and no crash. Valgrind doesn't report a bounds violation.

epics> dbpr A 4
dbpr_msgOut: ERROR - msg length=149 limit=80

Here is what I did:

git clone https://git.launchpad.net/epics-base
cd epics-base/
git checkout 3.16
wget https://bugs.launchpad.net/epics-base/+bug/1776141/+attachment/5151133/+files/test.db
./bin/linux-x86_64/softIoc -D dbd/softIoc.dbd -d test.db

mdavidsaver (mdavidsaver) wrote :

As we found yesterday, the specific error you see is due to building with _FORTIFY_SOURCE checks enabled.

The print which triggers this is in the DBF_*LINK case of dbpr_report().

sprintf(pmsg,"%s:%s %s", pfield_name,

Specifically the "dbGetString()" call is returning a long string.

mdavidsaver (mdavidsaver) wrote :

*** buffer overflow detected ***: ./bin/linux-x86_64/softIoc terminated
======= Backtrace: =========

mdavidsaver (mdavidsaver) wrote :

cat <<EOF > configure/os/CONFIG_SITE.Common.linux-x86_64
OP_SYS_CFLAGS += -fdebug-prefix-map=$(TOP)=. -fstack-protector-strong -Wformat -Werror=format-security
OP_SYS_CXXFLAGS += -fdebug-prefix-map=$(TOP)=. -fstack-protector-strong -Wformat -Werror=format-security

Andrew Johnson (anj) wrote :

I have just committed some changes to the 3.16 branch which should fix this bug. I already had some changes ready that improve the output from dbpr, so I committed those, fixed the erroneous sprintf() that caused the above problem, and also fixed an incorrect output problem when printing long strings from dbgf.

Please update and test.

Changed in epics-base:
status: New → In Progress

I can confirm that Andrew's change fixed the bug.

This bug also affects Base 3.15.

Andrew Johnson (anj) wrote :

@Ralph is this important enough to hold up 3.15.6 for? That's what attaching that milestone to the bug means to me.

IIRC my fix on the 3.16 branch relied on some other changes that were already in dbTest.c so back-porting the fix to the 3.15 branch might take some work.

Ralph Lange (ralph-lange) wrote :

I admit I have lost track on where we stand wrt the next releases.

No, I don't think this would be a reason to hold up anything. Feel free to close as wontfix or retarget.

Andrew Johnson (anj) wrote :

I just untargeted 3.15.6; as Martin says the bug affects the 3.15 branch so I won't close it.

Andrew Johnson (anj) wrote :

Core Group review at ESS: Fixed in 3.16, we're open to someone else back-porting or otherwise fixing 3.15.

To post a comment you must log in.
This report contains Public information  Edit
Everyone can see this information.

Other bug subscribers

Bug attachments