2015-09-08 00:54:24 +02:00
|
|
|
package pathfs_frontend
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
2015-10-04 14:36:20 +02:00
|
|
|
"io"
|
2015-09-08 00:54:24 +02:00
|
|
|
"os"
|
|
|
|
"sync"
|
|
|
|
"syscall"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hanwen/go-fuse/fuse"
|
|
|
|
"github.com/hanwen/go-fuse/fuse/nodefs"
|
|
|
|
"github.com/rfjakob/gocryptfs/cryptfs"
|
|
|
|
)
|
|
|
|
|
|
|
|
// File - based on loopbackFile in go-fuse/fuse/nodefs/files.go
|
|
|
|
type file struct {
|
|
|
|
fd *os.File
|
|
|
|
|
|
|
|
// os.File is not threadsafe. Although fd themselves are
|
|
|
|
// constant during the lifetime of an open file, the OS may
|
|
|
|
// reuse the fd number after it is closed. When open races
|
|
|
|
// with another close, they may lead to confusion as which
|
|
|
|
// file gets written in the end.
|
|
|
|
lock sync.Mutex
|
|
|
|
|
|
|
|
// Was the file opened O_WRONLY?
|
|
|
|
writeOnly bool
|
|
|
|
|
|
|
|
// Parent CryptFS
|
|
|
|
cfs *cryptfs.CryptFS
|
2015-10-03 13:36:49 +02:00
|
|
|
|
|
|
|
// Inode number
|
|
|
|
ino uint64
|
2015-09-08 00:54:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewFile(fd *os.File, writeOnly bool, cfs *cryptfs.CryptFS) nodefs.File {
|
2015-10-03 13:36:49 +02:00
|
|
|
var st syscall.Stat_t
|
|
|
|
syscall.Fstat(int(fd.Fd()), &st)
|
|
|
|
|
2015-09-08 00:54:24 +02:00
|
|
|
return &file{
|
2015-10-04 14:36:20 +02:00
|
|
|
fd: fd,
|
2015-09-08 00:54:24 +02:00
|
|
|
writeOnly: writeOnly,
|
2015-10-04 14:36:20 +02:00
|
|
|
cfs: cfs,
|
|
|
|
ino: st.Ino,
|
2015-09-08 00:54:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *file) InnerFile() nodefs.File {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *file) SetInode(n *nodefs.Inode) {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *file) String() string {
|
|
|
|
return fmt.Sprintf("cryptFile(%s)", f.fd.Name())
|
|
|
|
}
|
|
|
|
|
2015-10-03 13:36:49 +02:00
|
|
|
// doRead - returns "length" plaintext bytes from plaintext offset "off".
|
|
|
|
// Arguments "length" and "off" do not have to be aligned.
|
2015-09-30 22:36:53 +02:00
|
|
|
//
|
2015-10-03 13:36:49 +02:00
|
|
|
// doRead reads the corresponding ciphertext blocks from disk, decryptfs them and
|
|
|
|
// returns the requested part of the plaintext.
|
|
|
|
//
|
|
|
|
// Called by Read() and by Write() and Truncate() for RMW
|
2015-09-08 21:35:06 +02:00
|
|
|
func (f *file) doRead(off uint64, length uint64) ([]byte, fuse.Status) {
|
2015-09-08 00:54:24 +02:00
|
|
|
|
|
|
|
// Read the backing ciphertext in one go
|
2015-09-08 21:35:06 +02:00
|
|
|
alignedOffset, alignedLength, skip := f.cfs.CiphertextRange(off, length)
|
2015-09-08 22:34:23 +02:00
|
|
|
cryptfs.Debug.Printf("CiphertextRange(%d, %d) -> %d, %d, %d\n", off, length, alignedOffset, alignedLength, skip)
|
2015-09-08 00:54:24 +02:00
|
|
|
ciphertext := make([]byte, int(alignedLength))
|
2015-09-09 19:32:59 +02:00
|
|
|
f.lock.Lock()
|
2015-09-08 21:35:06 +02:00
|
|
|
n, err := f.fd.ReadAt(ciphertext, int64(alignedOffset))
|
2015-09-09 19:32:59 +02:00
|
|
|
f.lock.Unlock()
|
2015-09-08 00:54:24 +02:00
|
|
|
if err != nil && err != io.EOF {
|
2015-09-08 21:35:06 +02:00
|
|
|
cryptfs.Warn.Printf("read: ReadAt: %s\n", err.Error())
|
2015-09-08 00:54:24 +02:00
|
|
|
return nil, fuse.ToStatus(err)
|
|
|
|
}
|
2015-10-03 13:36:49 +02:00
|
|
|
// Truncate ciphertext buffer down to actually read bytes
|
|
|
|
ciphertext = ciphertext[0:n]
|
2015-10-06 22:27:37 +02:00
|
|
|
|
|
|
|
blockNo := alignedOffset / f.cfs.CipherBS()
|
|
|
|
cryptfs.Debug.Printf("ReadAt offset=%d bytes (%d blocks), want=%d, got=%d\n", alignedOffset, blockNo, alignedLength, n)
|
2015-09-08 00:54:24 +02:00
|
|
|
|
|
|
|
// Decrypt it
|
2015-10-06 22:27:37 +02:00
|
|
|
plaintext, err := f.cfs.DecryptBlocks(ciphertext, blockNo)
|
2015-09-08 00:54:24 +02:00
|
|
|
if err != nil {
|
2015-10-03 13:36:49 +02:00
|
|
|
blockNo := (alignedOffset + uint64(len(plaintext))) / f.cfs.PlainBS()
|
|
|
|
cipherOff := blockNo * f.cfs.CipherBS()
|
|
|
|
plainOff := blockNo * f.cfs.PlainBS()
|
|
|
|
cryptfs.Warn.Printf("ino%d: doRead: corrupt block #%d (plainOff=%d/%d, cipherOff=%d/%d)\n",
|
|
|
|
f.ino, blockNo, plainOff, f.cfs.PlainBS(), cipherOff, f.cfs.CipherBS())
|
2015-09-08 00:54:24 +02:00
|
|
|
return nil, fuse.EIO
|
|
|
|
}
|
|
|
|
|
|
|
|
// Crop down to the relevant part
|
|
|
|
var out []byte
|
|
|
|
lenHave := len(plaintext)
|
2015-09-08 21:35:06 +02:00
|
|
|
lenWant := skip + int(length)
|
2015-09-08 00:54:24 +02:00
|
|
|
if lenHave > lenWant {
|
2015-10-04 14:36:20 +02:00
|
|
|
out = plaintext[skip : skip+int(length)]
|
2015-09-08 00:54:24 +02:00
|
|
|
} else if lenHave > skip {
|
|
|
|
out = plaintext[skip:lenHave]
|
2015-09-08 21:35:06 +02:00
|
|
|
} else {
|
|
|
|
// Out stays empty, file was smaller than the requested offset
|
|
|
|
}
|
|
|
|
|
|
|
|
return out, fuse.OK
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read - FUSE call
|
|
|
|
func (f *file) Read(buf []byte, off int64) (resultData fuse.ReadResult, code fuse.Status) {
|
2015-10-04 11:39:35 +02:00
|
|
|
cryptfs.Debug.Printf("ino%d: FUSE Read: offset=%d length=%d\n", f.ino, len(buf), off)
|
2015-09-08 21:35:06 +02:00
|
|
|
|
|
|
|
if f.writeOnly {
|
2015-10-03 13:36:49 +02:00
|
|
|
cryptfs.Warn.Printf("ino%d: Tried to read from write-only file\n", f.ino)
|
2015-09-08 21:35:06 +02:00
|
|
|
return nil, fuse.EBADF
|
2015-09-08 00:54:24 +02:00
|
|
|
}
|
|
|
|
|
2015-09-08 21:35:06 +02:00
|
|
|
out, status := f.doRead(uint64(off), uint64(len(buf)))
|
2015-09-30 20:32:24 +02:00
|
|
|
|
|
|
|
if status == fuse.EIO {
|
2015-10-03 13:36:49 +02:00
|
|
|
cryptfs.Warn.Printf("ino%d: Read failed with EIO, offset=%d, length=%d\n", f.ino, len(buf), off)
|
2015-09-30 20:32:24 +02:00
|
|
|
}
|
2015-09-08 21:35:06 +02:00
|
|
|
if status != fuse.OK {
|
|
|
|
return nil, status
|
|
|
|
}
|
2015-09-08 00:54:24 +02:00
|
|
|
|
2015-10-03 13:36:49 +02:00
|
|
|
cryptfs.Debug.Printf("ino%d: Read: status %v, returning %d bytes\n", f.ino, status, len(out))
|
2015-09-08 21:35:06 +02:00
|
|
|
return fuse.ReadResultData(out), status
|
2015-09-08 00:54:24 +02:00
|
|
|
}
|
|
|
|
|
2015-10-04 11:39:35 +02:00
|
|
|
// Do the actual write
|
|
|
|
func (f *file) doWrite(data []byte, off int64) (uint32, fuse.Status) {
|
2015-09-08 00:54:24 +02:00
|
|
|
var written uint32
|
2015-09-30 22:36:53 +02:00
|
|
|
status := fuse.OK
|
2015-09-08 00:54:24 +02:00
|
|
|
dataBuf := bytes.NewBuffer(data)
|
|
|
|
blocks := f.cfs.SplitRange(uint64(off), uint64(len(data)))
|
2015-10-04 14:36:20 +02:00
|
|
|
for _, b := range blocks {
|
2015-09-08 00:54:24 +02:00
|
|
|
|
|
|
|
blockData := dataBuf.Next(int(b.Length))
|
|
|
|
|
|
|
|
// Incomplete block -> Read-Modify-Write
|
|
|
|
if b.IsPartial() {
|
|
|
|
// Read
|
|
|
|
o, _ := b.PlaintextRange()
|
2015-09-08 22:34:23 +02:00
|
|
|
oldData, status := f.doRead(o, f.cfs.PlainBS())
|
2015-09-08 00:54:24 +02:00
|
|
|
if status != fuse.OK {
|
2015-09-08 21:35:06 +02:00
|
|
|
cryptfs.Warn.Printf("RMW read failed: %s\n", status.String())
|
2015-09-08 00:54:24 +02:00
|
|
|
return written, status
|
|
|
|
}
|
|
|
|
// Modify
|
2015-10-04 14:24:43 +02:00
|
|
|
blockData = f.cfs.MergeBlocks(oldData, blockData, int(b.Skip))
|
2015-09-08 22:34:42 +02:00
|
|
|
cryptfs.Debug.Printf("len(oldData)=%d len(blockData)=%d\n", len(oldData), len(blockData))
|
2015-09-08 00:54:24 +02:00
|
|
|
}
|
2015-09-09 19:32:59 +02:00
|
|
|
|
2015-09-08 00:54:24 +02:00
|
|
|
// Write
|
|
|
|
blockOffset, _ := b.CiphertextRange()
|
2015-10-06 22:27:37 +02:00
|
|
|
blockData = f.cfs.EncryptBlock(blockData, b.BlockNo)
|
2015-10-03 13:36:49 +02:00
|
|
|
cryptfs.Debug.Printf("ino%d: Writing %d bytes to block #%d, md5=%s\n", f.ino, len(blockData), b.BlockNo, cryptfs.Debug.Md5sum(blockData))
|
|
|
|
if len(blockData) != int(f.cfs.CipherBS()) {
|
2015-10-04 00:26:20 +02:00
|
|
|
cryptfs.Debug.Printf("ino%d: Writing partial block #%d (%d bytes)\n", f.ino, b.BlockNo, len(blockData))
|
2015-10-03 13:36:49 +02:00
|
|
|
}
|
2015-09-09 19:32:59 +02:00
|
|
|
f.lock.Lock()
|
2015-09-08 00:54:24 +02:00
|
|
|
_, err := f.fd.WriteAt(blockData, int64(blockOffset))
|
2015-09-09 19:32:59 +02:00
|
|
|
f.lock.Unlock()
|
2015-09-08 00:54:24 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
cryptfs.Warn.Printf("Write failed: %s\n", err.Error())
|
|
|
|
status = fuse.ToStatus(err)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
written += uint32(b.Length)
|
|
|
|
}
|
|
|
|
return written, status
|
|
|
|
}
|
|
|
|
|
2015-10-04 11:39:35 +02:00
|
|
|
// Write - FUSE call
|
|
|
|
func (f *file) Write(data []byte, off int64) (uint32, fuse.Status) {
|
2015-10-04 14:36:20 +02:00
|
|
|
cryptfs.Debug.Printf("ino%d: FUSE Write: offset=%d length=%d\n", f.ino, off, len(data))
|
2015-10-04 14:21:07 +02:00
|
|
|
|
|
|
|
fi, err := f.fd.Stat()
|
|
|
|
if err != nil {
|
|
|
|
cryptfs.Warn.Printf("Write: Fstat failed: %v\n", err)
|
|
|
|
return 0, fuse.ToStatus(err)
|
|
|
|
}
|
|
|
|
plainSize := f.cfs.PlainSize(uint64(fi.Size()))
|
|
|
|
if f.createsHole(plainSize, off) {
|
|
|
|
status := f.zeroPad(plainSize)
|
|
|
|
if status != fuse.OK {
|
|
|
|
cryptfs.Warn.Printf("zeroPad returned error %v\n", status)
|
|
|
|
return 0, status
|
|
|
|
}
|
|
|
|
}
|
2015-10-04 11:39:35 +02:00
|
|
|
return f.doWrite(data, off)
|
|
|
|
}
|
|
|
|
|
2015-09-08 00:54:24 +02:00
|
|
|
// Release - FUSE call, forget file
|
|
|
|
func (f *file) Release() {
|
|
|
|
f.lock.Lock()
|
|
|
|
f.fd.Close()
|
|
|
|
f.lock.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Flush - FUSE call
|
|
|
|
func (f *file) Flush() fuse.Status {
|
|
|
|
f.lock.Lock()
|
|
|
|
|
|
|
|
// Since Flush() may be called for each dup'd fd, we don't
|
|
|
|
// want to really close the file, we just want to flush. This
|
|
|
|
// is achieved by closing a dup'd fd.
|
|
|
|
newFd, err := syscall.Dup(int(f.fd.Fd()))
|
|
|
|
f.lock.Unlock()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return fuse.ToStatus(err)
|
|
|
|
}
|
|
|
|
err = syscall.Close(newFd)
|
|
|
|
return fuse.ToStatus(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *file) Fsync(flags int) (code fuse.Status) {
|
|
|
|
f.lock.Lock()
|
|
|
|
r := fuse.ToStatus(syscall.Fsync(int(f.fd.Fd())))
|
|
|
|
f.lock.Unlock()
|
|
|
|
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2015-09-30 22:36:53 +02:00
|
|
|
func (f *file) Truncate(newSize uint64) fuse.Status {
|
2015-09-08 00:54:24 +02:00
|
|
|
|
2015-09-30 22:36:53 +02:00
|
|
|
// We need the old file size to determine if we are growing or shrinking
|
|
|
|
// the file
|
|
|
|
fi, err := f.fd.Stat()
|
|
|
|
if err != nil {
|
2015-10-04 14:21:07 +02:00
|
|
|
cryptfs.Warn.Printf("Truncate: Fstat failed: %v\n", err)
|
2015-09-30 22:36:53 +02:00
|
|
|
return fuse.ToStatus(err)
|
|
|
|
}
|
2015-10-04 00:26:20 +02:00
|
|
|
oldSize := f.cfs.PlainSize(uint64(fi.Size()))
|
|
|
|
{
|
2015-10-04 15:43:46 +02:00
|
|
|
oldB := float32(oldSize) / float32(f.cfs.PlainBS())
|
|
|
|
newB := float32(newSize) / float32(f.cfs.PlainBS())
|
|
|
|
cryptfs.Debug.Printf("ino%d: FUSE Truncate from %.2f to %.2f blocks (%d to %d bytes)\n", f.ino, oldB, newB, oldSize, newSize)
|
2015-10-04 00:26:20 +02:00
|
|
|
}
|
2015-10-04 14:21:07 +02:00
|
|
|
|
|
|
|
// File grows
|
2015-09-30 22:36:53 +02:00
|
|
|
if newSize > oldSize {
|
2015-10-04 14:36:20 +02:00
|
|
|
blocks := f.cfs.SplitRange(oldSize, newSize-oldSize)
|
|
|
|
for _, b := range blocks {
|
2015-10-04 14:21:07 +02:00
|
|
|
// First and last block may be partial
|
|
|
|
if b.IsPartial() {
|
|
|
|
off, _ := b.PlaintextRange()
|
2015-10-04 14:24:43 +02:00
|
|
|
off += b.Skip
|
2015-10-04 14:21:07 +02:00
|
|
|
_, status := f.doWrite(make([]byte, b.Length), int64(off))
|
|
|
|
if status != fuse.OK {
|
|
|
|
return status
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
off, length := b.CiphertextRange()
|
|
|
|
f.lock.Lock()
|
2015-10-04 14:36:20 +02:00
|
|
|
err := syscall.Ftruncate(int(f.fd.Fd()), int64(off+length))
|
2015-10-04 14:21:07 +02:00
|
|
|
f.lock.Unlock()
|
|
|
|
if err != nil {
|
|
|
|
cryptfs.Warn.Printf("grow Ftruncate returned error: %v", err)
|
|
|
|
return fuse.ToStatus(err)
|
|
|
|
}
|
|
|
|
}
|
2015-09-30 22:36:53 +02:00
|
|
|
}
|
2015-10-04 14:21:07 +02:00
|
|
|
return fuse.OK
|
|
|
|
} else {
|
2015-10-04 16:04:25 +02:00
|
|
|
// File shrinks
|
2015-10-04 14:21:07 +02:00
|
|
|
blockNo := f.cfs.BlockNoPlainOff(newSize)
|
2015-10-04 16:04:25 +02:00
|
|
|
cipherOff := blockNo * f.cfs.CipherBS()
|
|
|
|
plainOff := blockNo * f.cfs.PlainBS()
|
|
|
|
lastBlockLen := newSize - plainOff
|
2015-10-04 14:21:07 +02:00
|
|
|
var data []byte
|
|
|
|
if lastBlockLen > 0 {
|
|
|
|
var status fuse.Status
|
2015-10-04 16:04:25 +02:00
|
|
|
data, status = f.doRead(plainOff, lastBlockLen)
|
2015-09-30 22:36:53 +02:00
|
|
|
if status != fuse.OK {
|
2015-10-04 14:21:07 +02:00
|
|
|
cryptfs.Warn.Printf("shrink doRead returned error: %v", err)
|
2015-09-30 22:36:53 +02:00
|
|
|
return status
|
|
|
|
}
|
|
|
|
}
|
|
|
|
f.lock.Lock()
|
2015-10-04 16:04:25 +02:00
|
|
|
err = syscall.Ftruncate(int(f.fd.Fd()), int64(cipherOff))
|
2015-09-30 22:36:53 +02:00
|
|
|
f.lock.Unlock()
|
|
|
|
if err != nil {
|
2015-10-04 14:21:07 +02:00
|
|
|
cryptfs.Warn.Printf("shrink Ftruncate returned error: %v", err)
|
2015-09-30 22:36:53 +02:00
|
|
|
return fuse.ToStatus(err)
|
|
|
|
}
|
2015-10-04 14:21:07 +02:00
|
|
|
if lastBlockLen > 0 {
|
2015-10-04 16:04:25 +02:00
|
|
|
_, status := f.doWrite(data, int64(plainOff))
|
2015-10-04 14:21:07 +02:00
|
|
|
return status
|
|
|
|
}
|
|
|
|
return fuse.OK
|
2015-09-30 22:36:53 +02:00
|
|
|
}
|
2015-09-08 00:54:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *file) Chmod(mode uint32) fuse.Status {
|
|
|
|
f.lock.Lock()
|
|
|
|
r := fuse.ToStatus(f.fd.Chmod(os.FileMode(mode)))
|
|
|
|
f.lock.Unlock()
|
|
|
|
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *file) Chown(uid uint32, gid uint32) fuse.Status {
|
|
|
|
f.lock.Lock()
|
|
|
|
r := fuse.ToStatus(f.fd.Chown(int(uid), int(gid)))
|
|
|
|
f.lock.Unlock()
|
|
|
|
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *file) GetAttr(a *fuse.Attr) fuse.Status {
|
2015-09-16 19:32:37 +02:00
|
|
|
cryptfs.Debug.Printf("file.GetAttr()\n")
|
2015-09-08 00:54:24 +02:00
|
|
|
st := syscall.Stat_t{}
|
|
|
|
f.lock.Lock()
|
|
|
|
err := syscall.Fstat(int(f.fd.Fd()), &st)
|
|
|
|
f.lock.Unlock()
|
|
|
|
if err != nil {
|
|
|
|
return fuse.ToStatus(err)
|
|
|
|
}
|
|
|
|
a.FromStat(&st)
|
2015-09-09 19:32:59 +02:00
|
|
|
a.Size = f.cfs.PlainSize(a.Size)
|
2015-09-08 00:54:24 +02:00
|
|
|
|
|
|
|
return fuse.OK
|
|
|
|
}
|
|
|
|
|
2015-10-04 17:14:40 +02:00
|
|
|
// Allocate FUSE call, fallocate(2)
|
2015-09-08 00:54:24 +02:00
|
|
|
func (f *file) Allocate(off uint64, sz uint64, mode uint32) fuse.Status {
|
2015-10-04 17:14:40 +02:00
|
|
|
cryptfs.Warn.Printf("Fallocate is not supported, returning ENOSYS - see https://github.com/rfjakob/gocryptfs/issues/1\n")
|
|
|
|
return fuse.ENOSYS
|
2015-09-08 00:54:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const _UTIME_NOW = ((1 << 30) - 1)
|
|
|
|
const _UTIME_OMIT = ((1 << 30) - 2)
|
|
|
|
|
|
|
|
func (f *file) Utimens(a *time.Time, m *time.Time) fuse.Status {
|
2015-10-04 20:32:15 +02:00
|
|
|
ts := make([]syscall.Timespec, 2)
|
|
|
|
|
2015-09-08 00:54:24 +02:00
|
|
|
if a == nil {
|
2015-10-04 20:32:15 +02:00
|
|
|
ts[0].Nsec = _UTIME_OMIT
|
2015-09-08 00:54:24 +02:00
|
|
|
} else {
|
2015-10-04 20:32:15 +02:00
|
|
|
ts[0].Sec = a.Unix()
|
2015-09-08 00:54:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if m == nil {
|
2015-10-04 20:32:15 +02:00
|
|
|
ts[1].Nsec = _UTIME_OMIT
|
2015-09-08 00:54:24 +02:00
|
|
|
} else {
|
2015-10-04 20:32:15 +02:00
|
|
|
ts[1].Sec = m.Unix()
|
2015-09-08 00:54:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
f.lock.Lock()
|
2015-10-04 20:32:15 +02:00
|
|
|
fn := fmt.Sprintf("/proc/self/fd/%d", f.fd.Fd())
|
|
|
|
err := syscall.UtimesNano(fn, ts)
|
2015-09-08 00:54:24 +02:00
|
|
|
f.lock.Unlock()
|
|
|
|
return fuse.ToStatus(err)
|
|
|
|
}
|