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

Prevent verifying schema version of a file to mmap it #615

Merged
merged 1 commit into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
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
8 changes: 8 additions & 0 deletions src/lavinmq/mfile.cr
Original file line number Diff line number Diff line change
Expand Up @@ -254,4 +254,12 @@ class MFile < IO
@size = new_size.to_i64
@pos = new_size.to_i64 if @pos > new_size
end

# Read from a specific position in the file
# but without mapping the whole file, it uses `pread`
def read_at(pos, bytes)
cnt = LibC.pread(@fd, bytes, bytes.bytesize, pos)
raise IO::Error.from_errno("pread") if cnt == -1
cnt
end
end
12 changes: 11 additions & 1 deletion src/lavinmq/schema.cr
Original file line number Diff line number Diff line change
Expand Up @@ -278,14 +278,24 @@ module LavinMQ
index: 4,
}

def self.verify(file, type) : Int32
def self.verify(file : File, type) : Int32
version = file.read_bytes Int32
if version != VERSIONS[type]
raise OutdatedSchemaVersion.new version, file.path
end
version
end

def self.verify(file : MFile, type) : Int32
buf = uninitialized UInt8[4]
file.read_at(0, buf.to_slice)
version = IO::ByteFormat::SystemEndian.decode(Int32, buf.to_slice)
if version != VERSIONS[type]
raise OutdatedSchemaVersion.new version, file.path
end
version
end

def self.prefix(file, type) : Int32
version = VERSIONS[type]
file.write_bytes version
Expand Down
Loading