Skip to content

Commit

Permalink
Harden SQLite permissions (#12096)
Browse files Browse the repository at this point in the history
  • Loading branch information
xacrimon committed May 3, 2022
1 parent d906486 commit 59c11d8
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions lib/backend/lite/lite.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"context"
"database/sql"
"database/sql/driver"
"errors"
"net/url"
"os"
"path/filepath"
Expand Down Expand Up @@ -56,7 +57,10 @@ const (
const (
// defaultDirMode is the mode of the newly created directories that are part
// of the Path
defaultDirMode os.FileMode = 0770
defaultDirMode os.FileMode = 0700

// dbMode is the mode set on sqlite database files
dbMode os.FileMode = 0600

// defaultDBFile is the file name of the sqlite db in the directory
// specified by Path
Expand Down Expand Up @@ -197,15 +201,32 @@ func NewWithConfig(ctx context.Context, cfg Config) (*Backend, error) {
return nil, trace.Wrap(err)
}
connectionURI := cfg.ConnectionURI()
path := filepath.Join(cfg.Path, defaultDBFile)
// Ensure that the path to the root directory exists.
err := os.MkdirAll(cfg.Path, defaultDirMode)
err := os.MkdirAll(cfg.Path, os.ModeDir|defaultDirMode)
if err != nil {
return nil, trace.ConvertSystemError(err)
}

setPermissions := false
if _, err := os.Stat(path); errors.Is(err, os.ErrNotExist) {
setPermissions = true
}

db, err := sql.Open("sqlite3", cfg.ConnectionURI())
if err != nil {
return nil, trace.Wrap(err, "error opening URI: %v", connectionURI)
}

if setPermissions {
// Ensure the database has restrictive access permissions.
db.PingContext(ctx)
err = os.Chmod(path, dbMode)
if err != nil {
return nil, trace.ConvertSystemError(err)
}
}

// serialize access to sqlite, as we're using immediate transactions anyway,
// and in-memory go locks are faster than sqlite locks
db.SetMaxOpenConns(1)
Expand Down

0 comments on commit 59c11d8

Please sign in to comment.