crystal icon indicating copy to clipboard operation
crystal copied to clipboard

`Dir.glob("/proc/*/map_files")` raises `File::AccessDeniedError`

Open OakAtsume opened this issue 5 months ago • 8 comments

Summary

Calling Dir.glob("/**/*") can cause an unhandled crash when encountering an unreadable directory such as /proc/*/map_files; This makes Dir.glob inherently unsafe for any general-purpose file-tree-traversal on Unix-like systems. Specially when it faces directories like /proc, /sys, /dev.

I've noticed that other languages like PHP, Python3, and Ruby handle this by simply skipping that entry or creating a way for the user to handle that error, but crystal doesn't seem to have a way to do so.

Steps to reproduce

# Use follow_symlinks: false to avoid getting stuck in a loop
Dir.glob("/**/*", follow_symlinks: false) do |path|
  puts path
end

Expected crash log:

Unhandled exception: Error reading directory entries: '/proc/1319/map_files': Permission denied (File::AccessDeniedError)
  from /usr/share/crystal/src/crystal/system/unix/dir.cr:34:7 in 'next_entry'
  from /usr/share/crystal/src/dir/glob.cr:416:7 in 'read_entry'
  from /usr/share/crystal/src/dir/glob.cr:336:24 in '__crystal_main'
  from /usr/share/crystal/src/crystal/main.cr:129:5 in 'main_user_code'
  from /usr/share/crystal/src/crystal/main.cr:115:7 in 'main'
  from /usr/share/crystal/src/crystal/system/unix/main.cr:9:3 in 'main'
  from /lib64/libc.so.6 in '??'
  from /lib64/libc.so.6 in '__libc_start_main'
  ....

Current Environment:

Crystal 1.16.1 [d2369ac2e] (2025-04-16)
LLVM: 18.1.8
Default target: x86_64-unknown-linux-gnu
Arch Linux 6.14.6-arch1-1

OakAtsume avatar May 13 '25 17:05 OakAtsume