Skip to contentSkip to conversation

dmsetup: improve message command

PATCH In review Werner Koch 2 participants 5 messages Feb 26, 2016 – Mar 21, 2016 Device mapper

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 dmsetup: improve message command

Hi!

I am playing with a new crypto container format and propose to enhance "dmsetup message" to accept the actual message from stdin instead of taking it only from the command line. This is useful to set a key and similar to how "dmsetup create" is reading the table from stdin.

Patch attached.

Shalom-Salam,

Werner

Signature · 1 line
Die Gedanken sind frei.  Ausnahmen regelt ein Bundesgesetz.

From ae2b5739007b0829c3a142d0d5e782b3b7fe3028 Mon Sep 17 00:00:00 2001
From: Werner Koch <[email protected]>
Date: Fri, 19 Feb 2016 21:47:45 +0100
Subject: [PATCH] dmsetup: command message may now read the message from stdin.

When "dmsetup messsage" is used to set an encryption key it is better to read that from stdin so that the key does not show up in ps or the shell history. Thus instead of an error message when the third arg is missing, it is now taken from stdin. stdin is also switched to unbuffered mode so that sensitive data should not end up in stdio buffers. A new wipememory macro (taken from GnuPG) is used to securely erase the message from memory.

---
 man/dmsetup.8.in |  5 +++--
 tools/dmsetup.c  | 68 ++++++++++++++++++++++++++++++++++++++++++--------------
 2 files changed, 54 insertions(+), 19 deletions(-)
man/dmsetup.8.in +3 -2
index f92dbe5..c9c886a 100644
@@ -127,7 +127,7 @@ dmsetup \(em low level logical volume management
 .  BR message
 .  IR device_name
 .  IR sector
-.  IR message
+.  RI [ message ]
 ..
 .CMD_MESSAGE
 .
@@ -714,7 +714,8 @@ reactivating it with proper mangling mode used (see also \fB\-\-manglename\fP).
 .HP
 .CMD_MESSAGE
 .br
-Send message to target. If sector not needed use 0.
+Send message to target. If sector not needed use 0.  If the message
+argument is not given, the first line read from stdin is used.
 .
 .HP
 .CMD_MKNODES
tools/dmsetup.c +51 -17

89 lines not on this page · raw message

Signature · 1 line
2.1.4
#1 lore raw
Quoted 3 lines · On Fri, 26 Feb 2016 12:42, [email protected] said:

I am playing with a new crypto container format and propose to enhance "dmsetup message" to accept the actual message from stdin instead of taking it only from the command line. This is useful to set a key and

Is there anything I can do to help you evaluate the patch?

Salam-Shalom,

Werner

Signature · 1 line
Die Gedanken sind frei.  Ausnahmen regelt ein Bundesgesetz.

From ae2b5739007b0829c3a142d0d5e782b3b7fe3028 Mon Sep 17 00:00:00 2001
From: Werner Koch <[email protected]>
Date: Fri, 19 Feb 2016 21:47:45 +0100
Subject: [PATCH] dmsetup: command message may now read the message from stdin.

When "dmsetup messsage" is used to set an encryption key it is better to read that from stdin so that the key does not show up in ps or the shell history. Thus instead of an error message when the third arg is missing, it is now taken from stdin. stdin is also switched to unbuffered mode so that sensitive data should not end up in stdio buffers. A new wipememory macro (taken from GnuPG) is used to securely erase the message from memory.

---
 man/dmsetup.8.in |  5 +++--
 tools/dmsetup.c  | 68 ++++++++++++++++++++++++++++++++++++++++++--------------
 2 files changed, 54 insertions(+), 19 deletions(-)
man/dmsetup.8.in +3 -2
index f92dbe5..c9c886a 100644
@@ -127,7 +127,7 @@ dmsetup \(em low level logical volume management
 .  BR message
 .  IR device_name
 .  IR sector
-.  IR message
+.  RI [ message ]
 ..
 .CMD_MESSAGE
 .
@@ -714,7 +714,8 @@ reactivating it with proper mangling mode used (see also \fB\-\-manglename\fP).
 .HP
 .CMD_MESSAGE
 .br
-Send message to target. If sector not needed use 0.
+Send message to target. If sector not needed use 0.  If the message
+argument is not given, the first line read from stdin is used.
 .
 .HP
 .CMD_MKNODES
tools/dmsetup.c +51 -17

89 lines not on this page · raw message

Signature · 1 line
2.1.4
#2 lore raw

Dne 18.3.2016 v 12:06 Werner Koch napsal(a):

Quoted 8 lines · On Fri, 26 Feb 2016 12:42, [email protected] said:
Quoted 4 lines

I am playing with a new crypto container format and propose to enhance "dmsetup message" to accept the actual message from stdin instead of taking it only from the command line. This is useful to set a key and

Is there anything I can do to help you evaluate the patch?

Hi

It looked usable - thought could you trim down the zeroing of dm-malloced area (wipememory) macro.

It's useless for heap allocation.

Unless you show example and compiler which would optimize 'library' call away.
a) such compiler would be horrible broken (since I could always LD_PRELOAD my free() implementation),
b) we would need to do same for dm_task_struct - since your 'dmsetup mem is duplicated for dm_task.

Regards

Zdenek

#3 lore raw
Quoted 2 lines · On Fri, 18 Mar 2016 12:52, [email protected] said:

It looked usable - thought could you trim down the zeroing of dm-malloced area (wipememory) macro.

Actually the patch zeroes the given string (from command line or stdin). That is in general pretty short and given that this is a command line tool there won't be any noticable speed penality.

Quoted 1 line

It's useless for heap allocation.

Sorry, I have to dissent: If dmsetup is swapped out the key would end up in the swap (which is still not encrypted by default on Linux). free() does not zero out the memory and thus another malloc may reveal the key. Thus it is important to zeroise all sensitive data before a free.

Quoted 3 lines

Unless you show example and compiler which would optimize 'library' call away.
a) such compiler would be horrible broken (since I could always LD_PRELOAD my free() implementation),

It is not about removing a library call but about optimizing compilers which may remove a plain memset if they can deduce that the memory is not used after that memset call. Can you exclude that it will never happen that a somehow attributed free() will be detected by a future gcc/clang version to elimination - what they call - dead code?

The current discussion is to provide a memset_s function which other platforms already have, for example see:

http://www.openwall.com/lists/musl/2015/09/09/5

The wipememory macro we use in GnuPG is currently the most portable solution. There are way faster implementations, for example what we use in Libgcrypt, but that is overkill and frankly for Linux we should wait for a memset_s.

Quoted 2 lines

b) we would need to do same for dm_task_struct - since your 'dmsetup mem is duplicated for dm_task.

Right. However a full audit of sensitive code paths in dmsetup was out of my scope. I merely did what can be expected for new code.

Shalom-Salam,

Werner

Signature · 1 line
Die Gedanken sind frei.  Ausnahmen regelt ein Bundesgesetz.
#4 lore raw

Dne 21.3.2016 v 11:55 Werner Koch napsal(a):

Quoted 36 lines · On Fri, 18 Mar 2016 12:52, [email protected] said:
Quoted 3 lines

It looked usable - thought could you trim down the zeroing of dm-malloced area (wipememory) macro.

Actually the patch zeroes the given string (from command line or stdin). That is in general pretty short and given that this is a command line tool there won't be any noticable speed penality.

Quoted 1 line

It's useless for heap allocation.

Sorry, I have to dissent: If dmsetup is swapped out the key would end up in the swap (which is still not encrypted by default on Linux). free() does not zero out the memory and thus another malloc may reveal the key. Thus it is important to zeroise all sensitive data before a free.

Quoted 3 lines

Unless you show example and compiler which would optimize 'library' call away.
a) such compiler would be horrible broken (since I could always LD_PRELOAD my free() implementation),

It is not about removing a library call but about optimizing compilers which may remove a plain memset if they can deduce that the memory is not used after that memset call. Can you exclude that it will never happen that a somehow attributed free() will be detected by a future gcc/clang version to elimination - what they call - dead code?

The current discussion is to provide a memset_s function which other platforms already have, for example see:

   http://www.openwall.com/lists/musl/2015/09/09/5

The wipememory macro we use in GnuPG is currently the most portable solution. There are way faster implementations, for example what we use in Libgcrypt, but that is overkill and frankly for Linux we should wait for a memset_s.

Quoted 5 lines
Quoted 2 lines

b) we would need to do same for dm_task_struct - since your 'dmsetup mem is duplicated for dm_task.

Right. However a full audit of sensitive code paths in dmsetup was out of my scope. I merely did what can be expected for new code.

I've probably not been clear enough.
As we normally 'clear' i.e. password with memset().

It's about extra zeroing with wipememory() macro.

I'm still convinced compiler CANNOT 'drop' memset() before calling 'free()'
it CANNOT optimize this away for heap allocation (it can do it for on stack
buffer).

It's purely about consistency - we can't do things randomly across code base. It's either a) or b) everywhere (dmsetup, libdm,....)

So please either use plain memset(0)in your patch (like we user everywhere else) or prove us we need to use weird macros for clearing heap memory as it seems to be serious issue.

Regards

Zdenek