πŸ’₯ LET’S TRIGGER SOME KERNEL TEARS


🧠 03_read_closed_fd_trap

πŸ’£ read() From a Closed FD

What happens if you try to read from an FD you already closed?
This vault entry shows how the kernel responds β€” and how you can catch and analyze it.


πŸ“„ 03_read_closed_fd_trap.c (Simple, sharp, single-file trap)

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
 
int	main(void)
{
	int		fd;
	ssize_t	rd;
	char	buf[32];
 
	fd = open("/dev/urandom", O_RDONLY);
	if (fd == -1)
	{
		perror("open");
		exit(EXIT_FAILURE);
	}
	printf("βœ… Opened FD %d\n", fd);
 
	// Close it deliberately
	close(fd);
	printf("❌ Closed FD %d\n", fd);
 
	// Try to read from the closed FD
	rd = read(fd, buf, sizeof(buf));
	if (rd == -1)
	{
		printf("πŸ’₯ read() failed as expected!\n");
		printf("errno: %d (%s)\n", errno, strerror(errno));
	}
	else
	{
		printf("⚠️ Unexpectedly read %ld bytes: %.*s\n", rd, (int)rd, buf);
	}
	return (0);
}

πŸ§ͺ Sample Output

βœ… Opened FD 3
❌ Closed FD 3
πŸ’₯ read() failed as expected!
errno: 9 (Bad file descriptor)

🧠 Why This Matters

ConceptInsight
FD LifetimeFDs are valid only while open. Use-after-close = undefined
errno 9Standard error for invalid FD: EBADF
Trap PointCan happen in real life after complex dup2() or exec() cleanup
DebuggingPerfect for scripting runtime FD sanity checks

πŸ“– What You’ll Learn

  • That closing a file descriptor doesn’t erase its number, but invalidates its FD table entry

  • That read() checks validity before syscall runs

  • That reading from an invalid FD does not crash β€” it fails gracefully with errno


🧰 Pro Tips

  • Try to read from 0, 1, 2 after closing them 😈

  • Pipe this into strace:

    strace ./read_closed_fd_trap

βœ… Ready to Run?

gcc 03_read_closed_fd_trap.c -o fdtrap && ./fdtrap

πŸš€ Next Up Options?

  • πŸ”„ [[04_fd_mirror_fanout]] – duplicate a single FD into many and watch how close() affects all

  • 🧼 [[05_fd_exhaustion_test]] – how many FDs can you open before your OS says β€œENOUGH”

  • 🧟 Or: zombie_maker/ to trap unreaped children

Say the word β€” and the syscall abuse continues.
You’re now writing syscall horror stories as a form of study πŸ§ πŸ’€πŸ“–