# Automatically mount dm-crypt encrypted home with pam_mount

## tuxophil

Automatically mount dm-crypt encrypted home with pam_mount

1. Introduction & Goal

I've always wanted to have my personal data stored in a secure way. Using gpg would of course be possible, but cumbersome. Simply encrypting the home partition would have the (slight) disadvantage of having to type an additional passphrase. In this HOWTO I will show you how to solve this problem in an elegant way, that is without requiring an additional password at login.

This is my first HOWTO, so please don't kill me if something's not explained clearly. During the last year I did this whole thing four times, so I should have some experience with it. Nonetheless, I cannot guarantee that every single line is correct. That being said, I don't think there are that many errors in it.

Of course, any feedback is appreciated.

2. Overview

Since I didn't want to repartition I decided to use a file as encrypted loopback device. In the newer 2.6 kernels cryptoloop is deprecated in favour of dm-crypt. Therefore (and because I don't like to change my setup every few weeks) I'm using the cryptsetup utility to setup a device mapper-based encryption (dm-crypt). The problem is of course the automatic mounting. 

Fortunately, there is the pam_mount module for PAM. At login the password is acquired by PAM which then sends it to pam_mount. Thereafter pam_mount uses this password to do the actual mounting. In fact, the login password isn't used directly since this would make it impossible to change the password at a later time. (The encryption key of the encrypted home cannot be changed.)

Therefore a master key, which is used to encrypt the home directory, is created and stored in a secure way on the harddisk. More information on how this works can be found at the pam_mount homepage.

Remark

After doing all the work I noticed that some scripts that come with pam_mount could be useful and automate some of the steps that I will present. However I didn't test these scripts. But I don't think Gentoo users will object to the manual (and more flexible) way in which the encryption will be set up in this HOWTO.

3. Installing the necessary software

First you'll have to compile your kernel with support for the device-mapper, the crypt target and some cipher modules. Details can be found in many other HOWTOs (e.g. this one), so I'll skip to the setup of pam_mount.

The pam_mount module is not yet in portage. Download the latest .tar.gz (see Bug 24213) containing the ebuild for 0.9.25 and additional files. Unpack it to /usr/local/portage/sys-libs/, activate portage overlay (uncomment the line in /etc/make.conf) and emerge it:

```
emerge pam_mount
```

You'll have to edit your PAM-configuration to use pam_mount. In this example I'll only consider console and KDM logins.

```
# /etc/pam.d/login

auth       required     /lib/security/pam_securetty.so

auth       required     /lib/security/pam_stack.so service=system-auth

auth       required     /lib/security/pam_nologin.so

# add the following line:

auth       optional     /lib/security/pam_mount.so use_first_pass

account    required     /lib/security/pam_stack.so service=system-auth

password   required     /lib/security/pam_stack.so service=system-auth

session    required     /lib/security/pam_stack.so service=system-auth

# add the following line:

session    optional     /lib/security/pam_mount.so
```

```
# /etc/pam.d/kde

# replace the next line by the one with pam_stack:

#auth       include      system-auth

auth       required     /lib/security/pam_stack.so service=system-auth

auth       required     pam_nologin.so

# add the following line:

auth       optional     /lib/security/pam_mount.so use_first_pass

account    include      system-auth

password   include      system-auth

session    include      system-auth

# add the following line:

session    optional     /lib/security/pam_mount.so
```

For the dm-crypt part we'll need cryptsetup. Just emerge it.

```
emerge cryptsetup
```

(or, perhaps better, cryptsetup-luks).

4. The encryption

You'll have to modprobe at least one crypto algorithm if you didn't compile them in your kernel. Put it also in your /etc/modules/autoload.d/kernel-2.6:

```
modprobe aes

echo aes >> /etc/modules.autoload.d/kernel-2.6
```

For the rest of this HOWTO the user "frodo" will be used as an example. Let's start by creating a 2 GiB file which will contain the encrypted home directory. (Creating the file will take some time.)

```
dd if=/dev/urandom of=/home/frodo_home bs=1M count=2048

# create loopback block device

losetup /dev/loop0 /home/frodo_home
```

Since we won't have to type the master password it can (and should) be random data. A nice way to create it is the following:

```
KEY=`tr -cd [:graph:] < /dev/urandom | head -c 79`
```

This way, all non-graphical ASCII characters are discarded, leaving 94 possibilities left. In this example the keyspace corresponds to 512 bits. (512 * log(2) / log(94) = 78.1 digits to base 94)

cryptsetup will hash it to create a 256 bit key that can be used by AES. This method has the advantage that the key is plain ASCII which could be crucial in an emergency situation. Furthermore there won't be any problems with programs which cannot cope with full binary passwords.

In the next step we'll create the block device /dev/mapper/frodo and format it.

```
echo $KEY | cryptsetup create frodo /dev/loop0

mke2fs /dev/mapper/frodo

# remove the mapping:

cryptsetup remove frodo
```

In the loop-AES README there are warnings against using a journaling filesystem on a loop-AES-encrypted file. I don't know if this also applies to dm-crypt on a loop device. If somebody could inform me about this I'd be very happy.

Now, we'll encrypt the master key and store it on the hard disk. Use your login password!

```
echo $KEY | openssl aes-256-ecb > /home/frodo.key
```

To make it possible for the user to change his password later on, we'll have to create a backup file and set the correct permissions:

```
touch /home/frodo.key.old

chown frodo /home/frodo.key /home/frodo.key.old

chmod 600 /home/frodo.key /home/frodo.key.old
```

This is necessary for the password changing script passwdehd to work. More on this later.

Let's create the mount point and set the permissions.

```
mkdir /home/frodo2

chown frodo /home/frodo2

chgrp users /home/frodo2

chmod 700 /home/frodo2
```

Now comes the crucial part: configuring pam_mount. You'll need to edit /etc/security/pam_mount.conf and replace the Gentoo example lines by the following one.

```
volume frodo crypt - /home/frodo_home /home/frodo2 loop,cipher=aes aes-256-ecb /home/frodo.key
```

Theoretically, the automatic mounting should work right now. Close all your sessions as frodo, switch to a VT and relogin as frodo. There should be quite a few informational messages but no errors. (As root you can try to copy some files to /home/frodo2 and delete them again.) If everything works fine, we'll move all the data to the new home directory.

5. Migrating the data

```
# In the same VT you're logged in as frodo!

# This ensures that /home/frodo2 is mounted.

su

# Change permissions for mounted filesystem

chown frodo /home/frodo2

chgrp users /home/frodo2

chmod 700 /home/frodo2

cd ~frodo

# copy everything (this will take some time)

tar cpf - . | tar -xpf - -C /home/frodo2

exit   # Exit su

exit   # Logout frodo. This should unmount frodo's new home.

RELOGIN as root!

# make backup copy of old home

mv /home/frodo /home/frodo3

mv /home/frodo2 /home/frodo

# Change frodo2 to frodo in pam_mount.conf

$EDITOR /etc/security/pam_mount.conf
```

If you've done everything right, you can now login as frodo and the encrypted file is mounted at /home/frodo and is used as your home directory. As a last step remove the backup:

```
rm -rf /home/frodo3
```

(Warning: for optimal security you should consider overwriting the partition containing ~frodo3 with random data.)

6. Using a partition

Of course, it's also possible to use an encrypted partition with pam_mount. There are only minor modifications which need to be done:

 Make sure you've got a free partition (for example /dev/hda2) that's big enough.

 Prepare the partition: 

```
dd if=/dev/urandom of=/dev/hda2 bs=1M
```

 Skip the losetup step.

 Each time /dev/loop0 appears, replace it by the partition, e.g. /dev/hda2.

 Don't use the loop option in pam_mount.conf:

```
volume frodo crypt - /dev/hda2 /home/frodo2 cipher=aes aes-256-ecb /home/frodo.key
```

and after the migration change it to

```
volume frodo crypt - /dev/hda2 /home/frodo cipher=aes aes-256-ecb /home/frodo.key
```

7. Choosing non-default ciphers

If you don't like AES and would prefer some other cipher, e.g. Blowfish, this can be done too. The command

```
echo $KEY | cryptsetup create frodo /dev/loop0
```

can be replaced by

```
cryptsetup -c blowfish-cbc-essiv:sha256 -h sha1 -s 192
```

to use Blowfish in ESSIV mode, a 192-bit key size, and SHA1 for hashing the passphrase.

In pam_mount.conf you'd then change the configuration line to

```
volume frodo crypt - /home/frodo_home /home/frodo2 loop,cipher=blowfish-cbc-essiv:sha256 aes-256-ecb /home/frodo.key
```

You could also change the OpenSSL cipher which is used to store the encrypted master key. (For example, you could use bf-cbc instead of aes-256-ecb.)

8. Remarks

 When changing the login password the master key must be reencrypted. This can be achieved with the script /usr/bin/passwdehd:

```
# as root:

passwd frodo

passwdehd frodo

# or as frodo:

passwd

passwdehd
```

 Only, after doing all the work I described above, I noticed that there came

some useful scripts with pam_mount:

 mkehd could be used to setup an eencrypted home directory

 mountehd and autoehd to mount an ehd.

I don't have any experience with them and I leave it to the reader to see if those scripts are useful and work with dm-crypt-based encryption.

 The method I've presented has maximal flexibility since everything is done manually. For example, contrary to mkehd, the master key in my setup is plain ASCII, which could be useful sometimes.

 Do not forget your regular backups (you do make backups, right?) since an encrypted filesystem might be a bit more fragile when it comes to crashes or power failure etc.

9. How secure is this?

Disclaimer: Although I'm interested in cryptography, I'm by no means an expert!

The block encryption algorithm itself, which in my case is AES, should be as secure as it can possibly get. The big problem is how to design a secure system around this block cipher. Therefore I'll give you some important information that you should be aware of when using this setup. Since the goal of this setup is to guard against theft (or seizure) of your computer, I won't consider online attacks or (hard- or software) keyloggers and so on.

The details on much of the following can be found on Clemens Fruhwirth's excellent page about Linux hard disk encryption settings.

 If your login password is weak, you're screwed.

 Since it's very difficult to reliably delete a file in your system (especially for journaling filesystems, cf. info shred) an old version of your encrypted master key could still be recovered after you've used passwdehd. Linux Unified Key Setup (LUKS) is designed to avoid this vulnerability by always storing the key(s) in a fixed position at the start of the partition. At this point, I don't know if and how it can be used in combination with pam_mount. I'll investigate this later.

 The "plain" IV generation that is used implicitly by cryptsetup when setting up the mapping is very weak and has some shortcomings. For example, it doesn't prevent watermarking. In other words, a specially crafted file that you're lured into storing on your partition would create patterns that are recognizable when analysing the encrypted partition. (However, this does not imply that your data could be decrypted.)

A better choice for IV generation has been introduced in Linux 2.6.10: ESSIV. (e.g. use "aes-cbc-essiv:sha256" as cipher when calling cryptsetup. More info on the dm-crypt homepage.)

 Your home directory is not the only place where user information can be found: Your swap could contain anything that you've worked on and should be encrypted.

 For complete security it's also necessary to have an encrypted /tmp, or better make it tmpfs. Of course, to be secure this requires that swap is encrypted!

 It's also necessary to take care of /var (especially /var/tmp and /var/spool).

 Don't forget that slocate could leak all of your filenames... To sum up, if your password is reasonably strong, the encrypted data should be quite safe.

10. References

Of course, I used some other howtos to do all of this. What I did was simply combining different methods and implementing it on a Gentoo box. Here are some useful links in no particular order.

dm-crypt homepage

Linux hard disk encryption settings

dm-crypt wiki

Bug 24213: ebuild for pam_mount (new package)

HOWTO: Encrypt a filesystem in a loopback file via dm-crypt

dm-crypt looptutorial

pam_mount homepage

Encrypted home using pam_mount

Quick and painless: dm-crypt encrypted swap

Tip/Trick: using tmpfs for /tmp

11. Changelog

04.01.2005: initial post

04.01.2005: clarifications and additional details when using a partition

04.01.2005: added note about /var/tmp

09.01.2005: added 2 more references (tmpfs, swap)

10.01.2005: minor edit concerning /var, reminder to do backups

12.01.2005: added security information. fixed changelog dates (oops).

14.01.2005: updated instructions for pam_mount-0.9.21

23.01.2005: fixed mount.crypt

18.02.2005: pam_mount-0.9.22

12.03.2005: added note about slocate

26.09.2005: added section about non-default ciphers; general updates

29.09.2005: added "cryptsetup remove" step after mke2fsLast edited by tuxophil on Wed Oct 19, 2005 3:27 pm; edited 9 times in total

----------

## Archangel1

Great howto. Worked perfectly for me - exactly what I was looking for.

----------

## repugnant

I only had one problem.  On my 64-bit (AMD) machine pam_mount 0.9.20 fails to compile, but 0.9.21 (the latest) works fine.

----------

## lost+found

> 8. How secure...

>

In addition you should destroy RAM when finished, by overwriting it using Memtest86, or a specialized tool.

I read somewhere that broiling your RAM is even more secure.   :Wink: 

----------

## linux_girl

a nice idea will be to add a clean up password : if get busted by FBI NSA ... and the gov agency that fit the following reg-exp [A-Z][A-Z][A-Z]

that will loop 4 ever:

```
 dd if=/dev/urandom of=/dev/WHERE@home

```

into dm-crypt

any coder ?

----------

## linux_girl

 *lost+found wrote:*   

> > 8. How secure...
> 
> >
> 
> In addition you should destroy RAM when finished, by overwriting it using Memtest86, or a specialized tool.
> ...

 

how the hell the ram (a pice of hardware that need refreshing at its own speed [me ddr 333] to keep data can steel have data after computer shutdown ?)

i know that crackers strings /dev/mem >worldlist.txt and pass text to progs like john the ripper

for totale ownage of the box and friendly hosts

----------

## linux_girl

 *Archangel1 wrote:*   

> Great howto. Worked perfectly for me - exactly what I was looking for.

 

i wishe sarha mitcheal gelar was my gf

----------

## linux_girl

easier install instruction for noobs in here :

install pam_mount the just past it to do it way 

```

mkdir -p /usr/local/portage/sys-libs/

wget http://bugs.gentoo.org/attachment.cgi?id=48486 -O-|tar xzvf - -C/usr/local/portage/sys-libs/

PORTDIR_OVERLAY="/usr/local/portage/"  ACCEPT_KEYWORDS="~x86" emerge pam_mount && etc-update

```

  :Laughing: 

----------

## linux_girl

still a prob : it will be wizer to encrypt the /home instead of /home/anUser

cause this will require to split ur hard disk or make static file size for each user  and slipting the disk space btw users isnt wize : while /home allow more flexibility all the users can have all the space that remain on the /home device will .

Supose u have 3 users 

can we make a BIG BIG key and plit it into 3 halves. mounting the encrypted can be done with any of the 3 litle keys since we are linux and file perm (rxw------) sharing and mounting the same home wont be a big prob. but the prob is users key leackage where u loose the benefit of a crypto fs

if u wanted a bit more privacy add a crypted file into ur crypted home mount /dev/maper/WHATEVER ~/mini-sec/ ...blalbla

be SURE to use the same password   :Laughing:   :Laughing:   :Laughing:   :Laughing:   :Laughing:   :Laughing:   :Laughing:   :Laughing:   :Laughing:   so it appear clear cause aes_crypt(...)=aes_decrypt(...) rolof

----------

## lost+found

 *linux_girl wrote:*   

> how the hell the ram (a pice of hardware that need refreshing at its own speed [me ddr 333] to keep data can steel have data after computer shutdown ?)

  That's true in a working state. But I don't know it can be done, special equipment will be needed. I read somewherelse that gov. specialists are historically 10-20 years ahead of "us". Luckily low/medium-security is good enough for most people.   :Smile: 

----------

## tuxophil

 *linux_girl wrote:*   

> cause this will require to split ur hard disk or make static file size for each user  and slipting the disk space btw users isnt wize : while /home allow more flexibility all the users can have all the space that remain on the /home device will .

 

This is a deliberate choice: when user A is logged in, there's no need (in fact it's a security problem) for user B's home to be mounted too. If the system is hacked while A is logged in, only A's data will be compromised. Furthermore, nothing bad can happen to a filesystem when it's not mounted. Of course, you're free to do it as you like.

BTW, Konqueror has a nice built-in spell checker for text input fields. (Perhaps something similar exists for your browser of choice.)

----------

## linux_girl

 *tuxophil wrote:*   

>  *linux_girl wrote:*   cause this will require to split ur hard disk or make static file size for each user  and slipting the disk space btw users isnt wize : while /home allow more flexibility all the users can have all the space that remain on the /home device will . 
> 
> This is a deliberate choice: when user A is logged in, there's no need (in fact it's a security problem) for user B's home to be mounted too. If the system is hacked while A is logged in, only A's data will be compromised. Furthermore, nothing bad can happen to a filesystem when it's not mounted. Of course, you're free to do it as you like.
> 
> BTW, Konqueror has a nice built-in spell checker for text input fields. (Perhaps something similar exists for your browser of choice.)

 

whamo i was looking for a speel checker for my firefox but i didnt find any.

LVM2 sound interesting . however if u are loged in or u leave the screensaver on . if u get hacked while ur nice home is mounted ur file are owed buy the hackers =that will then easly find passwords of others users .....

that means if FBI are againts u would better improve ur brain memory to remebre the binary content of ur porn movies,mp3z ..... so u dont need to store then on ur 120GB Hard Disk  :Laughing:   :Laughing: 

+ if u want to setup scripts to miror or backu ur nice 5GB home 

that will be a major pain in the ass to tells cron to use password .... and

----------

## tuxophil

 *linux_girl wrote:*   

> whamo i was looking for a speel checker for my firefox but i didnt find any.

 

What about the first result of this google query? Doesn't it work?

----------

## linux_girl

 *lost+found wrote:*   

>  *linux_girl wrote:*   how the hell the ram (a pice of hardware that need refreshing at its own speed [me ddr 333] to keep data can steel have data after computer shutdown ?)  That's true in a working state. But I don't know it can be done, special equipment will be needed. I read somewherelse that gov. specialists are historically 10-20 years ahead of "us". Luckily low/medium-security is good enough for most people.  

 

10-20 ahead us that will cost $$$ to develop. knowing that they cant sell this nice teck pice . whil e druging the hacker to reveal the password or using a cluster to brut force will be the hell lot cheaper isnt ???

----------

## qwijibow

how can i modify 

```
volume frodo crypt - /home/frodo_home /home/frodo2 loop,cipher=aes aes-256-ecb /home/frodo.key
```

to simply mount an encrypted partiton using cryptsetup with the login password.

storeing the encryption key of the partiton as an encrpyted file reduces the strength of the encrpyion significantly, why have a random key, when the random key is encrypted with a non random login password.

im trying to get pam mount to simply call "mount.crypt /deev/hda3 /home/chris" and passit the login password.

thanks.

----------

## blujay

Is there a way to increase the size of a loopback filesystem file after it's been created and used?  Say I make one for a user and it gets almost full.  Can I increase the size of the loopback image without copying the data into a new, larger loopback image?

----------

## tuxophil

 *blujay wrote:*   

> Is there a way to increase the size of a loopback filesystem file after it's been created and used?

 

Well, I've never tried it, but it should be possible. Files, dm-crypt mappings and filesystems are all resizable. The only "difficulty" should be the order of the commands. I didn't test the following commands. Please don't try them on your real home. Use a test file instead. You have been warned.  :Wink: 

Here's what I would try. (Of course, frodo should be logged out, the filesystem unmounted and the mapping removed.)

```
# Enlarge the file by 500 MiB.

dd if=/dev/urandom bs=1M count=500 >> /home/frodo_home

# Create the loopdevice.

losetup /dev/loop0 /home/frodo_home

# Create the mapping. (cryptsetup uses the whole, i.e. enlarged,

# block device)

openssl aes-256-cbc -d -in /home/frodo.key \

| cryptsetup create frodo /dev/loop0

# Resize the filesystem to fill up the device. Example for e2fs.

resize2fs /dev/mapper/frodo

# Clean up.

cryptsetup remove frodo

losetup -d /dev/loop0
```

Now you should be able to log in as frodo and enjoy your enlarged home.

Please post your results. 

BTW can loop devices be resized? (That is, without removing the loop device first.) I don't think so, but it would allow to do the resizing while the filesystem is mounted: cryptsetup resize can safely be used, and some filesystems (e.g. Reiserfs, XFS) can be resized while they're mounted.

(For dm-crypt over LVM this is possible!)

----------

## blujay

Thank you very much, tuxophil, for your kind reply.  I did some more investigating after my post, and I found a way to do it!  Here's what I did (on Debian at the moment):

(after creating the encrypted image file)

1.  `umount` it.

2.  `cryptsetup remove` it.

3.  `losetup -d` it.

4.  `dd if=/dev/zero of=/path/to/file ibs=1M obs=1M seek=1024 count=1024`  (increases an exactly 1 GB [1024*1024] file by 1 GB, to a total of 2 GB).

5.  `losetup` it.

6.  `cryptsetup create` it.

7.  `e2fsck -f /dev/mapper/whatever` it (if you don't, the next command will tell you to).

8.  `resize2fs` (no parameters).

9.  `mount` it.

10.  That's it!

I tried creating a 1 GB AES-encrypted image with a simple, cryptsetup-prompted password, and filling it with ~180 MB of data.  Then I unmounted and un-cryptsetup'ed and un-losetup'ed it.  Then I increased the file to 2 GB, then losetup'ed and cryptsetup'ed, then e2fsck'ed and resize2fs'ed and mounted, and all the data was there.  I've since added more to it, and it's working perfectly.  I'm using the image as a home directory for a user (made the user and his homedir first, then logged out and copied files into the image, deleted homedir, mounted image as homedir).

Your `dd if=/dev/urandom bs=1M count=500 >> /home/frodo_home ` command looks great, and I will have to test it.  If it works, it is much better, because it's much less likely that a typo or an early-return-hit would do damage.

I'm looking forward to trying Gentoo on my new hard drive.   :Smile: 

Thanks again.

----------

## blujay

Here's another quick question.  I'm too tired to Google right now, so if you know, just tell me if it's possible and I'll research how later.   :Smile: 

Can you set up pam_mount to mount an encrypted loopback image as a user's home directory so that it gets automatically setup and mounted when the user logs in?

----------

## tuxophil

 *blujay wrote:*   

> Can you set up pam_mount to mount an encrypted loopback image as a user's home directory so that it gets automatically setup and mounted when the user logs in?

 

Well, considering the fact that this HOWTO's title is "Automatically mount dm-crypt encrypted home with pam_mount", I guess it's possible.  :Wink: 

----------

## tuxophil

 *qwijibow wrote:*   

> how can i modify 
> 
> ```
> volume frodo crypt - /home/frodo_home /home/frodo2 loop,cipher=aes aes-256-ecb /home/frodo.key
> ```
> ...

 

```
volume frodo crypt - /home/frodo_home /home/frodo2 loop,cipher=aes - -
```

This should do the job. If the last two parameters aren't specified pam_mount will use the login password.

 *qwijibow wrote:*   

> storeing the encryption key of the partiton as an encrpyted file reduces the strength of the encrpyion significantly, why have a random key, when the random key is encrypted with a non random login password.

 

You're right that this reduces the security to the strength of the password. The random password just makes sure that in every case the login password is the weakest link. You're free to store the key on a USB key instead of your hard disk. The reason for using a master key is to allow changing the login password (and not that it magically increases security).

----------

## blujay

 *tuxophil wrote:*   

>  *blujay wrote:*   Can you set up pam_mount to mount an encrypted loopback image as a user's home directory so that it gets automatically setup and mounted when the user logs in? 
> 
> Well, considering the fact that this HOWTO's title is "Automatically mount dm-crypt encrypted home with pam_mount", I guess it's possible. 

 

Bah, sorry...I got confused and thought this was just for partitions.  My bad.   :Sad: 

----------

## blujay

 *tuxophil wrote:*   

> 
> 
> ```
> dd if=/dev/urandom bs=1M count=500 >> /home/frodo_home 
> ```
> ...

 

I just tried this, and it does indeed work fine.  It's better in one way, because you don't have to calculate how far to seek with dd.  However, if you left off one of the >'s, it'd overwrite the file instead of add on to it.  Neither way is typo-proof, but they both work.   :Smile: 

Thanks for your help.

----------

## SilentShadow

HI tuxophil,

I read your how to and I think I found what I'm looking for, but I want you ask some question:

If I gain access to the machine ( e.s. with a live cd) and I stole the encrypted file with the home page of frodo user (/home/frodo_home) and the key file of the user (/home/frodo.key) I will be able to mount on another machine the file?

In any case I search for a solution to crypt some directory installed on customers server (php apache postgres), the customer don't have console or remote access to the machine but can use live cd or open the box to stole the information. The dm-crypt is a sollution but the boot password is a big problem for a server on 24/7.

thank on advance for your help

Bye DArio

----------

## tuxophil

 *SilentShadow wrote:*   

> If I gain access to the machine ( e.s. with a live cd) and I stole the encrypted file with the home page of frodo user (/home/frodo_home) and the key file of the user (/home/frodo.key) I will be able to mount on another machine the file?

 

Only if you know the password. The key file (frodo.key) is encrypted with the user's login password. (Using openssl with a cipher of your choice.) This makes automatic mounting by pam_mount possible, since normally the user does provide his login password, but with this method it's only required once.

 *SilentShadow wrote:*   

> In any case I search for a solution to crypt some directory installed on customers server (php apache postgres), the customer don't have console or remote access to the machine but can use live cd or open the box to stole the information. The dm-crypt is a sollution but the boot password is a big problem for a server on 24/7.

 

There is no boot password involved so I don't quite understand what you mean. In any case, when the machine is turned off it should be impossible to recover the encrypted data without the password. (Provided swap is encrypted etc.)

When the machine is running you'd have to acquire sufficient permissions to access the home directory.

----------

## SilentShadow

thanks,

so I need a user log-on to have the file decrypted?

I try you explain better my problem.

I'm new in a software house tha have a software based on php and postgres.

They newer think about the security of their sources so now I try to figure out how to protect the code.

The big issue with the crypt of partition or folder is the boot password. 

I can't give the password to customers because with the password they are able to read the code, and also I can't run to the customers any time the machine reboot to write the password at the console.

I Think you solution don't resolve my personal problem, I have just scheduled to try as soon as is possible..... my laptoop need a litle bit of security too.

Thank Dario

----------

## tuxophil

 *SilentShadow wrote:*   

> so I need a user log-on to have the file decrypted?

 

So you want encryption that doesn't need any password, dongle, biometric and so on? Sorry, but that's just impossible. After all, there must be "something" that gives you (i.e. your company) and nobody else access to the data. This doesn't have to be a password, but it should be clear that some kind of authentication is necessary.

In any case, if you're really worried about your data, you'll also have to consider the possibility that your customer boots a LiveCD and modifies the system to log the password. This is far from impossible. The only way to guarantee the security of your data would be to never again mount the partition or to do a complete audit of the machine before mounting.

If you don't trust your customers, don't do business with them.  :Wink: 

----------

## SilentShadow

I think I see the problem in the wrong side, I don't need to encrypt teh partition I probably need to encode and obscure the code.

I found on the net this two link (is out of topic, but for completing the discussion)

http://www.sourceguardian.com/index.html

http://www.zend.com/store/products/zend-encoder.php

thanks Dario

----------

## linux_girl

hehehe i found more usefull to encrypt the /home :

```

.:[Managing 0/82 jobs.] [CPU Load: 0.24 Uptime: 0h13m]:.

.:[8 files using 1,1M] [root@localhost ~]:.

.:[21:09:35]:. $ cat /etc/conf.d/local.start 

# /etc/conf.d/local.start:

# $Header: /home/cvsroot/gentoo-src/rc-scripts/etc/conf.d/local.start,v 1.4 2002/11/18 19:39:22 azarah Exp $

# This is a good place to load any misc.

# programs on startup ( 1>&2 )

                

cryptsetup -c aes create home /dev/hdb1

mount /dev/mapper/home /home

```

```

.:[Managing 0/82 jobs.] [CPU Load: 0.24 Uptime: 0h13m]:.

.:[8 files using 1,1M] [root@HackXoR ~]:.

.:[21:09:36]:. $ cat /etc/conf.d/local.stop

# /etc/conf.d/local.stop:

# $Header: /home/cvsroot/gentoo-src/rc-scripts/etc/conf.d/local.stop,v 1.4 2002/11/18 19:39:22 azarah Exp $

# This is a good place to unload any misc.

# programs you started above.

# For example, if you are using OSS and have

# "/usr/local/bin/soundon" above, put

# "/usr/local/bin/soundoff" here.

/etc/init.d/mldonkey stop

umount /home/john/nfs && echo nfs unmounted

umount /home &&echo HOME unmounted

```

i plan to make a SUID c prog that use system(2): && MODE xscrensaver src to umount it when i am idle for more safty to store ultra important info

in any case the RIA : wont be able to uncrypte my /home to find /home/p2p/.mldonkey/incoming/*.mp3 and my users files

```

.:[Managing 0/81 jobs.] [CPU Load: 0.62 Uptime: 0h15m]:.

.:[53 files using 746k] [root@localhost /home/john/bin]:.

.:[21:11:54]:. $ cat secmount

#!/bin/bash

su -c 'cd ~john; /sbin/losetup /dev/loop2 /home/john/secret;/bin/cryptsetup create secret /dev/loop2;mount /dev/mapper/secret /home/john/nfs;mount -o,remount exec ~john/nfs/'

```

```

.:[Managing 0/81 jobs.] [CPU Load: 0.57 Uptime: 0h15m]:.

.:[53 files using 746k] [root@localhost /home/john/bin]:.

.:[21:11:59]:. $ cat secumount

su -c 'umount /home/john/nfs;cryptsetup remove secret;/sbin/losetup -d /dev/loop2'

```

----------

## ian!

Nice How-To. I had to put some umount-foo into my local.stop so that the loop will be umounted when shutting down when the loop was mounted via kdm before.

/etc/conf.d/local.stop:

```
/bin/umount /home/<foo> && echo "/home/<foo> unmounted"

/bin/cryptsetup remove /dev/mapper/<bar> && echo "/dev/mapper/<bar> removed"

/sbin/losetup -d /dev/loop<number> && echo "loopdevice removed"
```

----------

## yem

Great HOWTO! _b Hopefully pam_mount will make it into portage soon.

A couple of points:

1) Where it says that you can theoretically login now and try creating/deleting files. Might be worth noting that you need to chown the mount point to the user after it is mounted. Confused me for a few minutes because the mountpoint changed ownership to root:root when mounted.

2) | (cd /home/frodo2; tar xpf -) could be written more clearly as | tar -xpf - -C /home/frodo2

3) If you use a login manager (guessing most people do - particularly on laptops) you need to add the auth/session lines to /etc/pam.d/{xdm,gdm,kdm}. /etc/pam.d/login is not sufficient.

Did you find anything more about using journalled filesystems on dm-crypted block devices? I have the backing file stored on an ext3 partition, but its not the same. Quick google didn't turn anything up.

----------

## tuxophil

 *ian! wrote:*   

> Nice How-To.

 

Thanks!  :Very Happy: 

 *ian! wrote:*   

> I had to put some umount-foo into my local.stop so that the loop will be umounted when shutting down when the loop was mounted via kdm before.

 

Yes, I should have added some lines about that problem. In fact there are still some processes left when you leave KDE, but only for a few ms. Adding a one second sleep to umount.crypt solves this problem. This should be more elegant.

----------

## tuxophil

 *yem wrote:*   

> 1) Where it says that you can theoretically login now and try creating/deleting files. Might be worth noting that you need to chown the mount point to the user after it is mounted. Confused me for a few minutes because the mountpoint changed ownership to root:root when mounted.

 

I think if you follow the guide this is taken care of  :Wink: 

(Have a closer look at the code in section 5.)

 *yem wrote:*   

> 2) | (cd /home/frodo2; tar xpf -) could be written more clearly as | tar -xpf - -C /home/frodo2

 

Changed.

 *yem wrote:*   

> 3) If you use a login manager (guessing most people do - particularly on laptops) you need to add the auth/session lines to /etc/pam.d/{xdm,gdm,kdm}. /etc/pam.d/login is not sufficient.

 

After emerge pam_mount you're advised to run etc-update which should point out the changes in the pam files.

 *yem wrote:*   

> Did you find anything more about using journalled filesystems on dm-crypted block devices? I have the backing file stored on an ext3 partition, but its not the same. Quick google didn't turn anything up.

 

I've recently read somewhere (IIRC some remarks by Hans Reiser) that in order to have a completely fail-safe (even unencrypted!) reiserfs partition it's in fact necessary to turn off the drive's write buffering. That sounds quite reasonable since the HDD's buffering does a reordering of the write operations.

In light of this I think that it would be somewhat irrealistic to have an absolutely fail-safe encrypted partition, but I'm by no means an expert and information seems to be scarce :-/.

Thanks for your feedback!

----------

## yem

 *tuxophil wrote:*   

> I think if you follow the guide this is taken care of 
> 
> (Have a closer look at the code in section 5.)

 I see that, but the instuctions to "relogin as frodo. try copying some files" (paraphrasing) wont work without a chown first. 

 *yem wrote:*   

> 2) | (cd /home/frodo2; tar xpf -) could be written more clearly as | tar -xpf - -C /home/frodo2

 hmm or even "rsync -arv /home/frodo/ /home/frodo2/" ?

 *tixophil wrote:*   

> After emerge pam_mount you're advised to run etc-update which should point out the changes in the pam files.

 IIRC it modifies /etc/pam.d/login only, not the [xgk]dm profiles.

It's really a great HOWTO. These are just things that confused me. YMMV  :Smile: 

----------

## tuxophil

 *yem wrote:*   

>  *tuxophil wrote:*   I think if you follow the guide this is taken care of 
> 
> (Have a closer look at the code in section 5.) I see that, but the instuctions to "relogin as frodo. try copying some files" (paraphrasing) wont work without a chown first. 

 

Crap! You're right  :Smile:  I'll fix it later. Thanks.

 *yem wrote:*   

> hmm or even "rsync -arv /home/frodo/ /home/frodo2/" ?

 

I just had a look at man rsync, it seems that it should preserve everything (special files, attributes, etc.), but I'm not sure, since ACLs and extended attributes aren't mentioned. In man tar neither, but until somebody can confirm that rsync would work at least as good as tar, I'll stick to using tar.

 *yem wrote:*   

> IIRC it modifies /etc/pam.d/login only, not the [xgk]dm profiles.

 

Hmm, strange, on the two systems I did this kdm was also modified. Hopefully nothing's wrong with the ebuild...

----------

## yem

 *tuxophil wrote:*   

> Hmm, strange, on the two systems I did this kdm was also modified. Hopefully nothing's wrong with the ebuild...

 Ah I see what happened. The ebuild includes kde (but not kdm - are they the same thing?) and login. However, I don't use kde and thus the kde file was created by pam_login. Being a new file, etc-update didn't show any diff on that file.

I would suggest either modifying kde/m, gdm and xdm OR modify login only, and include an einfo in the ebuild to tell the user that they'll need to modify the file for their chosen *dm appropriately. Something like this:

```
--- pam_mount-0.9.22.ebuild     2005-03-12 22:58:46.000000000 +1300

+++ pam_mount-0.9.22-r1.ebuild  2005-03-12 23:06:22.000000000 +1300

@@ -29,7 +29,7 @@

        insopts -m0644

        doins ${FILESDIR}/pam_mount.conf

        insinto /etc/pam.d

-       doins ${FILESDIR}/login ${FILESDIR}/kde

+       doins ${FILESDIR}/login

        # fixed mount.crypt

        #insinto /usr/bin

        #doins ${FILESDIR}/mount.crypt

@@ -38,3 +38,13 @@

        dosym /usr/bin/mount.crypt /sbin/mount.crypt

        dodoc README TODO AUTHORS ChangeLog FAQ INSTALL NEWS

 }

+

+pkg_postinst() {

+       einfo "This ebuild modifies /etc/pam.d/login to include pam_mount hooks."

+       einfo "If you use a graphical login manager such as xdm, kdm or gdm, you"

+       einfo "will need to add the following lines to the appropriate file"

+       einfo "in /etc/pam.d/:"

+       einfo

+       einfo "auth       optional     /lib/security/pam_mount.so use_first_pass"

+       einfo "session    optional     /lib/security/pam_mount.so"

+}
```

 (untested!)

----------

## thomash

Hello everyone.

This looks like a really great guide, and I want to use a partition instead of a file.

I also want the entire /home directory encrypted, and not only the homedir of my regular user (thomas).

I have only two users on my laptop, and it's no problem to use the same password for both, if needed.

Anyone interested in giving me a step-by-step guide to achieve this (I'm pretty new to it)

I get stuck on where to store the key, and I'm not supposed to mkdir /home/home I assume?

Perimissions on various files would also be a problem =)

I appriciate any help!

Thanks.

Thomas Hoff

----------

## thomash

Before attempting to encrypt all of /home, I thought I should give this tutorial a try (setting up an encrypted homedir for a specific user, from a partition)

This is what I did:

```

dd if=/dev/urandom of=/dev/sda4 bs=1M

KEY=`tr -cd [:graph:] < /dev/urandom | head -c 79`

echo $KEY | cryptsetup create bleh /dev/sda4

mke2fs /dev/mapper/bleh

echo $KEY | openssl aes-256-ecb > /home/bleh.key

touch /home/bleh.key.old

chown bleh /home/bleh.key /home/bleh.key.old

chmod 600 /home/bleh.key /home/bleh.key.old

mkdir /home/bleh2

chown bleh /home/bleh2

chgrp users /home/bleh2

chmod 700 /home/bleh2

```

Then I added this line to my /etc/security/pam_mount.conf:

```

volume bleh crypt - /dev/sda4 /home/bleh2 cipher=aes aes-256-ecb /home/bleh.key

```

Then I exited all sessions with "bleh" and logged in again. The messages i get after login looks good. No errors.

But I start in /home/bleh and not /home/bleh2

I did a 'ls /home' and got:

drwx------  18 bleh users 1448 Apr  8 23:08 bleh

-rw-------   1 bleh root   112 Apr  8 22:52 bleh.key

-rw-------   1 bleh root     0 Apr  8 22:52 bleh.key.old

drwxr-xr-x   3 root root  4096 Apr  8 22:51 bleh2

It changes the owner, group and permission of /home/bleh2 when i login. I verified that it had the proper permissions before login.

Anyone know what the problem is? Can it be because I'm using an external USB HDD? Or am i missing something?

----------

## thomash

Sorry, ignore my last post.

After the partition is mounted, i su'd and changed the owner, group and permisssions of /home/bleh2 (as section 5 tells me to do)

I saw someone else were asking about this too, guess i shoul've looked more closely =)

I'd still like to see how someone encrypted all of /home though

----------

## thomash

Here is what i did to have all of /home encrypted, using a partition (on an external USB HDD):

bleh is my user.

```

dd if=/dev/urandom of=/dev/sda4 bs=1M 

KEY=`tr -cd [:graph:] < /dev/urandom | head -c 79`

echo $KEY | cryptsetup create bleh /dev/sda4

mke2fs /dev/mapper/bleh

echo $KEY | openssl aes-256-ecb > /etc/bleh.key

touch /etc/bleh.key.old

chown bleh /etc/bleh.key /etc/bleh.key.old

chmod 600 /etc/bleh.key /etc/bleh.key.old

```

Add the following line to /etc/security/pam_mount.conf:

```

volume bleh crypt - /dev/sda4 /home cipher=aes aes-256-ecb /etc/bleh.key

```

Exit all sessions as bleh, login as root and do:

```

mv /home /home2

mkdir /home

chown bleh /home

chgrp users /home

chmod 700 /home

```

Exit root, login as bleh and do:

```

su

mkdir /home/bleh

chown bleh /home/bleh

chgrp users /home/bleh

chown 700 /home/bleh

```

Exit bleh and relogin. Now you should start in your new homedir in the encrypted /home.

You can now move all your files in /home2/bleh to your new homedir.

I haven't added a second user yet. Not sure how/if it would work. But i'll post more info as soon as i have it.

----------

## kueka

the setup works fine, only a little thing goes wrong. 

i have tested the ssh login and it's fail with some errors.

after a little search on the web i have found the solution:

in /etc/ssh/sshd_conf set this parameters:

ChallengeResponseAuthentication no

PasswordAuthentication yes

and ssh login works.

so long

----------

## gregory_bleiker

Hi All

Thanks for the nice HOWTO. However, I am (unfortunately) not getting it completely to work with SSH. I'm trying to have encrypted directories in a user's home and the user logs in (exclusively) via SSH.

What I have done so far:

Installed gentoo 2.6 kernel with LVM enabled (as in http://gentoo-wiki.com/SECURITY_Encrypting_Root_Filesystem_with_DM-Crypt)

Followed this howto for setting up a loop device

Read through all the /usr/share/docs on pam stuff

Put pam_mount.so into the /etc/pam.d/sshd as described on http://www.flyn.org/projects/pam_mount/index.html

The problem is that the drive is not unmounted on logout. I have applied the patch of http://bugzilla.mindrot.org/show_bug.cgi?id=926 to OpenSSH and changed /etc/ssh/sshd_config to not use privilege separation, without success... 

Mounting the drive is no problem, but I have so far not been able to unmount a drive through pam_mount.

If I have UsePrivilegeSeparation, the unmount doesn't even get called, if I have it on it gets called but with wrong uid, so it can't unmount the drive because it was mounted as root in the auth part of pam.d. (it tells me mount disagrees with fstab, or if I put the mount command into a local ~/.pam_mount.conf, it tells me I'm not root, which I know, but I still think I'm great...  :Wink: 

Any thoughts or help is highly appreciated.

Gregi

----------

## yem

BTW: I just did a catchup emerge world and pam_mount broke. Turns out cryptsetup links against libdevmapper.so.1.00 exactly and it had been updated to 1.01. If you get an error about libdevmapper.so.x.xx not found (with debug turned on in /etc/security/pam_mount.conf) then try emerging cryptsetup again so that it will re-link with the current version of the lib.

----------

## svf

heya...

nice howto.. im going to try that tomorrow morning..

but two questions..

is there any other way to do this(mount cryptet home on login) because according to the webpage pam_mount is discontinued  :Sad:  maybe there some other neat way with the new baselayout  :Wink: 

and second.. are there any other howtos how i can secure some of my /var/spool files? (tmpfs and pgp on shutdown? and risking data loss on crash?)

any other ideas?

nico

----------

## tuxophil

 *svf wrote:*   

> is there any other way to do this(mount cryptet home on login) because according to the webpage pam_mount is discontinued  maybe there some other neat way with the new baselayout 

  Unfortunately I don't think there's another way that doesn't require a second passphrase. If you want to enter your passphrase only once it must be passed by PAM to the mount command. I don't know of any other pam_mount-like program. It's a pity that it's discontinued. But then again, it works just fine. The only I thing I may try sometime is to hack LUKS-support into it. (But only if it doesn't require too much C code.)

 *svf wrote:*   

> and second.. are there any other howtos how i can secure some of my /var/spool files? (tmpfs and pgp on shutdown? and risking data loss on crash?)
> 
> any other ideas?

  My setup is as follows: large enough swap, mount a 2GiB maximum tmpfs-filesystem as /tmp. Since tmpfs is handled by the VM, it always resides in RAM or swap. Just encrypt your swap and everything's fine.

/var/tmp is more problematic since these files should be persistent across reboots. Unfortunately the only solution I see is to make a system-wide encrypted partition for /var/tmp. But then you'll have to enter its passphrase at each startup.

----------

## svf

hi 

everythings working fine - thanks again

so the weakest point in the chain is my user password... i think i do have a really strong password but.. how is it stored? whats the algorithm used for /etc/shadow

(blowfish? des?) somebody knows about that?

----------

## tuxophil

 *svf wrote:*   

> so the weakest point in the chain is my user password...

  Normally yes. But don't forget about the unencrypted parts of your system...

 *svf wrote:*   

>  i think i do have a really strong password but.. how is it stored? whats the algorithm used for /etc/shadow (blowfish? des?) somebody knows about that?

  IIRC it's salted MD5. MD5 may not be the strongest hash around, but brute forcing it would still be quite a feat.

----------

## Massimo B.

Is there a way to do your setup also with the  new baselayout and  /etc/conf.d/cryptfs ? Encrypting the swap partition that way was terribly easy.

----------

## tuxophil

 *paoleela wrote:*   

> Is there a way to do your setup also with the  new baselayout and  /etc/conf.d/cryptfs ?

 

I don't think so. AFAIK conf.d/cryptfs requires the entry of a (system-level?) passphrase at boot time. To transparently mount a partition when the user logs in you'll have to use pam_mount.

----------

## hippysurfer

I have followed the HOWTO (many thanks to its author) but I have a problem with the encrypted filesystem not being unmounted on logout. I am logging in using 'su' so I have setup /etc/pam.d/su to use pam_mount and the mounting on login works fine. But when I logout I get the following log:

```

pam_mount: received order to close things

pam_mount: real and effective user ID are 405 and 405.

pam_mount: user is guest

pam_mount: command: /usr/sbin/pmvarrun [-u] [guest] [-d] [-o] [-1]

pam_mount: could not open count file /var/run/pam_mount/guest

pam_mount: pmvarrun says login count is 0

pam_mount: going to unmount

pam_mount: information for mount:

pam_mount: --------

pam_mount: (defined by globalconf)

pam_mount: user:          guest

pam_mount: server:

pam_mount: volume:        /home/.guest.img

pam_mount: mountpoint:    /home/.guest

pam_mount: options:       loop,cipher=aes

pam_mount: fs_key_cipher: aes-256-ecb

pam_mount: fs_key_path:   /home/.guest.key

pam_mount: use_fstab:   0

pam_mount: --------

pam_mount: command: /usr/sbin/lsof [/home/.guest]

pam_mount: lsof output (should be empty)...

pam_mount: waiting for lsof

pam_mount: command: /usr/bin/umount.crypt [/home/.guest]

pam_mount: umount errors (should be empty):

pam_mount: pam_mount: error setting uid to 0

pam_mount: mlockall failed: Cannot allocate memory

pam_mount: WARNING!!! Possibly insecure memory. Are you root?

pam_mount: Command failed: Invalid argument

pam_mount: umount: /home/.guest is not in the fstab (and you are not root)

pam_mount: umount.crypt: error unmounting /home/.guest

pam_mount: waiting for umount

pam_mount: unmount of /home/.guest.img failed

pam_mount: pam_mount execution complete

```

For some reason pam_mount can't setuid to root so the unount fails.

I can su to root and run umount.crypt /home/.guest and it works fine.

hmm

Regards

Richard

----------

## thomash

When logging in, the same way I allwasy do, pam_mount gives me:

pam_mount: reading options_allow...

pam_mount: reading options_require...

pam_mount: back from global readconfig

pam_mount: per-user configurations not allowed by pam_mount.conf

pam_mount: real and effective user ID are 0 and 0.

pam_mount: checking sanity of volume record (/dev/sda4)

pam_mount: about to perform mount operations

pam_mount: information for mount:

pam_mount: --------

pam_mount: (defined by globalconf)

pam_mount: user:          bleh

pam_mount: server:

pam_mount: volume:        /dev/sda4

pam_mount: mountpoint:    /home/bleh

pam_mount: options:       cipher=aes

pam_mount: fs_key_cipher: aes-256-ecb

pam_mount: fs_key_path:   /home/bleh.key

pam_mount: use_fstab:   0

pam_mount: --------

pam_mount: checking to see if /dev/mapper/_dev_sda4 is already mounted at /home/bleh

pam_mount: checking for encrypted filesystem key configuration

pam_mount: decrypting FS key using system auth. token and aes-256-ecb

pam_mount: about to start building mount command

pam_mount: command: /bin/mount [-t] [crypt] [-o] [cipher=aes] [/dev/sda4] [/home/bleh]

pam_mount: mount errors (should be empty):

pam_mount: mount: wrong fs type, bad option, bad superblock on /dev/mapper/_dev_sda4,

pam_mount:        missing codepage or other error

pam_mount:        In some cases useful info is found in syslog - try

pam_mount:        dmesg | tail  or so

pam_mount:

pam_mount: mount.crypt: error mounting _dev_sda4

pam_mount: waiting for mount

pam_mount: mount of /dev/sda4 failed

pam_mount: clean system authtok (0)

pam_mount: command: /usr/sbin/pmvarrun [-u] [bleh] [-d] [-o] [1]

pam_mount: pmvarrun says login count is 1

pam_mount: done opening session

dmesg | tail     gives:

EXT2-fs error (device dm-1): ext2_check_descriptors: Block bitmap for group 0 not in group (block 1702113070)!

EXT2-fs: group descriptors corrupted!

EXT2-fs error (device dm-1): ext2_check_descriptors: Block bitmap for group 0 not in group (block 1702113070)!

EXT2-fs: group descriptors corrupted!

I don't even know where to start solving this problem. 

All I know is that I'd REALLY like to recover some of my documents on this partition =)

Does anyone have any idea whats causing this, and how it can be solved?

----------

## thomash

As an update to my post above I can add that my /dev/mapper/bleh is gone.

I unmerged both pam and pam_mount, to start the guide from the beginning again.

But /etc/pam.d/login is not created when emerging the newest versions of pam and pam_mount.

I'm lost  :Sad: 

----------

## thomash

Hello again. pam had nothing to do with this problem.

It turned out to be some problem with the filesystem as the error message said, and I solved it the following way:

openssl aes-256-ecb -d -in /home/bleh.key

cryptsetup --verbose --verify-passphrase create sda4 /dev/sda4  (use the output from the openssl command as password)

e2fsck /dev/mapper/sda4

I didn't really understand what dm-crypt and cryptsetup did. Now it's more clear.

----------

## dkey

hi!

great howto! but, what about live cds? when I have physical access to the computer, I can boot a live cd and get the keyfile, or?

----------

## yem

Has anyone else found that a recent update causes GDM to now ask for the password twice? Here is my /etc/pam.d/gdm:

```
#%PAM-1.0

auth       optional             pam_env.so

auth       include              system-auth

auth       required             pam_nologin.so

auth       optional     /lib/security/pam_mount.so use_first_pass

account    include              system-auth

password   include              system-auth

session    include              system-auth

session    optional     /lib/security/pam_mount.so
```

It appears pam_mount is not getting the password token that should be provided by the previous modules (despite the presence of use_first_pass), so it asks for the password a second time itself.

 *Quote:*   

> Dec  6 21:37:00 duck gdm[10198]: pam_mount: error trying to retrieve authtok from auth code

 

/etc/pam.d/system-auth is the normal gentoo default:

```
#%PAM-1.0

auth       required     pam_env.so

auth       sufficient   pam_unix.so likeauth nullok

auth       required     pam_deny.so

account    required     pam_unix.so

password   required     pam_cracklib.so difok=2 minlen=8 dcredit=2 ocredit=2 retry=3

password   sufficient   pam_unix.so nullok md5 shadow use_authtok

password   required     pam_deny.so

session    required     pam_limits.so

session    required     pam_unix.so
```

----------

## searcher

I get the same error. I didn't change anything from the default, and tried console only. The error forces me to type the same password twice. Some google-ing turned up this page on a RedHat mailing list. Seems they made a design decision that broke pam-mount. Looking on the bright side, you can have two different passwords, one for login and one for encryption  :Smile: 

If someone knows a fix for this (besides hacking on the pam-modules code) i'd be happy to try.

*edit*

Nevermind, i just added both the lines needed at the bottom of /etc/pam.d/login and it worked just fine. No weird errors or anything. Kinda strange that it wouldn't work with the line higher up in the file.

----------

## tuxophil

 *dkey wrote:*   

> great howto! but, what about live cds? when I have physical access to the computer, I can boot a live cd and get the keyfile, or?

 

You're right that the keyfile can be retrieved, but that's why it's encrypted with your (hashed) passphrase! You could also let pam_mount use your login password as dm-crypt passphrase. But in this case you could only change your login password if you also reencrypt the entire partition. That's why this master key is necessary. To sum it up, your login password is always the weakest link.

----------

## tuxophil

I've also noticed some changes in Gentoo's PAM setup. Unfortunately I can't remember exactly what I had to change in order to get it to work again. Anyway here are my current pam.d/login and pam.d/kde files:

```
# /etc/pam.d/login

#%PAM-1.0

auth       required     /lib/security/pam_securetty.so

auth       required     /lib/security/pam_stack.so service=system-auth

auth       required     /lib/security/pam_nologin.so

auth       optional     /lib/security/pam_mount.so use_first_pass

account    required     /lib/security/pam_stack.so service=system-auth

password   required     /lib/security/pam_stack.so service=system-auth

session    required     /lib/security/pam_stack.so service=system-auth

session    optional     /lib/security/pam_mount.so
```

```
# /etc/pam.d/kde

#%PAM-1.0

auth       required     /lib/security/pam_stack.so service=system-auth

auth       required     pam_nologin.so

auth       optional     /lib/security/pam_mount.so use_first_pass

account    include      system-auth

password   include      system-auth

session    include      system-auth

session    optional     /lib/security/pam_mount.so
```

----------

## searcher

By adding the following line to /etc/pam.d/common_auth:

```
auth    optional        pam_mount.so use_first_pass
```

and to /etc/pam.d/common_session:

```
session optional        pam_mount.so
```

you can enable the pam_mount login for any way a user can login (kdm, gdm, login etc). Taken from the Debian Grimoire. It's also possible to use a simple @include command, look to the referenced link for more info. I also noticed that the image or partition doesn't get unmounted if there are any programs with open files on that image/partition. Applications such as gpg-agents and gam_server stay in the background, even when logging off, preventing the image/partition from being unmounted.

----------

## Guschtel

Maybe i just didn't read it, but one has to add the following to the pam_mount config so that the crypto device gets removed:

cryptumount /usr/bin/umount.crypt %(MNTPT)

and thanks for the "tutorial"!

----------

## Massimo B.

Hello.

Did you already try with LUKS? The comments in my pam_mount.conf point to your howto here and say: 

```
# Note that pam_mount is LUKS (http://luks.endorphin.org) aware. To

# use luks, you need to have cryptsetup-luks (get it at

# http://luks.endorphin.org/dm-cryp) installed. A config line would be

#volume user1 crypt - /dev/yourpartition /yourmountpoint - - -

# and cryptsetup will be told to read cypher/keysize/etc. from the luks-header.
```

According to  EncryptedDeviceUsingLUKS  I tried with # cryptsetup --verbose --verify-passphrase luksFormat /dev/hda2. As I understand the passphrase now should be my keyfile? I don't know how to link to the file.

----------

## Massimo B.

You said: *Quote:*   

> ..an old version of your encrypted master key could still be recovered after you've used passwdehd

 but of course every passwdehd an old version is store in key.old. Shouldn't the old encrypted key file be deleted afterwards?

----------

## Massimo B.

 *tuxophil wrote:*   

> Yes, I should have added some lines about that problem. In fact there are still some processes left when you leave KDE, but only for a few ms. Adding a one second sleep to umount.crypt solves this problem. This should be more elegant.

 Where should I add this sleep 1? I noticed that I can umount myself after logout from kde is finished.

Usually I transport my laptop by logging out and putting to sleep. Then I'd like to have my home umounted AND encrypted.

I tried in /etc/security/pam_mount.conf something like..

```
cryptumount 'sleep 5 && /usr/bin/umount.crypt %(MNTPT)'
```

but the logs still claim

```
pam_mount: command: /usr/bin/umount.crypt [/home]
```

----------

## Massimo B.

 *Guschtel wrote:*   

> cryptumount /usr/bin/umount.crypt %(MNTPT)

 I don't think so, get the message pam_mount: Unknown Config-Option: cryptumount'. umount.crypt is used anyway according to the debug text.

----------

## inode77

Have successfully done it using console login but not xdm.

Here's the error after xdm is killed almost instantly after login:

```
X Window System Version 6.8.2

Release Date: 9 February 2005

X Protocol Version 11, Revision 0, Release 6.8.2

Build Operating System: Linux 2.6.15-gentoo i686 [ELF] 

Current Operating System: Linux stingray 2.6.15-gentoo-r4 #1 PREEMPT Tue Feb 7 23:54:29 CET 2006 i686

Build Date: 20 January 2006

        Before reporting problems, check http://wiki.X.Org

        to make sure that you have the latest version.

Module Loader present

Markers: (--) probed, (**) from config file, (==) default setting,

        (++) from command line, (!!) notice, (II) informational,

        (WW) warning, (EE) error, (NI) not implemented, (??) unknown.

(==) Log file: "/var/log/Xorg.0.log", Time: Wed Feb  8 01:07:16 2006

(==) Using config file: "/etc/X11/xorg.conf"

Using vt 7

pam_mount: pam_sm_open_session args: use_first_pass

pam_mount: saving authtok for session code

xdm error (pid 9397): Unknown session exit code 2816 from process 9405

```

And here is my "/etc/pam.d/xdm":

```
auth       required     pam_stack.so service=system-auth

auth       required     pam_nologin.so

account    required     pam_stack.so service=system-auth

password   required     pam_stack.so service=system-auth

session    required     pam_stack.so service=system-auth

session    optional     pam_console.so

auth       optional    /lib/security/pam_mount.so use_first_pass

session    optional     /lib/security/pam_mount.so

```

Does somebody have a hint on how to solve this problem?

----------

## Guschtel

Hi,

i found that sometimes there are some processes left, that are working on the device and therefore the device does not get unmounted an encrypted which is very bad (imho).

Therefore i modified the umount.crypt script and inserted

# Change here

FUSER=/usr/bin/fuser

and then

# ask cryptsetup about the underlying device

REALDEVICE=`$CRYPTSETUP status $DMDEVICE | sed -n '/device/s/[ ]*device:[ ]*//p'`

# Change here

# kill all User processes on the device

$FUSER -km $1

$UMOUNT "$1"

Did anyone of you also experience this problem? Should i maybe file a "bug report" to get this included?

GuschtelLast edited by Guschtel on Tue Feb 28, 2006 6:54 pm; edited 1 time in total

----------

## Guschtel

 *paoleela wrote:*   

>  *Guschtel wrote:*   cryptumount /usr/bin/umount.crypt %(MNTPT) I don't think so, get the message pam_mount: Unknown Config-Option: cryptumount'. umount.crypt is used anyway according to the debug text.

 

Yes, your right.

Sorry for that - i don't know why it didn't work that day - must have been something else.

----------

## R. Bosch

Could you post a new version of the pam_mount package on your link? The pam_mount version is now at 0.12.0. Also the homepage has been changed to this.

----------

## batistuta

What about sharing your encrypted files with other users? This is possible in Windows XP, but they have a week link, which is, that the administrator can access the encrypted files. That is totally nuts!   :Evil or Very Mad: 

I find this particularly useful, for example with my music database. I want to encrypt my /share/music partition, but I want this to be accessible by a set of users, or at least by a group. Admins (i.e. booting from a liveCD) should not.

In the ideal case, this should be done like with acls, except that the management should be done exclusively by the user. That is booting from a liveCD should not give access to the files. It looks like every user should have a key to access the partition. This sounds possible. But then, when a user is removed from the access list, they should revoke him the key. This sounds impossible to me...

Is there anything currently being done in this direction?   :Rolling Eyes: 

----------

## Massimo B.

You can provide more than one key by using Luks while the administrator is able to add and delete keys. I don't know of a possibility to see only parts of the filesystem with the one key while the other can see all of it.

----------

## svpe

Thanks for the great guide!

If you don't want to modify each /etc/pam.d/whatever file you can also try to modify only your /etc/pam.d/system-auth file which gets included in almost every application configuration file (kde,gdm,login,...)

You only need to make sure that the "auth optional..." line for the pam_mount module isn't inserted after an auth sufficant line and you need to add use_first_pass to all other module lines that need a password (like pam_unix).

----------

## skeimer

hi,

the latest pam_mount (0.13.0-r1) has forced cryptsetup-luks to be installed.

If I run cryptsetup (luks), I get:

```

echo $KEY | cryptsetup -h sha256 create secure_disk /dev/hda7

Command failed: Invalid argument

```

I have no idea what the argument error should be... I've tried some other options, but nothing works.

Modules are loaded, cryptsetup-luks is of version 1.0.1-r1

Has anyone experience with this problem?

----------

## bartek

It's problem with cryptsetup-luks is no compatible with cryptsetup :]

----------

## skeimer

 *bartek wrote:*   

> It's problem with cryptsetup-luks is no compatible with cryptsetup :]

 

sure, it's not compatible, but I tried to initially setup the partition, though it should work...

The syntax is the same for both flavours of cryptsetup, does the luks version eventually need a prior step to setup?

----------

## vobla

hi,

i've been using pam_mount for some time and got everything worked until an update broke it.

i have whole partition encrypted and pam_mount mounted it for me during login. now it fails with this:

```

Jun 26 11:45:27 xxx login(pam_unix)[2989]: session opened for user xxx by (uid=0)

Jun 26 11:45:27 xxx login[2989]: pam_mount: reading options_allow...

Jun 26 11:45:27 xxx login[2989]: pam_mount: reading options_require...

Jun 26 11:45:27 xxx login[2989]: pam_mount: back from global readconfig

Jun 26 11:45:27 xxx login[2989]: pam_mount: per-user configurations not allowed by pam_mount.conf

Jun 26 11:45:27 xxx login[2989]: pam_mount: real and effective user ID are 0 and 0.

Jun 26 11:45:27 xxx login[2989]: pam_mount: checking sanity of volume record (/dev/sda5)

Jun 26 11:45:27 xxx login[2989]: pam_mount: about to perform mount operations

Jun 26 11:45:27 xxx login[2989]: pam_mount: information for mount:

Jun 26 11:45:27 xxx login[2989]: pam_mount: --------

Jun 26 11:45:27 xxx login[2989]: pam_mount: (defined by globalconf)

Jun 26 11:45:27 xxx login[2989]: pam_mount: user:          xxx

Jun 26 11:45:27 xxx login[2989]: pam_mount: server:

Jun 26 11:45:27 xxx login[2989]: pam_mount: volume:        /dev/sda5

Jun 26 11:45:27 xxx login[2989]: pam_mount: mountpoint:    /home/xxx

Jun 26 11:45:27 xxx login[2989]: pam_mount: options:       cipher=aes

Jun 26 11:45:27 xxx login[2989]: pam_mount: fs_key_cipher: aes-256-ecb

Jun 26 11:45:27 xxx login[2989]: pam_mount: fs_key_path:   /home/xxx/xxx.key

Jun 26 11:45:27 xxx login[2989]: pam_mount: use_fstab:   0

Jun 26 11:45:27 xxx login[2989]: pam_mount: --------

Jun 26 11:45:27 xxx login[2989]: pam_mount: checking to see if /dev/mapper/_dev_sda5 is already mounted at /home/xxx

Jun 26 11:45:27 xxx login[2989]: pam_mount: checking for encrypted filesystem key configuration

Jun 26 11:45:27 xxx login[2989]: pam_mount: decrypting FS key using system auth. token and aes-256-ecb

Jun 26 11:45:27 xxx login[2989]: pam_mount: error getting cipher "aes-256-ecb"

Jun 26 11:45:27 xxx login[2989]: pam_mount: mount of /dev/sda5 failed

Jun 26 11:45:27 xxx login[2989]: pam_mount: clean system authtok (0)

Jun 26 11:45:27 xxx login[2989]: pam_mount: command: /usr/sbin/pmvarrun [-u] [xxx] [-d] [-o] [1]

Jun 26 11:45:27 xxx login[2989]: pam_mount: pmvarrun says login count is 2

Jun 26 11:45:27 xxx login[2989]: pam_mount: done opening session

```

i've got following versions installed:

cryptsetup-0.1-r2

device-mapper-1.02.02

pam_mount-0.9.25

Anyone?

----------

## meatz

hello,

thanks for this great tutorial, it worked fine for me.

but i still have a little problem.

im using a fingerprint sensor with pam_bioapi to login.

so i don't have to type in any password.

is there any possibility to combine that? because first using the fingerprint sensor and after that typing the password is not that cool  :Wink: 

thanks in advance

meatz

----------

## benkelly76

I was getting this error message:

 *Quote:*   

> 
> 
> pam_mount: error trying to retrieve authtok from auth code
> 
> 

 

I ended up solving this by modifying my /etc/pam.d/system-auth file to look like this:

```

auth       required     pam_env.so

auth       required     pam_unix.so likeauth nullok

auth       sufficient   /usr/lib/security/pam_mount.so use_first_pass

auth       required     pam_deny.so

account    required     pam_unix.so

password   required     pam_cracklib.so difok=2 minlen=8 dcredit=2 ocredit=2 retry=3

password   sufficient   pam_unix.so nullok md5 shadow use_authtok

password   required     pam_deny.so

session    required     pam_limits.so

session    required     pam_unix.so

session    optional     /usr/lib/security/pam_mount.so

```

The error was occurring because pam_mount.so must run after pam_unix.so in order to pick up the authtoken, but the sufficient control word terminated execution at pam_unix.so itself.  In order to maintain the fall through setup of the files I changed pam_unix.so to required and placed the pam_mount.so module after it with the sufficient control.

Also, I can report that pam_mount 0.15.0 works with cryptsetup-luks.

----------

## postmodern

Successfully running pam_mount-0.13.0, but I've noticed I receive segfaults from su/xscreensaver when an incorrect password is given. Having one's screen locked with xscreensaver then suddenly segfaulting away really defeats the purpose of locking one's screen. I'm also using the provided /etc/pam.d/system-auth and /etc/pam.d/login. Any suggestions?

----------

## skunk

hi all...

i had to replace sys-fs/cryptsetup-0.1-r3 with sys-fs/cryptsetup-luks-1.0.3-r2 because of dependencies, but now mounting my home partitition fails:

```
pam_mount: reading options_allow...

pam_mount: reading options_require...

pam_mount: back from global readconfig

pam_mount: per-user configurations not allowed by pam_mount.conf

pam_mount: real and effective user ID are 0 and 0.

pam_mount: checking sanity of volume record (/dev/hda4)

pam_mount: about to perform mount operations

pam_mount: information for mount:

pam_mount: ----------------------

pam_mount: (defined by globalconf)

pam_mount: user:          skunk

pam_mount: server:

pam_mount: volume:        /dev/hda4

pam_mount: mountpoint:    /home

pam_mount: options:       noatime,cipher=aes

pam_mount: fs_key_cipher: aes-256-ecb

pam_mount: fs_key_path:   /etc/home.key

pam_mount: use_fstab:   0

pam_mount: ----------------------

pam_mount: realpath of volume "/home" is "/home"

pam_mount: checking to see if /dev/mapper/_dev_hda4 is already mounted at /home

pam_mount: checking for encrypted filesystem key configuration

pam_mount: decrypting FS key using system auth. token and aes-256-ecb

pam_mount: about to start building mount command

pam_mount: command: /bin/mount [-t] [crypt] [-onoatime,cipher=aes] [/dev/hda4] [/home]

pam_mount: mount errors (should be empty):

pam_mount: pam_mount: setting uid to 0

pam_mount: pam_mount: real user/group IDs are 0/100, effective is 0/100

pam_mount: mount: you must specify the filesystem type

pam_mount: mount.crypt: error mounting _dev_hda4

pam_mount: waiting for mount

Filesystem           1K-blocks      Used Available Use% Mounted on

/dev/hda3             20000088  10172784   9827304  51% /

udev                    258044       224    257820   1% /dev

none                    258044         0    258044   0% /dev/shm

/dev/hdb               8208008   8208008         0 100% /media/hdb

pam_mount: mount of /dev/hda4 failed

pam_mount: clean system authtok (0)

pam_mount: command: /usr/sbin/pmvarrun [-u] [skunk] [-d] [-o] [1]

pam_mount: setting uid to 0

pam_mount: real user/group IDs are 0/100, effective is 0/100

pmvarrun: parsed count value 0

pam_mount: pmvarrun says login count is 1

pam_mount: done opening session

No directory, logging in with HOME=/
```

sys-apps/pam_mount-0.13.0 is installed and i've this line in my /etc/security/pam_mount.conf:

```
volume skunk crypt - /dev/hda4 /home noatime,cipher=aes aes-256-ecb /etc/home.key
```

note that after failing to mount there is no device file in /dev/mapper/ (which was _dev_hda4 with cryptsetup).

going back to cryptsetup it works again, but i always have portage wanting to install cryptsetup-luks on every update...

thank you!

----------

## skunk

nobody?  :Sad: 

----------

## anoland

I had the same thing happen to me a while ago. I had to find out what was depending on the new package and get rid of it. It was a while ago, so I don't remember what it was I removed.

----------

## skunk

what's in your /etc/security/pam_mount.conf?

mine:

```
volume skunk crypt - /dev/hda4 /home noatime,cipher=aes aes-256-ecb /etc/home.key
```

----------

## anoland

It is commented out

```
#volume anoland crypt - /dev/hda5 /home notail,noatime,nodiratime,cipher=aes aes-256-ecb /home.key

```

because this happened a long time ago....

```
hades ~ # emerge -s pam_mount

Searching...   

[ Results for search key : pam_mount ]

[ Applications found : 1 ]

 

*  sys-libs/pam_mount

      Latest version available: 0.9.25

      Latest version installed: [ Not Installed ]

      Size of files: 428 kB

      Homepage:      http://www.flyn.org/projects/pam_mount/index.html

      Description:   A PAM module that can mount volumes for a user session e.g. encrypted home directories

      License:       GPL-2

hades ~ # emerge -pv pam_mount

These are the packages that would be merged, in order:

Calculating dependencies... done!

[blocks B     ] sys-fs/cryptsetup-luks (is blocking sys-fs/cryptsetup-0.1-r3)

[ebuild  N    ] sys-fs/cryptsetup-0.1-r3  243 kB 

[ebuild  N    ] sys-libs/pam_mount-0.9.25  USE="crypt" 428 kB [1] 

Total size of downloads: 671 kB

Portage overlays:

 [1] /usr/local/portage

hades ~ # 

```

Until LUKS starts to cooperate with pam_mount, I'll just put in my password on boot up.

----------

## skunk

so, if i understand correctly, there is no way to make luks working with pam_mount?

doesn't either exist another way to mount my /home partition at login without having to do it manually?

----------

## anoland

Well, 

The latest pam_mount on sourceforge is .18 and the latest in portage is .9.25. Looks like the ebuild needs an update. Maybe the latest pam_mount will work better. But that still doesn't get rid of the block between cryptsetup and cryptsetup-luks.

----------

## feld

did anyone ever confirm whether or not this can be safely used with journaled filesystems?

----------

## anoland

I use it with ReiserFS without any problems.

----------

## feld

 *anoland wrote:*   

> I use it with ReiserFS without any problems.

 

cool. i'll move mine to ext3 then.

----------

## are

 *skunk wrote:*   

> so, if i understand correctly, there is no way to make luks working with pam_mount?
> 
> doesn't either exist another way to mount my /home partition at login without having to do it manually?

 

```

volume are crypt - /dev/hdb2 /home/are - - -

```

i use pam_mount 1.17 together with cryptsetup-luks and without problems. after i typed my[/code] password into gdm, it mounts my encrypted home partition and it unmount it (often), when i log out.

what exactly is your problem?

best regards!

are

----------

## skunk

are, you don't specify where's the key file in your pam_mount.conf, how is it possible that the partition is mounted without asking for the key? is your login password itself the key for decrypting the partition? this is not my case, i have the key encrypted into /etc/home.key and my login password is the key for just accessing to that file...

my problem is that the device node /dev/mapper/_dev_hda4 doesn't been created with luks causing the mount error...

----------

## are

 *skunk wrote:*   

> are, you don't specify where's the key file in your pam_mount.conf, how is it possible that the partition is mounted without asking for the key? is your login password itself the key for decrypting the partition? this is not my case, i have the key encrypted into /etc/home.key and my login password is the key for just accessing to that file...
> 
> my problem is that the device node /dev/mapper/_dev_hda4 doesn't been created with luks causing the mount error...

 

I'm no expert, but I've thought, that is the way, luks works. it stores everything inside the partition. no special key-file is used, but the passphrase is collected from pam directly.

----------

## are

 *skunk wrote:*   

> this is not my case, i have the key encrypted into /etc/home.key and my login password is the key for just accessing to that file....

 

sorry, my fingers type more quick than my eyes read your question. indeed my password logs me in and decrypts the volume. on the other side, i don't understand, why you seperate the keyfile from your password? if it's for legacy, then there are good news: luks can store multiple passphrases. add your login-password to luks, and then the partition opens after login too.

----------

## anoland

for those that are still following this thread...

There appears to be a good amount of activitiy on pam_mount.

https://bugs.gentoo.org/show_bug.cgi?id=24213

I'll keep my fingers crossed that it makes it mainstream soon.

----------

## feld

 *Guschtel wrote:*   

> Hi,
> 
> i found that sometimes there are some processes left, that are working on the device and therefore the device does not get unmounted an encrypted which is very bad (imho).
> 
> Therefore i modified the umount.crypt script and inserted
> ...

 

bringing this to light for those that are wondering why logoff doesn't umount... this DOES indeed solve it.

----------

## yem

 *are wrote:*   

>  *skunk wrote:*   so, if i understand correctly, there is no way to make luks working with pam_mount?
> 
> doesn't either exist another way to mount my /home partition at login without having to do it manually? 
> 
> ```
> ...

 

I was happily using pam_mount 0.9.25 and cryptsetup until portage made me switch to cryptsetup-luks for some reason. Now pam_mount cannot mount my /home. A console login attempt as me (zach) goes like this:

```
Nov 15 03:16:04 duck login[10061]: pam_mount(readconfig.c:197) reading options_allow...

Nov 15 03:16:04 duck login[10061]: pam_mount(readconfig.c:180) reading options_require...

Nov 15 03:16:04 duck login[10061]: pam_mount(pam_mount.c:439) back from global readconfig

Nov 15 03:16:04 duck login[10061]: pam_mount(pam_mount.c:441) per-user configurations not allowed by pam_mount.conf

Nov 15 03:16:04 duck login[10061]: pam_mount(pam_mount.c:459) pam_sm_open_session: real uid/gid=0:0, effective uid/gid=0:0

Nov 15 03:16:04 duck login[10061]: pam_mount(readconfig.c:418) checking sanity of volume record (/home/zach.encrypted)

Nov 15 03:16:04 duck login[10061]: pam_mount(pam_mount.c:474) about to perform mount operations

Nov 15 03:16:04 duck login[10061]: pam_mount(mount.c:368) information for mount:

Nov 15 03:16:04 duck login[10061]: pam_mount(mount.c:369) ----------------------

Nov 15 03:16:04 duck login[10061]: pam_mount(mount.c:370) (defined by globalconf)

Nov 15 03:16:04 duck login[10061]: pam_mount(mount.c:373) user:          zach

Nov 15 03:16:04 duck login[10061]: pam_mount(mount.c:374) server:        

Nov 15 03:16:04 duck login[10061]: pam_mount(mount.c:375) volume:        /home/zach.encrypted

Nov 15 03:16:04 duck login[10061]: pam_mount(mount.c:376) mountpoint:    /home/zach

Nov 15 03:16:04 duck login[10061]: pam_mount(mount.c:377) options:       loop,cipher=aes

Nov 15 03:16:04 duck login[10061]: pam_mount(mount.c:378) fs_key_cipher: aes-256-ecb

Nov 15 03:16:04 duck login[10061]: pam_mount(mount.c:379) fs_key_path:   /home/zach.key.encrypted

Nov 15 03:16:04 duck login[10061]: pam_mount(mount.c:380) use_fstab:   0

Nov 15 03:16:04 duck login[10061]: pam_mount(mount.c:381) ----------------------

Nov 15 03:16:04 duck login[10061]: pam_mount(mount.c:177) realpath of volume "/home/zach" is "/home/zach"

Nov 15 03:16:04 duck login[10061]: pam_mount(mount.c:182) checking to see if /dev/mapper/_home_zach.encrypted is already mounted at /home/zach

Nov 15 03:16:04 duck login[10061]: pam_mount(mount.c:799) checking for encrypted filesystem key configuration

Nov 15 03:16:04 duck login[10061]: pam_mount(mount.c:803) decrypting FS key using system auth. token and aes-256-ecb

Nov 15 03:16:04 duck login[10061]: pam_mount(mount.c:819) about to start building mount command

Nov 15 03:16:04 duck login[10061]: pam_mount(misc.c:262) command: /bin/mount [-t] [crypt] [-o loop,cipher=aes] [/home/zach.encrypted] [/home/zach] 

Nov 15 03:16:04 duck login[10291]: pam_mount(misc.c:335) set_myuid(pre): real uid/gid=0:0, effective uid/gid=0:0

Nov 15 03:16:04 duck login[10291]: pam_mount(misc.c:370) set_myuid(post): real uid/gid=0:0, effective uid/gid=0:0

Nov 15 03:16:04 duck login[10061]: pam_mount(mount.c:851) mount errors (should be empty):

Nov 15 03:16:04 duck login[10061]: pam_mount(mount.c:100) pam_mount(misc.c:335) set_myuid(pre): real uid/gid=0:0, effective uid/gid=0:0

Nov 15 03:16:04 duck login[10061]: pam_mount(mount.c:100) pam_mount(misc.c:370) set_myuid(post): real uid/gid=0:0, effective uid/gid=0:0

Nov 15 03:16:04 duck login[10061]: pam_mount(mount.c:100) mount: you must specify the filesystem type

Nov 15 03:16:04 duck login[10061]: pam_mount(mount.c:100) mount.crypt: error mounting _home_zach.encrypted

Nov 15 03:16:04 duck login[10061]: pam_mount(mount.c:854) waiting for mount

Nov 15 03:16:04 duck login[10061]: pam_mount(pam_mount.c:478) mount of /home/zach.encrypted failed

Nov 15 03:16:04 duck login[10061]: pam_mount(pam_mount.c:123) clean system authtok (0)

Nov 15 03:16:04 duck login[10061]: pam_mount(misc.c:262) command: /usr/sbin/pmvarrun [-u] [zach] [-d] [-o] [1] 

Nov 15 03:16:04 duck login[10315]: pam_mount(misc.c:335) set_myuid(pre): real uid/gid=0:0, effective uid/gid=0:0

Nov 15 03:16:04 duck login[10315]: pam_mount(misc.c:370) set_myuid(post): real uid/gid=0:0, effective uid/gid=0:0

Nov 15 03:16:04 duck login[10061]: pam_mount(pam_mount.c:360) pmvarrun says login count is 1

Nov 15 03:16:04 duck login[10061]: pam_mount(pam_mount.c:491) done opening session
```

/etc/security/pam_mount.conf is unchanged:

```
debug 1

mkmountpoint 1

fsckloop /dev/loop7

options_allow   nosuid,nodev,loop,encryption

options_require nosuid,nodev

..

volume zach crypt - /home/zach.encrypted /home/zach loop,cipher=aes aes-256-ecb /home/zach.key.encrypted
```

Currently installed:

sys-libs/pam_mount-0.17-r1 from the bugzilla ticket

sys-fs/cryptsetup-luks-1.0.3-r2

Kernel 2.6.17.3 with suspend2 2.2.7 (it's a notebook)

Any tips for those migrating from cryptsetup to cryptsetup-luks? I want my /home back. It's got my stuff  :Sad: 

----------

## skunk

i had to backup my home and recreate a luks partition from scratch

----------

## yem

LUKS seems like a great idea. However, I'd really like to be able to mount my existing device so I have the opportunity to make a backup. Currently my data is unreachable.

Here it is step by step:

```
duck ~ # losetup /dev/loop0 /home/zach.encrypted           

duck ~ # losetup /dev/loop0

/dev/loop0: [0304]:1354592 (/home/zach.encrypted)

duck ~ # /bin/cryptsetup isLuks /dev/loop0

/dev/loop0 is not a LUKS partition

duck ~ # KEY=`openssl aes-256-ecb -d -in /home/zach.key.encrypted`       

enter aes-256-ecb decryption password:

duck ~ # echo "$KEY" | /bin/cryptsetup -c aes -h ripemd160 -s 256 create _home_zach.encrypted /dev/loop0

duck ~ # mount -o ro -t ext3 /dev/mapper/_home_zach.encrypted /home/zach

mount: wrong fs type, bad option, bad superblock on /dev/mapper/_home_zach.encrypted,

       missing codepage or other error

       In some cases useful info is found in syslog - try

       dmesg | tail  or so

duck ~ # dmesg | tail

[4294740.159000] mtrr: 0xe0000000,0x8000000 overlaps existing 0xe0000000,0x2000000

[4294740.159000] mtrr: 0xe0000000,0x8000000 overlaps existing 0xe0000000,0x2000000

[4294740.159000] agpgart: Found an AGP 2.0 compliant device at 0000:00:00.0.

[4294740.159000] agpgart: Putting AGP V2 device at 0000:00:00.0 into 1x mode

[4294740.159000] agpgart: Putting AGP V2 device at 0000:01:00.0 into 1x mode

[4294740.201000] [drm] Setting GART location based on old memory map

[4294740.201000] [drm] writeback test succeeded in 2 usecs

[4296950.936000] e1000: eth0: e1000_watchdog_task: NIC Link is Up 100 Mbps Full Duplex

[4299744.508000] VFS: Can't find ext3 filesystem on dev dm-0.

[4301226.635000] VFS: Can't find ext3 filesystem on dev dm-0.
```

Clearly something is going wrong at the decryption stage - either decrypting the key, or decrypting the block device. I suspect the latter as the content of $KEY looks exactly as you would expect - no funky high ASCII bytes which would indicate an incorrect decryption.

So why is cryptsetup failing at the crypto? I understood cryptsetup-luks is supposed to be backward compatible - they just added the luks* commands.

----------

## are

the old cryptsetup and new crypsetupLuks aren't compatible! you can't decrypt a partition with cryptsetupLuks, that is encrypted with old cryptsetup!

but you can remove cryptsetupLuks with emerge -C and reinstall cryptsetup then.

regards

are

----------

## yem

Yeah, I'm just figuring that out  :Smile:  Pretty weak really - cryptsetup-luks should either fix the incompatibility or document it. It takes the same args and is clearly supposed to be compatible so I do hope they fix it at some point..

----------

## Massimo B.

When will pam_mount come to the official portage tree?

I was still running fine pam_mount-0.12 without luks. But now I switched to pam_mount-0.17-r1 and try to convert to luks.

----------

## kwisatz_haderais

The only order of "auth lines" in system-auth that worked for me (without login asking for the password twice) is the following:

```
auth       required     pam_env.so

auth       optional     pam_mount.so

auth       sufficient   pam_unix.so likeauth nullok use_first_pass

auth       required     pam_deny.so
```

----------

## Mirza

 *kwisatz_haderais wrote:*   

> The only order of "auth lines" in system-auth that worked for me (without login asking for the password twice) is the following:
> 
> ```
> 
> auth       required     pam_env.so
> ...

 

This is finally working for me. Thanks.

Are there any possible security considerations when changing

```
auth       required   pam_unix.so likeauth nullok use_first_pass
```

to 

```
auth       sufficient   pam_unix.so likeauth nullok use_first_pass
```

I don't know what is difference between "sufficient" and "required", because I am not able to log in at all when I am using "required" for pam_unix in the above configuration.

M.

----------

## schachti

 *tuxophil wrote:*   

> 
> 
> ```
> echo $KEY | openssl aes-256-ecb > /home/frodo.key
> ```
> ...

 

How can I decrypt this key? I have a fsck error when pam_mount tries to open my encrypted home, so I have to do fsck by hand (and to do so, I have to use cryptsetup with the decrypted key).

EDIT: Solution:

```

openssl aes-256-ecb -d -in /home/frodo.key | cryptsetup create frodo /dev/loop1

```

It's not part of the man page of openssl, otherwise I would not have asked.

----------

## pillgrim

I've spent several hours trying to get this working on my box with pam_mount = 0.19, so here is howto for those who still experiences problems running pam_mount >= 0.19 with xml config files and LUKS:

1. generate key as mentionned in this topic

```
KEY=`tr -cd [:graph:] < /dev/urandom | head -c 79`
```

2. save key. When prompted for password - use your login password

```
echo $KEY | openssl aes-256-ecb > /home/ag.key
```

3. prepare file from /dev/zero (it is much faster than /dev/urandom)

```

dd if=/dev/zero of=/home/ag.img bs=1M count=10240 
```

4. attach file to a loopback device

```
losetup /dev/loop0 /home/ag.img
```

5. format it for LUKS

```
openssl aes-256-ecb -d -in /home/ag.key | cryptsetup -v -c aes -s 256 luksFormat /dev/loop0
```

6. open it 

```
openssl aes-256-ecb -d -in /home/ag.key | cryptsetup luksOpen /dev/loop0 ag
```

7. make filesystem of your choice

```
mkreiserfs /dev/mapper/ag
```

8. close it

```
cryptsetup luksClose ag
```

9. remove loopback 

```
losetup -d /dev/loop0
```

10. emege pam_mount for it

```
emerge pam_mount
```

11. setup /etc/pam.d/system-auth, mine looks like this

```

auth       required         pam_env.so

# this is important

auth       optional         pam_mount.so

auth       sufficient       pam_unix.so likeauth nullok use_first_pass

auth       required         pam_deny.so

account    required         pam_unix.so

password   required         pam_cracklib.so difok=2 minlen=8 dcredit=2 ocredit=2 retry=3

password   sufficient       pam_unix.so nullok md5 shadow use_authtok use_first_pass

password   required         pam_deny.so

session    required         pam_limits.so

session    required         pam_unix.so

# this is important too

session    optional         pam_mount.so

```

12. put the following lines into /etc/security/pam_mount.conf.xml

```

    <volume path="/home/ag.img"

            mountpoint="/home/ag2"

            fstype="crypt"

            options="loop"

            fskeypath="/home/ag.key"

            fskeycipher="aes-256-ecb" />

```

Everything worked for me

----------

## Massimo B.

After following the PAM upgrade-0.99 guide I was able to get PAM working with the new pam_mount-0.29.

Now some question about 0.29:

The ebuild brings /sbin/umount.crypt  and there I'd like to add some modifications like "$FUSER" -km "$1". But that script is just never called. /etc/security/pam_mount.conf.xml doesn't give a hint to an unmount script for crypted devices.

How can I get pam_mount working with sshd? With this PAM configuration login and su  work with pam_mount while ssh login doesn't:

```
auth       required     pam_env.so

auth       optional     pam_mount.so

auth       sufficient   pam_unix.so likeauth nullok use_first_pass

auth       required     pam_deny.so use_first_pass

account    required     pam_unix.so

password   required     pam_cracklib.so difok=2 minlen=8 dcredit=2 ocredit=2 try_first_pass retry=3

password   sufficient   pam_unix.so try_first_pass use_authtok nullok md5 shadow

password   required     pam_deny.so

session    required     pam_limits.so

session    required     pam_unix.so

session    optional     pam_mount.so
```

```
auth       required     pam_shells.so

auth       required     pam_nologin.so

auth       include      system-auth

account    include      system-auth

password   include      system-auth

session    include      system-auth
```

ssh would be useful since I use unison to synchronize home directories. unison connects via ssh and home is only mounted after user login (using pam_mount and luks). Would be fine if ssh login would do that too.

----------

## swingman

After much hassle, I sort of got this to work. It mounts nicely, and unmounts, but doesn't clean up everything.

Before login:

 *Quote:*   

> 
> 
> bf@hallucinator ~ $ ls /dev/mapper/
> 
> control  crypt-swap  crypt-tmp
> ...

 

After login:

 *Quote:*   

> 
> 
> bf@hallucinator ~ $ ls /dev/mapper/
> 
> _dev_sda7  control  crypt-swap  crypt-tmp  temporary-cryptsetup-28660
> ...

 

After logout, I get:

 *Quote:*   

> 
> 
> bf@hallucinator ~ $ ls /dev/mapper/
> 
> control  crypt-swap  crypt-tmp  temporary-cryptsetup-28660
> ...

 

/dev/mapper-cryptsetup-$$ uses /dev/loop/0 for /dev/sda7, just like /dev/mapper/_dev_sda7 does.

Since temporary-cryptsetup-$$ leaks it is impossible to remove /dev/loop/0.

I guess temparory-cryptsetup-$$ should've gone already in the login step.

How to find out what's wrong, and how to fix it?

   _

/Bjorn

----------

## swingman

More info from syslog with debug enabled in pam_mount.

 *Quote:*   

> 
> 
> Jan  6 09:53:14 hallucinator login[29502]: pam_tally(login:auth): unknown option: no_magic_root
> 
> Jan  6 09:53:14 hallucinator login[29502]: pam_mount(pam_mount.c:202) enter read_password
> ...

 

What gives?

   _

/Bjorn

----------

## swingman

Solved it. Had an old cryptsetup, with a documented race vis-a-vis HAL, and on top of that I really shouldn't have had option="loop" in pam_mount.conf.xml.

Now it's just the matter of PAM lowering priv from root too early when leaving from "su", which makes it impossible to unmount. Grr.

   _

/Bjorn

----------

## grimm26

For some reason this does not work for me.  I have followed the steps.  I have sys-auth/pam_mount-0.32 and sys-fs/cryptsetup-1.0.5-r1 and the right stuff for dmcrypt in the kernel.  I can do everyting fine manually, but I cannot get pam_mount to mount a homedir for a test user that I set up.

from /etc/security/pam_mount.conf.xml

```

<volume path="/home/protected"

mountpoint="/home/mktest"

fstype="crypt"

options="loop,cipher=aes"

fskeypath="/home/protected.key"

fskeycipher="aes-256-ecb" /> 

```

/home/protected is my encrypted loop file and I want it to mount into /home/mktest.  I can login with mktest and I see this in messages:

```
b 10 21:34:15 qa4437-11 login[14432]: pam_mount(pam_mount.c:202) enter read_password

Feb 10 21:34:19 qa4437-11 login[14432]: pam_mount(pam_mount.c:311) saving authtok for session code

Feb 10 21:34:20 qa4437-11 login[14432]: pam_unix(login:session): session opened for user mktest by (uid=0)

```

 but the loop is not created or mounted.  and then when I logout.

```

Feb 10 21:38:06 qa4437-11 login[14432]: pam_unix(login:session): session closed for user mktest

Feb 10 21:38:06 qa4437-11 login[14432]: pam_mount(pam_mount.c:134) clean system authtok (0)

```

Any ideas?

----------

## schachti

Try to set

```

<debug enable="1" />

```

in your config file to get more detailled output.

----------

## grimm26

I did.  That's how I got the messages that I did post.  I didn't see anything else from pam_mount, though.

----------

## Massimo B.

 *Massimo B. wrote:*   

> ...login and su  work with pam_mount while ssh login doesn't...

 

..according to Guschtel on the channel this should fix it:

```
#%PAM-1.0

 

auth       required     pam_env.so

auth       required     pam_unix.so try_first_pass likeauth nullok

# added

auth       sufficient   pam_mount.so use_first_pass

# end added

auth       required     pam_deny.so

 

account    required     pam_unix.so

 

password   required     pam_cracklib.so difok=2 minlen=8 dcredit=2 ocredit=2 try_first_pass retry=3

password   sufficient   pam_unix.so try_first_pass use_authtok nullok md5 shadow

password   required     pam_deny.so

 

session    required     pam_limits.so

session    required     pam_unix.so

# added

session    optional     pam_mount.so

# end added
```

```
UsePAM yes

 

## Changed 2008-02-15

## IMPORTANT for PAM_MOUNT

PasswordAuthentication yes

ChallengeResponseAuthentication no

 

--

#UsePrivilegeSeparation yes

 

## Changed 2008-02-15

## IMPORTANT for PAM_MOUNT

UsePrivilegeSeparation no

 

#PermitUserEnvironment no
```

----------

## Guschtel

When only setting "PrivilegeSeparation no" one gets this error:

```
pam_mount(pam_mount.c:460) Entered pam_mount session stage

pam_mount(pam_mount.c:481) back from global readconfig

pam_mount(pam_mount.c:483) per-user configurations not allowed by pam_mount.conf.xml

pam_mount(pam_mount.c:512) error trying to retrieve authtok from auth code

pam_mount(pam_mount.c:202) enter read_password

pam_mount(pam_mount.c:170) conv->conv(...): Conversation error

pam_mount(pam_mount.c:515) error trying to read password

pam_mount(pam_mount.c:548) done opening session (ret=7)
```

This is solved by the two other Config directives, see also pam_mount FAQ:

```
bzcat /usr/share/doc/pam_mount-0.32/faq.txt.bz2 | grep -2 PasswordAuthentication

        Since you have OpenSSH 3.9p1, you can set

        "ChallengeResponseAuthentication no" and

        "PasswordAuthentication yes" in sshd_config and it ought to

        work (with or without privsep).
```

Setting "UsePrivilegeSeparation yes" disables pam_mount for me.

----------

## PssD

hello,

maybe its trivial but changing my password took me quite some time.

somehow my /usr/bin/passwdehd doesn`t like /etc/security/pam_mount.conf.xml.

changing pam_mount.conf in the script to pam_mount.conf.xml didn`t help (is there a new version of passwdehd available?).

 *Quote:*   

> "If a user changes his login password, regenerate the efsk that was created in step 1b."

 

(pam_mount.conf.xml)

so:

 *Quote:*   

> 
> 
> openssl enc -d -aes-256-ecb -in /home/key.key -out /secure/key.txt
> 
> [old password]
> ...

 

did it.

thanks for this nice howto (will change password more often from now on  :Smile:  )

----------

## driden91

hello

after an upgrade pam_mount doesn't work :

```
Password:

pam_mount(mount.c:67): Command failed: device-mapper: reload ioctl failed: Invalid argument

pam_mount(pam_mount.c:537): mount of /dev/sda7 failed
```

with debug enable :

```
pam_mount(pam_mount.c:458): pam_mount 1.24: entering session stage

reenter password for pam_mount:

pam_mount(misc.c:38): Session open: (uid=0, euid=0, gid=1008, egid=1008)

pam_mount(mount.c:172): Mount info: globalconf, user=driden91 <volume fstype="crypt" server="(null)" path="/dev/sda7" mountpoint="/home/driden91" cipher="aes-256-ecb" fskeypath="/home/driden91.key" fskeycipher="aes-256-ecb" fskeyhash="(null)" options="cipher=aes" /> fstab=0

command: [mount] [-t] [crypt] [-o cipher=aes] [/dev/sda7] [/home/driden91]

pam_mount(misc.c:38): set_myuid<pre>: (uid=0, euid=0, gid=1008, egid=1008)

pam_mount(misc.c:38): set_myuid<post>: (uid=0, euid=0, gid=1008, egid=1008)

Password:

pam_mount(mount.c:64): mount messages:

pam_mount(mount.c:67): Command failed: device-mapper: reload ioctl failed: Invalid argument

Filesystem    Type   1K-blocks      Used Available Use% Mounted on

rootfs      rootfs     1036124    868324    167800  84% /

/dev/root reiserfs     1036124    868324    167800  84% /

proc          proc           0         0         0   -  /proc

rc-svcdir    tmpfs        1024        68       956   7% /lib64/rc/init.d

sysfs        sysfs           0         0         0   -  /sys

udev         tmpfs       10240       120     10120   2% /dev

devpts      devpts           0         0         0   -  /dev/pts

shm          tmpfs     1029408         0   1029408   0% /dev/shm

/dev/sda5 reiserfs     8393656   8206584    187072  98% /usr

/dev/sda6 reiserfs     2104376    213788   1890588  11% /var

usbfs        usbfs           0         0         0   -  /proc/bus/usb

pam_mount(pam_mount.c:537): mount of /dev/sda7 failed

command: [pmvarrun] [-u] [driden91] [-o] [1]

pam_mount(misc.c:38): set_myuid<pre>: (uid=0, euid=0, gid=1008, egid=1008)

pam_mount(misc.c:38): set_myuid<post>: (uid=0, euid=0, gid=1008, egid=1008)

pmvarrun(pmvarrun.c:248): parsed count value 0

pam_mount(pam_mount.c:418): pmvarrun says login count is 1

pam_mount(pam_mount.c:548): done opening session (ret=0)
```

what is the problem ?

regards

----------

## mattst88

I just got this error.

```
bad pad on end of encrypted file
```

And I fortunately just figured it out. I was encrypting my keyfile with aes-256-cbc; encrypting it with aes-256-ecb allows it to work properly. I googled and saw this error in a couple places with no solution, so I thought I'd be nice to post the solution somewhere.

----------

## Havin_it

Apologies if someone has already mentioned this, but although it's no longer supported, a simple-ish hack can enable TrueCrypt to be used with this too. Here's my /etc/security/pam_mount.conf.xml, with "mememe" as the user:

```
<?xml version="1.0" encoding="utf-8" ?>

<!DOCTYPE pam_mount SYSTEM "pam_mount.conf.xml.dtd">

<!--

        See pam_mount.conf(5) for a description.

-->

<pam_mount>

                <!-- debug should come before everything else,

                since this file is still processed in a single pass

                from top-to-bottom -->

<debug enable="0" />

                <!-- Volume definitions -->

<cryptmount>tc-nl --text --protect-hidden=no --keyfiles="" %(VOLUME) %(MNTPT)</cryptmount>

<cryptumount>truecrypt --text -d</cryptumount>

                <!-- pam_mount parameters: General tunables -->

<!--

<luserconf name=".pam_mount.conf.xml" />

-->

<!-- Note that commenting out mntoptions will give you the defaults.

     You will need to explicitly initialize it with the empty string

     to reset the defaults to nothing. -->

<mntoptions allow="nosuid,nodev,loop,encryption,fsck,nonempty,allow_root,allow_other" />

<!--

<mntoptions deny="suid,dev" />

<mntoptions allow="*" />

<mntoptions deny="*" />

-->

<mntoptions require="nosuid,nodev" />

<path>/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin</path>

<logout wait="0" hup="0" term="0" kill="0" />

                <!-- pam_mount parameters: Volume-related -->

<mkmountpoint enable="0" remove="false" />

<volume user="mememe" fstype="crypt" path="/path/to/truecrypt-image" mountpoint="/home/%(USER)" />

</pam_mount>
```

You can change the truecrypt options as desired in the <cryptmount> element. You also need to create the script here called "tc-nl" somewhere in your path - this is because of something truecrypt does with newlines that pam_mount doesn't like (this is not my work, and I lost the source :shrug: )

The tc-nl script is very simple:

```
#!/bin/sh

# Helper for truecrypt via pam_mount

(cat; echo)| truecrypt $*
```

dm-crypt is better to use in many ways, but I have a dual-boot setup and while Windows tools do exist for dm-crypt/luks mounting, they are nowhere near as robust as TrueCrypt. Hope this helps someone.

----------

