Bug 268666 - syslog(3) prints parent's pid after fork()
Summary: syslog(3) prints parent's pid after fork()
Status: Closed FIXED
Alias: None
Product: Base System
Classification: Unclassified
Component: bin (show other bugs)
Version: 12.4-RELEASE
Hardware: Any Any
: --- Affects Many People
Assignee: Eugene Grosbein
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2022-12-31 10:16 UTC by Felix J. Ogris
Modified: 2023-01-06 05:16 UTC (History)
1 user (show)

See Also:


Attachments
proposed fix (471 bytes, patch)
2023-01-03 07:09 UTC, Eugene Grosbein
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Felix J. Ogris 2022-12-31 10:16:09 UTC
$ cat t.c
#include <unistd.h>
#include <syslog.h>
int main()
{
  openlog("test", LOG_PERROR, LOG_USER);
  syslog(LOG_INFO, "before fork: %d\n", getpid());
  fork();
  syslog(LOG_INFO, "after fork:  %d\n", getpid());
  return 0;
}
$ cc -o t t.c

On 12.4-RELEASE (note last line):
$ ./t
test 55317 - - before fork: 55317
test 55317 - - after fork:  55317
test 55317 - - after fork:  55318

On 13.1-RELEASE-p3:
$ ./t
test 16590 - - before fork: 16590
test 16590 - - after fork:  16590
test 16591 - - after fork:  16591

Suggested fix for 12.4: Make LogPid a local variable inside vsyslog1(), let parse_tag() return either the parsed pid or -1, and assign LogPid either that return value or getpid().
Comment 1 Eugene Grosbein freebsd_committer freebsd_triage 2023-01-03 07:09:10 UTC
Created attachment 239223 [details]
proposed fix
Comment 2 Eugene Grosbein freebsd_committer freebsd_triage 2023-01-03 07:10:43 UTC
Please test attached patch. You need to rebuild and reinstall libc only. Full buildworld not required.

fetch -o /tmp/syslog.c.diff https://bz-attachments.freebsd.org/attachment.cgi?id=239223
cd /usr/src/lib/libc
make obj depend && make all install

Running processes need restart to use fix.
Comment 3 Eugene Grosbein freebsd_committer freebsd_triage 2023-01-03 07:11:52 UTC
(In reply to Eugene Grosbein from comment #2)

Apply the patch after fetching it:

cd /usr/src
patch < /tmp/syslog.c.diff
Comment 4 Felix J. Ogris 2023-01-03 08:54:02 UTC
Thanks, your patch resolved this:

root@fbsd124:~ # freebsd-version -kru
12.4-RELEASE
12.4-RELEASE
12.4-RELEASE
root@fbsd124:~ # ./t
test 18410 - - before fork: 18410
test 18410 - - after fork:  18410
test 18411 - - after fork:  18411
root@fbsd124:~ # 

Had to build lib/libnetbsd before lib/libc, btw.
Comment 5 commit-hook freebsd_committer freebsd_triage 2023-01-03 08:59:04 UTC
A commit in branch main references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=2ce3ef55035093cac7839e71e9ff91f5562ebc29

commit 2ce3ef55035093cac7839e71e9ff91f5562ebc29
Author:     Eugene Grosbein <eugen@FreeBSD.org>
AuthorDate: 2023-01-03 08:53:47 +0000
Commit:     Eugene Grosbein <eugen@FreeBSD.org>
CommitDate: 2023-01-03 08:58:36 +0000

    syslog: fix PID of forking process

    Do not cache PID for a process that does not fabricate it,
    calls openlog() before forking and does not call exec() thereafter.

    PR:             268666
    Fixes:          e9ae9fa93745669b7dd0341d333257ad6cfe8e37
    Tested by:      kp
    MFC after:      3 days

 lib/libc/gen/syslog.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Comment 6 commit-hook freebsd_committer freebsd_triage 2023-01-06 04:41:45 UTC
A commit in branch stable/13 references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=77c0992af4e3b013915ae84ef3d831f470ea5dd5

commit 77c0992af4e3b013915ae84ef3d831f470ea5dd5
Author:     Eugene Grosbein <eugen@FreeBSD.org>
AuthorDate: 2023-01-03 08:53:47 +0000
Commit:     Eugene Grosbein <eugen@FreeBSD.org>
CommitDate: 2023-01-06 04:40:42 +0000

    syslog: fix PID of forking process

    Do not cache PID for a process that does not fabricate it,
    calls openlog() before forking and does not call exec() thereafter.

    PR:             268666
    Fixes:          e9ae9fa93745669b7dd0341d333257ad6cfe8e37
    Tested by:      kp

    (cherry picked from commit 2ce3ef55035093cac7839e71e9ff91f5562ebc29)

 lib/libc/gen/syslog.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Comment 7 commit-hook freebsd_committer freebsd_triage 2023-01-06 05:09:52 UTC
A commit in branch stable/12 references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=14814ff45bb4f8e9accd0b45cc1f3bcf4fd9d32f

commit 14814ff45bb4f8e9accd0b45cc1f3bcf4fd9d32f
Author:     Eugene Grosbein <eugen@FreeBSD.org>
AuthorDate: 2023-01-03 08:53:47 +0000
Commit:     Eugene Grosbein <eugen@FreeBSD.org>
CommitDate: 2023-01-06 04:46:33 +0000

    syslog: fix PID of forking process

    Do not cache PID for a process that does not fabricate it,
    calls openlog() before forking and does not call exec() thereafter.

    PR:             268666
    Fixes:          e9ae9fa93745669b7dd0341d333257ad6cfe8e37
    Tested by:      kp

    (cherry picked from commit 2ce3ef55035093cac7839e71e9ff91f5562ebc29)

 lib/libc/gen/syslog.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Comment 8 Eugene Grosbein freebsd_committer freebsd_triage 2023-01-06 05:16:42 UTC
Thank you very much for the report. The issue is fixed in head and stable branches.