Skip to content

Widespread TOCTTOU #234

Open
Open
@guijan

Description

@guijan

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions