Bug 206396 - Crash while concurrent POSIX semaphore access
Summary: Crash while concurrent POSIX semaphore access
Status: Closed FIXED
Alias: None
Product: Base System
Classification: Unclassified
Component: kern (show other bugs)
Version: 10.2-STABLE
Hardware: Any Any
: --- Affects Only Me
Assignee: Jilles Tjoelker
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2016-01-19 10:57 UTC by Alexander Saprykin
Modified: 2016-01-27 22:57 UTC (History)
1 user (show)

See Also:
jilles: mfc-stable10+
jilles: mfc-stable9-


Attachments
Test code (5.33 KB, text/x-c)
2016-01-19 10:57 UTC, Alexander Saprykin
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Alexander Saprykin 2016-01-19 10:57:49 UTC
Created attachment 165809 [details]
Test code

I have faced a strange issue with POSIX semaphores. Program crashes when several threads concurrently access POSIX semaphore under the same name. I attached a test code to illustrate it. I have tried to compile this code using Clang and GCC but it was the same result. If you add a pause between threads creation then it executes fine.
The same code works fine on Linux and latest DragonFlyBSD.
Comment 1 commit-hook freebsd_committer freebsd_triage 2016-01-22 14:53:07 UTC
A commit references this bug:

Author: jilles
Date: Fri Jan 22 14:52:31 UTC 2016
New revision: 294565
URL: https://svnweb.freebsd.org/changeset/base/294565

Log:
  sem: Don't free nameinfo that is still in list when open() fails.

  This bug could be reproduced easily by calling sem_open() with O_CREAT |
  O_EXCL on a semaphore that is already open in the process. The struct
  sem_nameinfo would be freed while still in sem_list and later calls to
  sem_open() or sem_close() could access freed memory.

  PR:		206396
  MFC after:	5 days

Changes:
  head/lib/libc/gen/sem_new.c
  head/tools/regression/posixsem2/semtest.c
Comment 2 Jilles Tjoelker freebsd_committer freebsd_triage 2016-01-22 14:55:44 UTC
Not amd64-specific.
Comment 3 commit-hook freebsd_committer freebsd_triage 2016-01-27 22:56:23 UTC
A commit references this bug:

Author: jilles
Date: Wed Jan 27 22:56:05 UTC 2016
New revision: 294963
URL: https://svnweb.freebsd.org/changeset/base/294963

Log:
  MFC r294565: sem: Don't free nameinfo that is still in list when open()
  fails.

  This bug could be reproduced easily by calling sem_open() with O_CREAT |
  O_EXCL on a semaphore that is already open in the process. The struct
  sem_nameinfo would be freed while still in sem_list and later calls to
  sem_open() or sem_close() could access freed memory.

  PR:		206396

Changes:
_U  stable/10/
  stable/10/lib/libc/gen/sem_new.c
  stable/10/tools/regression/posixsem2/semtest.c
Comment 4 Jilles Tjoelker freebsd_committer freebsd_triage 2016-01-27 22:57:18 UTC
Fixed in head and stable/10. Thanks for the report.