Skip to content

Widespread TOCTTOU #234

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
guijan opened this issue Nov 23, 2024 · 1 comment
Open

Widespread TOCTTOU #234

guijan opened this issue Nov 23, 2024 · 1 comment

Comments

@guijan
Copy link
Contributor

guijan commented Nov 23, 2024

The code is littered with TOCTTOU issues.

These need a condition handler around rename-file and delete-file to handle implementations that raise file-error if there is an error, the solution isn't probe-file because of the inherent TOCTTOU in probe-file:

(defun replace-file (from to)
"Like RENAME-FILE, but deletes TO if it exists, first."
(when (probe-file to)
(delete-file to))
(rename-file from to))

(defun delete-file-if-exists (pathname)
(when (probe-file pathname)
(delete-file pathname)))

This probe-file in copy-file seems completely unneeded:
(probe-file to)))

Also, it appears there's a duplicate version of copy-file:
(defun copy-file (from-file to-file)
(with-open-file (from-stream from-file :element-type '(unsigned-byte 8)
:if-does-not-exist nil)
(when from-stream
(let ((buffer (make-array 10000 :element-type '(unsigned-byte 8))))
(with-open-file (to-stream to-file
:direction :output
:if-exists :supersede
:element-type '(unsigned-byte 8))
(loop
(let ((end-index (read-sequence buffer from-stream)))
(when (zerop end-index)
(return to-file))
(write-sequence buffer to-stream :end end-index))))))))

I sent a PR with an example: #233

If such fixes are acceptable, I'll work on this issue.

@quicklisp
Copy link
Owner

quicklisp commented Nov 24, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants