Encrypting directories with ecryptfs

Introduction

This article describes basic usage of eCryptfs. It guides you through the process of creating a private and secure encrypted directory within your $HOME directory, where you can store all your sensitive files and private data.

In implementation eCryptfs differs from dm-crypt, which provides a block device encryption layer, while eCryptfs is an actual file-system – a stacked cryptographic file system to be exact. For comparison of the two you can refer to this table .

The summary is that it doesn't require special on-disk storage allocation effort, such as separate partitions, you can mount eCryptfs on top of any single directory to protect it. That includes i.e. your entire $HOME and network file systems (i.e. having encrypted NFS shares). All cryptographic metadata is stored in the headers of files, so encrypted data can be easily moved, stored for backup and recovered. There are other advantages, but there are also drawbacks, for instance eCryptfs is not suitable for encrypting complete partitions which also means you can't protect your swap space with it (instead you can combine it with dm-crypt).

Password

If you are encrypting your whole home, with auto-mounting you should use a strong password and consider changing the hash algorithm for _/etc/shadow_. From *md5* to stronger ones like sha512/bcrypt, that helps to protect your password against rainbow-table attacks. See https://wiki.archlinux.org/index.php/SHA_password_hashes for more information.

Basics

eCryptfs is a part of Linux since version 2.6.19. But to work with it you will need the userspace tools: ecryptfs-utils which in turn requires the keyutils package from – tools for the kernel key management system. Both packages are available in the connos-extra repo

Once you install those packages you can load the ecryptfs module and continue with the setup:

 # modprobe ecryptfs

The ecryptfs-utils package is distributed with a few helper scripts which will help you with key management and similar tasks. Some were written to automate this whole process of setting up encrypted directories (ecryptfs-setup-private) or help you combine eCryptfs with dm-crypt to protect swap space (ecryptfs-setup-swap). Despite those scripts we will go through the process manually so you get a better understanding of what is really being done.

Before we say anything else it's advised that you check the eCryptfs documentation. It is distributed with a very good and complete set of manual pages.

Setup

First create your private directories, in this example we will call them exactly that: Private

 $ su -
 # mkdir -m 700 /home/username/.Private
 # mkdir -m 500 /home/username/Private
 # chown username:username /home/username/{.Private,Private}

Let's summarize

  • Actual encrypted data will be stored in ~/.Private directory (so-called lower directory)
  • While mounted, decrypted data will be available in ~/Private directory (so-called upper directory)
    • While not mounted nothing can be written to this directory
    • While mounted it has the same permissions as the lower directory

eCryptfs can now be mounted on top of ~/Private.

 # mount -t ecryptfs /home/username/.Private /home/username/Private

You will need to answer a few questions and provide a passphrase which should be used to mount this directory in the future. However you can also have different keys encrypting different data (more about this below). For convenience we will limit this guide to only one key and passphrase. Let's see an example:

 Key type: passphrase
 Passphrase: ThisIsAVeryWeakPassphrase
 Cipher: aes
 Key byte: 16
 Plaintext passthrough: no
 Filename encryption: no
 Add signature to cache: yes 

Let's summarize

  • The passphrase is your mount passphrase which will be salted, hashed and loaded into the kernel keyring.
    • In eCryptfs terms, this salted, hashed passphrase is your “file encryption key, encryption key”, or fekek.
  • eCryptfs supports a few different ciphers (AES, blowfish, twofish…). You can read about them on Wikipedia.
  • Plaintext passthrough enables you to store and work with un-encrypted files stored in the lower directory.
  • Filename encryption is available since Linux 2.6.29
    • In eCryptfs terms the key used to protect filenames is known as “filename encryption key”, or fnek.
  • The signature of the key(s) will be stored in /root/.ecryptfs/sig-cache.txt

Since our later goal is to be able to mount without root privileges, we will now move the eCryptfs configuration directory to your own home and transfer the ownership to you:

 # mv /root/.ecryptfs /home/username
 # chown username:username /home/username/.ecryptfs

Your setup is now complete and directory is mounted. You can place any file in the ~/Private directory and it will get encrypted. Before you do anything else you should inspect your /etc/mtab file, the ecryptfs entry in particular – we will discuss the importance of it a few lines below.

Now copy a few files to your new private directory, and then un-mount it. If you inspect the files you will see that they are unreadable – encrypted. That was cool you say, but how do I get them back… and that brings us to:

Mounting

When ever you need your files available you can repeat the above mount procedure, using the same passphrase and options if you want to access your previously encrypted files or using a different passphrase (and possibly options) if for some reason you want to have different keys protecting different data (imagine having a publicly shared directory where different data is encrypted by different users, and their keys).

In any case going through those questions every time could be a bit tedious. First solution is that you provide all the options to the mount command (this is where the mtab line comes in) except for your passphrase for which you will be prompted:

 $ mount -t ecryptfs /home/username/.Private /home/username/Private -o ecryptfs_cipher=aes,ecryptfs_key_bytes=16,key=passphrase

Second (and recommended) solution is to create an entry in the /etc/fstab file for this mount point:

 # eCryptfs mount points
 /home/username/.Private /home/username/Private ecryptfs rw,user,noauto,ecryptfs_sig=XY,ecryptfs_cipher=aes,ecryptfs_key_bytes=16,ecrypfs_unlink_sigs 0 0

NOTE:

Let's summarize

  • You will notice that we defined the user option, it enables you to mount the directory as a user (if it does not works as a normal user, you may need to setuid mount.ecryptfs by running as root: chmod +s /sbin/mount.ecryptfs)
  • Notice the ecryptfs_sig option, replace XY with your own key signature (as seen in the mtab line earlier and in sig-cache.txt
  • If you enabled filename encryption then pass an additional mount option: ecryptfs_fnek_sig=XY, where XY is the same signature you provide with the ecryptfs_sig option.
  • Last option ecrypfs_unlink_sigs ensures that your keyring is cleared every time the directory is un-mounted

Since your key was deleted from the kernel keyring when you un-mounted, in order to mount you need to insert it into the keyring again. You can use the ecryptfs-add-passphrase utility or the ecryptfs-manager to do it:

When the key is inserted you can mount the directory:

 $ ecryptfs-add-passphrase
   Passphrase: ThisIsAVeryWeakPassphrase
 
 $ mount -i /home/username/Private

You will notice that we used the -i option this time. It disables invoking the mount helper. Speaking of which, using -i by default mounts with: nosuid, noexec and nodev. If you want to have at least executable files in your private directory you can add the exec option to the fstab line.

This would be a good place to mention the keyctl utility from the (earlier installed) keyutils package. It can be used for any advanced key management tasks. Following examples show how to list your keyring contents and how to clear them:

 $ keyctl list @u
 $ keyctl clear @u

Usage

Besides using your private directory as storage for sensitive files, and private data, you can also use it to protect application data. Take Firefox for an example, not only does it have an internal password manager but the browsing history and cache can also be sensitive. Protecting it is easy:

  $ mv ~/.mozilla ~/Private/mozilla
  $ ln -s ~/Private/mozilla ~/.mozilla

Removal

If you want to move a file out of the private directory just move it to it's new destination while ~/Private is mounted. Also note that there are no special steps involved if you want to remove your private directory. Make sure it is un-mounted and delete ~/.Private, along with all the files.

Backup

Setup explained here separates the directory with encrypted data from the mount point, so the encrypted data is available for backup at any time. With an overlay mount (i.e. ~/Secret mounted over ~/Secret) the lower, encrypted, data is harder to get to. Today when cronjobs and other automation software do automatic backups the risk of leaking your sensitive data is higher.

We explained earlier that all cryptographic metadata is stored in the headers of files. You can easily do backups, or incremental backups, of your ~/.Private directory, treating it like any other directory.

Based on "System Encryption with eCryptfs" from Archwiki

 
Back to top
configuration/ecryptfs.txt · Last modified: 2012/02/12 17:48 by slowlife
 
 
GNU Free Documentation License 1.3
chimeric.de = chi`s home Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0