Loopback Tricks

Some filesystems have very limited capabilities. In particular, one will find:

Warning

Unfortunately, the Network Security Toolkit probe can't do much about the risk with writing to NTFS file systems. I just wouldn't recommend writing to these systems period (maybe there will be a point in the future where NTFS write support will be considered risk free - but we aren't there yet). So, if your only available disk space lies on a NTFS partition, it is recommended that you skip the remainder of this section.

These limitations are not typically a big issue when transferring files. However, if one wants to make use of the space on one of these limited file systems, they will often run across permission and/or ownership errors that would not be encountered on a ext3 file system. For example, if the directory DIR lies on one of these limited file systems, one will find that invoking the setup_mysql -rdir DIR will fail because of ownership and permission issues. Hence you won't be able to keep a permanent copy of your SQL databases on one of these limited file systems.

However, if the root user can create and write to a single file on any of these limited file systems, we can treat that file as its own partition through the magic of the Linux loop back device.

Note

Much of the following information was gleaned through the result of a Google search that found the Backup ideas web page.

In the following example, I'm going to mount my USB thumb drive to /mnt/limited and create and prepare a 20MB file called nst.lp that will be used to emulate a full ext3 file system (even though it resides on a FAT file system).


At this point, we now have the 20MB file /mnt/limited/nst.lp prepared such that we can mount it like a ext3 file system via the loop back device. We will now create a new mount point and verify that the standard file operations are supported. Notice how the noatime option was added (this prevents updates to the file system each time a file is accessed - flash drives have a limited number of writes).


By using the loop back trick, I was able to let mysql make use of the available space on my USB thumb drive (and thus save a permanent copy of my snort logs).

Note

While the loop back trick is handy in a pinch, it should not be used in a situation where you need every ounce of performance that you can get. There is a significant amount of overhead involved when using a loop back device.

You can make use of the loop option on the mount command to mount a ISO image and access the files it contains.

[root@probe root]# mkdir /mnt/iso
[root@probe root]# mount -t iso9660 -o loop,ro /tmp/nst-1.0.5.iso /mnt/iso
[root@probe root]# mount
/dev/hda2 on / type ext3 (rw)
none on /proc type proc (rw)
usbdevfs on /proc/bus/usb type usbdevfs (rw)
none on /dev/pts type devpts (rw,gid=5,mode=620)
none on /dev/shm type tmpfs (rw)
/tmp/nst-1.0.5.iso on /mnt/iso type iso9660 (ro,loop=/dev/loop0)
[root@probe root]# losetup /dev/loop0
/dev/loop0: [0302]:213066 (/tmp/nst-1.0.5.iso) offset 0, no encryption
[root@probe root]# ls /mnt/iso
etc  isolinux  local  modules  README  README.html  utils
[root@probe root]# gzip -dc /mnt/iso/isolinux/initrdr9.gz > /tmp/initrd
[root@probe root]# umount /mnt/iso
[root@probe root]# 

Here is another way of mounting an ISO image by manually chosing the loop device with the losetup command.

[root@probe root]# mkdir /mnt/iso
[root@probe root]# losetup /dev/loop4 /tmp/nst-1.0.5.iso
[root@probe root]# mount -t iso9660 -o ro /dev/loop4 /mnt/iso
[root@probe root]# mount
/dev/hda2 on / type ext3 (rw)
none on /proc type proc (rw)
usbdevfs on /proc/bus/usb type usbdevfs (rw)
none on /dev/pts type devpts (rw,gid=5,mode=620)
none on /dev/shm type tmpfs (rw)
/dev/loop4 on /mnt/iso type iso9660 (ro)
[root@probe root]# losetup /dev/loop4
/dev/loop4: [0302]:213066 (/tmp/nst-1.0.5.iso) offset 0, no encryption
[root@probe root]# ls /mnt/iso
etc  isolinux  local  modules  README  README.html  utils
[root@probe root]# gzip -dc /mnt/iso/isolinux/initrdr9.gz > /tmp/initrd
[root@probe root]# umount /mnt/iso
[root@probe root]# losetup -d /dev/loop4
[root@probe root]# 

Most Linux distributions will boot and load files from a "initial RAM disk". This "initial RAM disk" is a single file containing a collection of files. The following demonstrates how one can make use of the loop back device to mount a initial RAM disk image. In this example, we will use the initial RAM disk image which we extracted from the Network Security Toolkit ISO image (shown in Mounting a ISO Image).

[root@probe root]# mkdir /mnt/initrd
[root@probe root]# mount -t ext2 -o loop /tmp/initrd /mnt/initrd
[root@probe root]# ls /mnt/initrd
bin   dev  home    lib         mnt  proc  sbin      tmp  var
boot  etc  initrd  lost+found  opt  root  tftpboot  usr
[root@probe root]# umount /mnt/initrd
[root@probe root]# 

Note

Notice that ext2 was specified as the file system for the /tmp/initrd file. It may be possible to create initial RAM disks using alternative filesystem formats (so you may need to adjust this value for your situation).

Mounting a file as a encrypted filesystem is very similar as to what was discussed in the previous section (Mounting A File As A Filesystem). The following steps need to be done:

  • The cryptography modules and loop back modules need to be available on the system. These come standard on a Network Security Toolkit distribution.

  • A file needs to be created and initialized with a random pattern. It takes longer to initialize a file in this manner, but it provides better security than zero filling the file.

  • The file needs to be initialized with a password through the losetup utility.

  • We will then need to format the file system.

Note

For additional details on encrypted filesystems, refer to: Using CryptoAPI, Cryptoloop HOWTO, and How to Configure an Encrypted Loopback Filesystem on Red Hat Linux 8.0.

In the following example, I'm going to mount a SMB filesystem (a folder "shared" by a Windows desktop computer) to store my encrypted filesystem on. This will be mounted to /mnt/smb. I will then create and prepare a 20MB file called crypt.ext3 that will be used to emulate a fully encrypted ext3 file system.


1

Filling a file with a random data stream from /dev/urandom can take a VERY long time - especially over a slow network link. You may be better off to create it on a local hard disk or RAM disk and then copy the file to your network drive.

2

This step assigns the encryption key (based on a hash from the password you provide). Do NOT forget this password as there is no way I know of to recover it (or your files) once forgotten.

3

This disconnects the file from the loopback device. We will need to mount it in the future when we are ready to make use of it.

At this point, we now have the 20MB file /mnt/smb/tmp/crypt.ext3 prepared such that we can mount it like a encrypted ext3 file system via the loop back device. We will now create a new mount point and verify that the standard file operations are supported.


By using the loop back trick, I was able to let mysql make use of the available space on a shared Windows drive and keep the data encrypted (if someone were able to get hold of the file - they would have a tough time getting usable information from it).

Warning

Do not forget the password you used to encrypt your file system with. As far as I know, there is no way to recover data from a encrypted file system once the password has been lost.