reverse: generate file header for Read()
Also create virtual gocryptfs.diriv entries (no content yet).
This commit is contained in:
parent
ac1221395e
commit
10f38e8870
@ -1,6 +1,7 @@
|
|||||||
package fusefrontend_reverse
|
package fusefrontend_reverse
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
@ -12,10 +13,12 @@ import (
|
|||||||
"github.com/rfjakob/gocryptfs/internal/tlog"
|
"github.com/rfjakob/gocryptfs/internal/tlog"
|
||||||
)
|
)
|
||||||
|
|
||||||
var zeroFileId []byte
|
// File header that contains an all-zero File ID
|
||||||
|
var zeroFileHeader *contentenc.FileHeader
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
zeroFileId = make([]byte, 16)
|
zeroFileHeader = contentenc.RandomHeader()
|
||||||
|
zeroFileHeader.Id = make([]byte, contentenc.HEADER_ID_LEN)
|
||||||
}
|
}
|
||||||
|
|
||||||
type reverseFile struct {
|
type reverseFile struct {
|
||||||
@ -41,12 +44,12 @@ func (rf *reverseFile) GetAttr(*fuse.Attr) fuse.Status {
|
|||||||
return fuse.ENOSYS
|
return fuse.ENOSYS
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read - FUSE call
|
// readFile - read from the backing plaintext file, encrypt it, return the
|
||||||
func (rf *reverseFile) Read(buf []byte, off int64) (resultData fuse.ReadResult, status fuse.Status) {
|
// ciphertext.
|
||||||
// TODO prefix file header
|
// "off" ... ciphertext offset (must be >= HEADER_LEN)
|
||||||
|
// "length" ... ciphertext length
|
||||||
length := uint64(len(buf))
|
func (rf *reverseFile) readFile(off uint64, length uint64) (out []byte, err error) {
|
||||||
blocks := rf.contentEnc.ExplodeCipherRange(uint64(off), length)
|
blocks := rf.contentEnc.ExplodeCipherRange(off, length)
|
||||||
|
|
||||||
// Read the backing plaintext in one go
|
// Read the backing plaintext in one go
|
||||||
alignedOffset, alignedLength := contentenc.JointPlaintextRange(blocks)
|
alignedOffset, alignedLength := contentenc.JointPlaintextRange(blocks)
|
||||||
@ -54,26 +57,62 @@ func (rf *reverseFile) Read(buf []byte, off int64) (resultData fuse.ReadResult,
|
|||||||
plaintext := make([]byte, int(alignedLength))
|
plaintext := make([]byte, int(alignedLength))
|
||||||
n, err := rf.fd.ReadAt(plaintext, int64(alignedOffset))
|
n, err := rf.fd.ReadAt(plaintext, int64(alignedOffset))
|
||||||
if err != nil && err != io.EOF {
|
if err != nil && err != io.EOF {
|
||||||
tlog.Warn.Printf("reverseFile.Read: ReadAt: %s", err.Error())
|
tlog.Warn.Printf("reverseFile.readFile: ReadAt: %s", err.Error())
|
||||||
return nil, fuse.ToStatus(err)
|
return nil, err
|
||||||
}
|
}
|
||||||
// Truncate buffer down to actually read bytes
|
// Truncate buffer down to actually read bytes
|
||||||
plaintext = plaintext[0:n]
|
plaintext = plaintext[0:n]
|
||||||
|
|
||||||
// Encrypt blocks
|
// Encrypt blocks
|
||||||
ciphertext := rf.contentEnc.EncryptBlocks(plaintext, blocks[0].BlockNo, zeroFileId)
|
ciphertext := rf.contentEnc.EncryptBlocks(plaintext, blocks[0].BlockNo, zeroFileHeader.Id)
|
||||||
|
|
||||||
// Crop down to the relevant part
|
// Crop down to the relevant part
|
||||||
var out []byte
|
|
||||||
lenHave := len(ciphertext)
|
lenHave := len(ciphertext)
|
||||||
skip := blocks[0].Skip
|
skip := blocks[0].Skip
|
||||||
endWant := int(skip + length)
|
endWant := int(skip + length)
|
||||||
if lenHave > endWant {
|
if lenHave > endWant {
|
||||||
out = plaintext[skip:endWant]
|
out = ciphertext[skip:endWant]
|
||||||
} else if lenHave > int(skip) {
|
} else if lenHave > int(skip) {
|
||||||
out = plaintext[skip:lenHave]
|
out = ciphertext[skip:lenHave]
|
||||||
}
|
} // else: out stays empty, file was smaller than the requested offset
|
||||||
// else: out stays empty, file was smaller than the requested offset
|
|
||||||
|
|
||||||
return fuse.ReadResultData(out), fuse.OK
|
return out, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read - FUSE call
|
||||||
|
func (rf *reverseFile) Read(buf []byte, ioff int64) (resultData fuse.ReadResult, status fuse.Status) {
|
||||||
|
length := uint64(len(buf))
|
||||||
|
off := uint64(ioff)
|
||||||
|
var out bytes.Buffer
|
||||||
|
var headerPart []byte
|
||||||
|
|
||||||
|
// Create a virtual file header
|
||||||
|
if off < contentenc.HEADER_LEN {
|
||||||
|
headerPart = zeroFileHeader.Pack()
|
||||||
|
headerPart = headerPart[off:]
|
||||||
|
if off+length < contentenc.HEADER_LEN {
|
||||||
|
headerPart = headerPart[:length]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
out.Write(headerPart)
|
||||||
|
hLen := uint64(len(headerPart))
|
||||||
|
off += hLen
|
||||||
|
length -= hLen
|
||||||
|
|
||||||
|
// Read actual file data
|
||||||
|
if length > 0 {
|
||||||
|
fileData, err := rf.readFile(off, length)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fuse.ToStatus(err)
|
||||||
|
}
|
||||||
|
if len(fileData) == 0 {
|
||||||
|
// If we could not read any actual data, we also don't want to
|
||||||
|
// return the file header. An empty file stays empty in encrypted
|
||||||
|
// form.
|
||||||
|
return nil, fuse.OK
|
||||||
|
}
|
||||||
|
out.Write(fileData)
|
||||||
|
}
|
||||||
|
|
||||||
|
return fuse.ReadResultData(out.Bytes()), fuse.OK
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,10 @@
|
|||||||
package fusefrontend_reverse
|
package fusefrontend_reverse
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"path"
|
||||||
|
"strings"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
|
||||||
"github.com/hanwen/go-fuse/fuse"
|
"github.com/hanwen/go-fuse/fuse"
|
||||||
@ -14,6 +17,10 @@ import (
|
|||||||
"github.com/rfjakob/gocryptfs/internal/nametransform"
|
"github.com/rfjakob/gocryptfs/internal/nametransform"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
DirIVMode = syscall.S_IFREG | 0400
|
||||||
|
)
|
||||||
|
|
||||||
type reverseFS struct {
|
type reverseFS struct {
|
||||||
// Embed pathfs.defaultFileSystem for a ENOSYS implementation of all methods
|
// Embed pathfs.defaultFileSystem for a ENOSYS implementation of all methods
|
||||||
pathfs.FileSystem
|
pathfs.FileSystem
|
||||||
@ -44,6 +51,46 @@ func NewFS(args fusefrontend.Args) *reverseFS {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (rfs *reverseFS) GetAttr(relPath string, context *fuse.Context) (*fuse.Attr, fuse.Status) {
|
func (rfs *reverseFS) GetAttr(relPath string, context *fuse.Context) (*fuse.Attr, fuse.Status) {
|
||||||
|
// Handle gocryptfs.diriv
|
||||||
|
if relPath == nametransform.DirIVFilename ||
|
||||||
|
strings.HasSuffix(relPath, nametransform.DirIVFilename) {
|
||||||
|
|
||||||
|
fmt.Printf("Handling gocryptfs.diriv\n")
|
||||||
|
|
||||||
|
cDir := path.Dir(relPath)
|
||||||
|
if cDir == "." {
|
||||||
|
cDir = ""
|
||||||
|
}
|
||||||
|
dir, err := rfs.decryptPath(cDir)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("decrypt err %q\n", cDir)
|
||||||
|
return nil, fuse.ToStatus(err)
|
||||||
|
}
|
||||||
|
// Does the parent dir exist?
|
||||||
|
a, status := rfs.loopbackfs.GetAttr(dir, context)
|
||||||
|
if !status.Ok() {
|
||||||
|
fmt.Printf("missing parent\n")
|
||||||
|
return nil, status
|
||||||
|
}
|
||||||
|
// Is it a dir at all?
|
||||||
|
if !a.IsDir() {
|
||||||
|
fmt.Printf("not isdir\n")
|
||||||
|
return nil, fuse.ENOTDIR
|
||||||
|
}
|
||||||
|
// Does the user have execute permissions?
|
||||||
|
if a.Mode&syscall.S_IXUSR == 0 {
|
||||||
|
fmt.Printf("not exec")
|
||||||
|
return nil, fuse.EPERM
|
||||||
|
}
|
||||||
|
// All good. Let's fake the file.
|
||||||
|
// We use the inode number of the parent dir (can this cause problems?).
|
||||||
|
a.Mode = DirIVMode
|
||||||
|
a.Size = nametransform.DirIVLen
|
||||||
|
a.Nlink = 1
|
||||||
|
|
||||||
|
return a, fuse.OK
|
||||||
|
}
|
||||||
|
|
||||||
if rfs.isFiltered(relPath) {
|
if rfs.isFiltered(relPath) {
|
||||||
return nil, fuse.EPERM
|
return nil, fuse.EPERM
|
||||||
}
|
}
|
||||||
@ -52,8 +99,8 @@ func (rfs *reverseFS) GetAttr(relPath string, context *fuse.Context) (*fuse.Attr
|
|||||||
return nil, fuse.ToStatus(err)
|
return nil, fuse.ToStatus(err)
|
||||||
}
|
}
|
||||||
a, status := rfs.loopbackfs.GetAttr(relPath, context)
|
a, status := rfs.loopbackfs.GetAttr(relPath, context)
|
||||||
if a == nil {
|
if !status.Ok() {
|
||||||
return a, status
|
return nil, status
|
||||||
}
|
}
|
||||||
// Calculate encrypted file size
|
// Calculate encrypted file size
|
||||||
if a.IsRegular() {
|
if a.IsRegular() {
|
||||||
@ -105,5 +152,8 @@ func (rfs *reverseFS) OpenDir(relPath string, context *fuse.Context) ([]fuse.Dir
|
|||||||
return nil, fuse.ToStatus(err)
|
return nil, fuse.ToStatus(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// Add virtual gocryptfs.diriv
|
||||||
|
entries = append(entries, fuse.DirEntry{syscall.S_IFREG | 0400, nametransform.DirIVFilename})
|
||||||
|
|
||||||
return entries, fuse.OK
|
return entries, fuse.OK
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user