Skip to content
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

fixes TOCTOU bug in bap log #937

Merged
merged 4 commits into from
Apr 2, 2019
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions lib/bap/bap_log.ml
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,26 @@ let print_message ppf msg =
| Error -> eprintf "%s@\n%!" msg.message
| _ -> ()

let lock log_folder =
let lock = log_folder / "lock" in
let lock = Unix.openfile lock Unix.[O_RDWR; O_CREAT] 0o666 in
Unix.lockf lock Unix.F_LOCK 0;
lock

let unlock lock =
Unix.lockf lock Unix.F_ULOCK 0;
Unix.close lock

let open_log_channel user_dir =
try
let log_folder = log_folder user_dir in
mkdir log_folder;
let file = log_folder / "log" in
if Sys.file_exists file
then rotate max_logs file;
let lock = lock log_folder in
ivg marked this conversation as resolved.
Show resolved Hide resolved
protect ~f:(fun () ->
mkdir log_folder;
if Sys.file_exists file
then rotate max_logs file)
~finally:(fun () -> unlock lock);
let ch = Out_channel.create file in
formatter_of_out_channel ch
with exn ->
Expand Down