0 branches
Tree Top files
Code
Clone with HTTPS:
56 years ago
..
filelock all: super_batch3 fixes last Apr 13 11.67 KB
README.md os: safer exec([]string) last May 12 1.87 KB
command.c.v all: super_batch6 fixes last Apr 17 2.08 KB
fd.c.v os: gc fix in fd_read last May 10 3.34 KB
file.c.v all: fix more tests last Apr 30 17.3 KB
os.js.v os: safer exec([]string) last May 12 4.81 KB
os.v all: super_batch6 fixes last Apr 17 32.58 KB
os_nix.c.v os: safer exec([]string) last May 12 16.26 KB
os_test.c.v os: safer exec([]string) last May 12 35.66 KB
os_windows.c.v os: windows unsafe fix last May 12 25.73 KB
pipe.c.v all: fix lots of tests last Apr 25 5.11 KB

Description

os provides common OS/platform independent functions for accessing command line arguments, reading/writing files, listing folders, handling processes etc.

On Windows, os.data_dir() uses %LocalAppData% for user-specific application data.

Running commands

Use os.exec(['program', 'arg 1', 'arg 2']) when the command and its arguments are already separate values. It runs the program directly and does not invoke a shell, so spaces and shell metacharacters inside arguments are passed literally.

Use os.execute('command string') only when shell syntax is intended.


Security advice related to TOCTOU attacks

A few os module functions can lead to the TOCTOU vulnerability if used incorrectly. TOCTOU (Time-of-Check-to-Time-of-Use problem) can occur when a file, folder or similar is checked for certain specifications (e.g. read, write permissions) and a change is made afterwards. In the time between the initial check and the edit, an attacker can then cause damage. The following example shows an attack strategy on the left and an improved variant on the right so that TOCTOU is no longer possible.

Example Hint: os.create() opens a file in write-only mode

if os.is_writable("file") {
    // time to make a quick attack
    // (e.g. symlink /etc/passwd to `file`)

    mut f := os.create('path/to/file')!
    // do something with file
    f.close()
}
mut f := os.create('path/to/file') or {
    println("file not writable")
}

// file is locked
// do something with file

f.close()

Proven affected functions The following functions should be used with care and only when used correctly.