dknewkey with ed25519 insecure chmod call / race condition

Bug #2017430 reported by Hanno Böck
256
This bug affects 1 person
Affects Status Importance Assigned to Milestone
dkimpy
Fix Released
Medium
Scott Kitterman
1.0
Fix Released
Medium
Scott Kitterman
1.1
Fix Released
Medium
Scott Kitterman

Bug Description

When generating an ed25519 key with dknewkey a race condition occurs with the file permission.

The problem is in this code:
```
    priv_key = skg.generate()
    with open(private_key_file, 'w') as pkf:
        pkf.write(priv_key.encode(encoder=nacl.encoding.Base64Encoder).decode(">
    if os.name == 'posix':
        os.chmod(private_key_file, 0o600)
    return(priv_key)
```

The chmod is only called after the key is written to the file.

This could be exploited on a multiuser system if keys are generated according to a known scheme. You can test this with fpracer, a proof of concept for such vulnerabilities:
https://github.com/hannob/fpracer

With an unprivileged user account run:
```
./fpracer /etc/dkim/example.com.key
```

With the root account run:
```
mkdir /etc/dkim
cd /etc/dkim
dknewkey --ktype ed25519 example.com
```

To avoid such races it is necessary to already create the file with secure permissions. This can be done for example via os.umask. I have attached a patch.

Revision history for this message
Hanno Böck (hanno-hboeck) wrote :
Revision history for this message
Scott Kitterman (kitterman) wrote :

Thanks. I agree this is an issue, but I don't think it's super urgent as it takes local access and knowing what the file name will be, in addition to knowing when. I'm going to give it a week and see if any other bugs pop up and then do a release to fix this and anything else.

Changed in dkimpy:
importance: Undecided → Medium
milestone: none → 1.1.3
status: New → Confirmed
Changed in dkimpy:
assignee: nobody → Scott Kitterman (kitterman)
Changed in dkimpy:
status: Confirmed → Fix Released
information type: Private Security → Public Security
To post a comment you must log in.
This report contains Public Security information  
Everyone can see this security related information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.