From: Randy Witt <randy.e.witt@linux.intel.com>
To: Joshua Watt <jpewhacker@gmail.com>,
"Burton, Ross" <ross.burton@intel.com>
Cc: OE-core <openembedded-core@lists.openembedded.org>
Subject: Re: [PATCH] openssh: Atomically generate host keys
Date: Tue, 23 May 2017 10:23:25 -0700 [thread overview]
Message-ID: <09a43b20-0aa0-beed-f019-762bb23d0a79@linux.intel.com> (raw)
In-Reply-To: <CAJdd5GZeT8h7qXfKF=XM7Q0kKoMgQSxNprqhWz7wYv_vBquzTQ@mail.gmail.com>
On 05/23/2017 08:29 AM, Joshua Watt wrote:
> On Tue, May 23, 2017 at 9:37 AM, Burton, Ross <ross.burton@intel.com> wrote:
>>
>> On 7 May 2017 at 02:33, Joshua Watt <jpewhacker@gmail.com> wrote:
>>>
>>> +if [ ! -f "$NAME" ]; then
>>> + echo " generating ssh $TYPE key..."
>>> + ssh-keygen -q -f "${NAME}.tmp" -N '' -t $TYPE
>>> +
>>> + # Sync to ensure data is written to temp file before renaming
>>> + sync
>>> +
>>> + # Move (Atomically rename) files
>>> + # Rename the .pub file first, since the check that triggers a
>>> + # key generation is based on the private file.
>>> + mv -f "${NAME}.tmp.pub" "${NAME}.pub"
>>> + sync
>>> +
>>> + mv -f "${NAME}.tmp" "${NAME}"
>>> + sync
>>> +fi
>>>
>>
>> All of these syncs seem quite enthusiastic, are they really needed? Writing
>> the file to a temporary name and then mving it to the real name should
>> result in either no file or a complete file in the event of power loss,
>> surely?
>
> My understanding (and observation) of most journal file systems is
> that only metadata (i.e. directory entries and such) are journaled in
> typical usage. The first sync is necessary in this case to ensure that
> the actual file data gets put on the disk before we rename the files,
> otherwise in the event of interruption journaled rename might get
> replayed but have garbage data. The second one is more of a "force
> operation order" sync to make sure the public file is written before
> the private one, as a reordering would cause problems. The last sync
> is the most optional, but I've seen it take minutes for disk to sync
> contents if no one calls "sync", so it is very possible that all our
> work of regenerating keys would be for naught if power is interrupted
> in the meantime.
>
> I think some of these syncs can be removed. Namely, the first and
> third one. The second one needs to be there, but can server double
> duty of syncing data to disk and enforcing the order between the
> public and private rename. It does mean we could get a state where the
> public key exists but is garbage, but this should be OK because the
> private key won't exist and it would be regenerated.
>
> The third sync can be removed and I can put one final sync at the end
> after all keys are generated to ensure we won't go through all the
> effort of regenerating the (last) key again in the event of
> interruption shortly after, unless you would prefer I didn't.
>
The typical convention for this is,
1. Update file data.
2. sync file
3. sync containing directory
4. mv file to new location
5. sync directory containing new file (although I've seen this step left out before)
https://lwn.net/Articles/457672/ is a good example which is linked from
https://lwn.net/Articles/457667/
But one of the important parts vs your code, is also that the example is only
calling sync on the files/directory needed, vs calling "sync" with no arguments
which is going to cause all data in all filesystem caches to be flushed.
>>
>>>
>>> diff --git a/meta/recipes-connectivity/openssh/openssh/sshdgenkeys.service
>>> b/meta/recipes-connectivity/openssh/openssh/sshdgenkeys.service
>>> index 148e6ad..af56404 100644
>>> --- a/meta/recipes-connectivity/openssh/openssh/sshdgenkeys.service
>>> +++ b/meta/recipes-connectivity/openssh/openssh/sshdgenkeys.service
>>> @@ -1,22 +1,14 @@
>>> [Unit]
>>> Description=OpenSSH Key Generation
>>> RequiresMountsFor=/var /run
>>> -ConditionPathExists=!/var/run/ssh/ssh_host_rsa_key
>>> -ConditionPathExists=!/var/run/ssh/ssh_host_dsa_key
>>> -ConditionPathExists=!/var/run/ssh/ssh_host_ecdsa_key
>>> -ConditionPathExists=!/var/run/ssh/ssh_host_ed25519_key
>>> -ConditionPathExists=!/etc/ssh/ssh_host_rsa_key
>>> -ConditionPathExists=!/etc/ssh/ssh_host_dsa_key
>>> -ConditionPathExists=!/etc/ssh/ssh_host_ecdsa_key
>>> -ConditionPathExists=!/etc/ssh/ssh_host_ed25519_key
>>
>>
>> Can you not continue to use ConditionPathExists to only run this unit if it
>> needs to run? You can prepend the argument with | to make them logical OR
>> instead of logical AND, if I'm reading this documentation correctly.
>
> I will try that. I wasn't aware that was an option since systemd conf
> files are somewhat new to me.
>
>>
>> Ross
next prev parent reply other threads:[~2017-05-23 17:23 UTC|newest]
Thread overview: 34+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-05-07 1:33 [PATCH] openssh: Atomically generate host keys Joshua Watt
2017-05-09 2:24 ` (No subject) Joshua Watt
2017-05-09 2:24 ` [PATCH v2] openssh: Atomically generate host keys Joshua Watt
2017-05-22 13:08 ` [PATCH] " Joshua Watt
2017-05-23 14:37 ` Burton, Ross
2017-05-23 15:29 ` Joshua Watt
2017-05-23 17:23 ` Randy Witt [this message]
2017-05-23 17:56 ` Joshua Watt
2017-05-23 19:56 ` Joshua Watt
2017-05-24 10:03 ` Peter Kjellerstedt
2017-05-24 13:38 ` Joshua Watt
2017-05-25 2:17 ` [meta-oe][PATCH v3] " Joshua Watt
2017-05-25 9:21 ` Ian Arkver
2017-05-26 1:52 ` [meta-oe][PATCH v4] " Joshua Watt
2017-05-26 18:02 ` Andre McCurdy
2017-05-26 1:54 ` [meta-oe][PATCH v5] " Joshua Watt
2017-05-26 13:33 ` Leonardo Sandoval
2017-05-26 13:33 ` Joshua Watt
2017-05-31 2:34 ` [PATCH v6] " Joshua Watt
2017-05-31 2:45 ` Otavio Salvador
2017-06-01 3:05 ` [PATCH v7] " Joshua Watt
2017-06-07 3:30 ` Joshua Watt
2017-06-12 12:25 ` Joshua Watt
2017-06-12 12:25 ` Joshua Watt
2017-06-14 3:31 ` [PATCH v8] " Joshua Watt
2017-06-14 3:38 ` Joshua Watt
2017-06-14 3:55 ` [PATCH v9] " Joshua Watt
2017-07-13 12:15 ` [PATCH v10] " Joshua Watt
2017-09-28 13:40 ` [PATCH v11] " Joshua Watt
2017-06-20 8:52 ` [PATCH] " Ulrich Ölmann
2017-06-20 14:07 ` Joshua Watt
2017-05-25 2:31 ` ✗ patchtest: failure for openssh: Atomically generate host keys (rev3) Patchwork
2017-05-26 2:01 ` ✗ patchtest: failure for openssh: Atomically generate host keys (rev4) Patchwork
2017-07-13 12:31 ` ✗ patchtest: failure for openssh: Atomically generate host keys (rev10) Patchwork
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=09a43b20-0aa0-beed-f019-762bb23d0a79@linux.intel.com \
--to=randy.e.witt@linux.intel.com \
--cc=jpewhacker@gmail.com \
--cc=openembedded-core@lists.openembedded.org \
--cc=ross.burton@intel.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox