Commit 9c202f7
committed
posix: shm: fix fd leak on error paths and protect shm_list with mutex
In subsys/portability/posix/options/shm.c, shm_open() reserves a file
descriptor using zvfs_reserve_fd() before checking whether the target
shm object exists or is unlinked.
On error conditions where the shared memory object does not exist (when
O_CREAT is omitted) or where the object has already been unlinked,
shm_open() returned -1 with errno set to ENOENT or EACCES without
calling zvfs_free_fd(fd). This caused a permanent file descriptor leak
in the global ZVFS descriptor table (CONFIG_ZVFS_OPEN_MAX).
Additionally, the global doubly-linked list shm_list and object
reference counters were manipulated without synchronization across
concurrent shm_open(), shm_unlink(), and shm_close() calls.
Fix both issues by:
1. Releasing reserved file descriptors via zvfs_free_fd(fd) on all error
return paths in shm_open().
2. Protecting shm_list traversal, insertion, removal, and reference
counting with a dedicated mutex (shm_lock).
3. Adding a test in xsi_realtime verifying that repeated failed open
attempts do not leak file descriptors.
Signed-off-by: harshit kudhial <harshitkudhial@gmail.com>1 parent e64f6be commit 9c202f7
2 files changed
Lines changed: 45 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
25 | 25 | | |
26 | 26 | | |
27 | 27 | | |
| 28 | + | |
28 | 29 | | |
29 | 30 | | |
30 | 31 | | |
| |||
239 | 240 | | |
240 | 241 | | |
241 | 242 | | |
| 243 | + | |
242 | 244 | | |
243 | 245 | | |
244 | 246 | | |
245 | 247 | | |
| 248 | + | |
246 | 249 | | |
247 | 250 | | |
248 | 251 | | |
| |||
336 | 339 | | |
337 | 340 | | |
338 | 341 | | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
339 | 345 | | |
340 | 346 | | |
341 | 347 | | |
| 348 | + | |
| 349 | + | |
342 | 350 | | |
343 | 351 | | |
344 | 352 | | |
345 | 353 | | |
346 | 354 | | |
347 | 355 | | |
| 356 | + | |
348 | 357 | | |
349 | 358 | | |
350 | 359 | | |
| |||
353 | 362 | | |
354 | 363 | | |
355 | 364 | | |
| 365 | + | |
356 | 366 | | |
357 | 367 | | |
358 | 368 | | |
| |||
362 | 372 | | |
363 | 373 | | |
364 | 374 | | |
| 375 | + | |
| 376 | + | |
365 | 377 | | |
366 | 378 | | |
367 | 379 | | |
368 | 380 | | |
369 | 381 | | |
| 382 | + | |
| 383 | + | |
370 | 384 | | |
371 | 385 | | |
372 | 386 | | |
| |||
384 | 398 | | |
385 | 399 | | |
386 | 400 | | |
| 401 | + | |
| 402 | + | |
| 403 | + | |
387 | 404 | | |
388 | 405 | | |
| 406 | + | |
389 | 407 | | |
390 | 408 | | |
391 | 409 | | |
| |||
395 | 413 | | |
396 | 414 | | |
397 | 415 | | |
| 416 | + | |
| 417 | + | |
398 | 418 | | |
399 | 419 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
200 | 200 | | |
201 | 201 | | |
202 | 202 | | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
0 commit comments