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 all 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
23 changes: 20 additions & 3 deletions lib/bap/bap_log.ml
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,30 @@ let print_message ppf msg =
| Error -> eprintf "%s@\n%!" msg.message
| _ -> ()

let lock_filename logdir =
let digest = Md5.digest_string logdir in
let name = sprintf "bap-%s.lock" (Md5.to_hex digest) in
Filename.get_temp_dir_name () / name

let lock file =
let lock = Unix.openfile file 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 (lock_filename log_folder) in
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