Hacktoberfest 2026:メンテナが10月に向けて印を付けた、オープンで初心者向けの issue。 Hacktoberfest の issue を見る

fork() succeeds with a descriptor missing when its pin cannot be allocated

オープン 初心者向け
#414 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 2 日以内に返信

まだ誰も着手していません。

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
75/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
c

調査の方向性

まず src/runtime/fork-state.c の fork_ipc_send_fd_table() から始め、fd_lifetime_pin_locked() の NULL 返りをどのように処理するかを確認してください。次に src/syscall/fdtable.c の fd_lifetime_pin_spare() を確認し、割り当て失敗のケースを理解してください。修正により、ピンの割り当てができない場合に fork() が ENOMEM 付きで -1 を返すようにする必要があり、説明されているフォールトインジェクションの再現手順で検証できます。

索引モデルが issue の本文から書いたものです。

説明

bug

fork_ipc_send_fd_table() (src/runtime/fork-state.c) skips a slot when fd_lifetime_pin_locked() returns NULL:

pin = fd_lifetime_pin_locked(i);
if (!pin)
    continue;

fd_lifetime_pin_spare() returns NULL for a closed slot, for a slot with no host descriptor, and when malloc fails. The loop has already skipped FD_CLOSED under the same fd_lock hold, so on an open slot with a host descriptor NULL means the allocation failed. The slot is then left out of the table sent to the child, the send completes, and the parent's fork() returns the child pid. Nothing is logged.

Linux fork() either gives the child every descriptor or fails with ENOMEM.

Reproduction

The allocation cannot be made to fail from the guest, so this uses fault injection on b57f367. In fd_lifetime_pin_spare() (src/syscall/fdtable.c):

-            lifetime = malloc(sizeof(*lifetime));
+            lifetime = (fd == 5 && getenv("PINFAIL"))
+                           ? NULL
+                           : malloc(sizeof(*lifetime));

Guest:

for (int i = 0; i < 5; i++)
    open("/dev/null", O_RDONLY); /* fds 3..7 */
pid_t pid = fork();
printf("[%s] fork returned %d\n", pid ? "parent" : "child", (int) pid);
if (pid == 0) {
    for (int fd = 3; fd <= 7; fd++)
        printf("child fd %d: %s\n", fd,
               fcntl(fd, F_GETFD) >= 0 ? "open" : strerror(errno));
    _exit(0);
}
waitpid(pid, NULL, 0);

With PINFAIL=1:

[parent] fork returned 2
[child] fork returned 0
child fd 3: open
child fd 4: open
child fd 5: Bad file descriptor
child fd 6: open
child fd 7: open

Without the variable, fd 5 is open in the child.

Expected

fork() returns -1 with ENOMEM and no child is left running.

The skip is older than the pin. At 628181b, where this loop still calls dup(), a guest holding 1021 descriptors under ulimit -n 1280 gets a successful fork() and a child with descriptors missing.

主要言語
C
スター
271
フォーク
28
平均マージ
2日 18時間
マージ済み PR(30日)
19

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

sysprog21/elfuse のほかの issue

sysprog21/elfuse の issue をすべて見る

似ている issue

C の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。