Skip to content

Conversation

@Jimbo4350
Copy link
Contributor

@Jimbo4350 Jimbo4350 commented Oct 8, 2020

Since we are now checking that the VRF signing key file has the correct owner read/write permissions, it makes sense to create the VRF signing key file with the correct permissions in cardano-cli.

@Jimbo4350 Jimbo4350 force-pushed the jordan/create-vrf-with-correct-permissions branch 5 times, most recently from dc6079d to 76d29ff Compare October 16, 2020 10:30
@Jimbo4350 Jimbo4350 marked this pull request as ready for review October 16, 2020 10:30
@Jimbo4350 Jimbo4350 force-pushed the jordan/create-vrf-with-correct-permissions branch 2 times, most recently from d3f166c to a62fc94 Compare October 16, 2020 12:07
@rvl
Copy link
Contributor

rvl commented Oct 20, 2020

Yes please! I found that with version 1.21.2, cardano-node checks permissions, but cardano-cli doesn't set those required permissions.

It might be easier in general if cardano-cli just set a restricted umask on startup. See setFileCreationMask.

Also to be able to use functions from the unix package, without infecting everything with CPP, there are some tricks. Either:

  1. Use a facade module like this: cardano-wallet-launcher.cabal. The implementation module for Cardano.Startup.POSIX uses functions from the unix library. The implementation module for Cardano.Startup.Windows has no-op functions. The module Cardano.Startup re-exports one or the other using a single instance of #ifdef WINDOWS CPP.
  2. Put the same module in multiple cabal source dirs like this:
    if os(windows)
      build-depends: Win32
      hs-source-dirs:
        src-windows
      cpp-options:   -DWINDOWS
    else
      build-depends: unix
      hs-source-dirs:
        src-posix
    

Copy link
Contributor

@dcoutts dcoutts left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Basic idea is fine.

As far as I can see the owner permission stuff is completely portable, and not unix-specific at all. And there's no reason for us to want to do it on one platform and not another.

I think it would be reasonable to add writeTextEnvelopeFileWithOwnerPermissions to the API itself. We can also simplify the code by having a separate (not exported) writeFileWithOwnerPermissions. That would more clearly separate the concerns. Then writeTextEnvelopeFileWithOwnerPermissions would be implemented like writeTextEnvelopeFile but calling writeFileWithOwnerPermissions rather than writeFile.

Why is it worth adding this function to the API so it can be used for any TextEnvelope format file? Because actually we probably want to use it everywhere that we write secret/signing keys. The auditors asked us to do this for VRF but really it applies universally.

@Jimbo4350 Jimbo4350 force-pushed the jordan/create-vrf-with-correct-permissions branch from a62fc94 to 86becdb Compare October 21, 2020 14:29
@rvl
Copy link
Contributor

rvl commented Oct 21, 2020

@dcoutts Do you know, is there a cross-platform way of removing non-owner read permissions, if the file already exists?

I couldn't find any helpful functions in System.Directory. Testing in ghci showed me that these functions only touch the owner permissions. I found a user posted to haskell-cafe more than 10 years ago noting the same.

@Jimbo4350 Jimbo4350 force-pushed the jordan/create-vrf-with-correct-permissions branch 2 times, most recently from 8c621f9 to 0208598 Compare October 22, 2020 08:39
@dcoutts
Copy link
Contributor

dcoutts commented Oct 22, 2020

@rvl no I don't think there is. Sadly the file permission models are totally different between unix and windows, and indeed between classic unix and unix with extended permissions.

The solution is to create the files with the correct permissions in the first place.

@Jimbo4350 Jimbo4350 force-pushed the jordan/create-vrf-with-correct-permissions branch 5 times, most recently from c390bbf to 350bf60 Compare October 22, 2020 16:02
Copy link
Contributor

@dcoutts dcoutts left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks much better. Nearly there.

Once the last bits are done I think we can also squash down to one patch.

Comment on lines +25 to +27
#ifdef UNIX
_ <- setFileCreationMask (otherModes `unionFileModes` groupModes)
#endif
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do we need this for? The openTempFile makes files with the right permissions irrespective of the default file creation mask doesn't it?

Copy link
Contributor Author

@Jimbo4350 Jimbo4350 Oct 23, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I run a bash script that uses cardano-cli, the file appears to be created with the incorrect permissions. I either have to put umask 077 at the top of my script or use setFileCreationMask in the cardano-cli exec.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I cannot reproduce this.

Irrespective of the initial umask, the file is created without "other" or "group" getting any permissions. The only time I can get the umask to affect things is if I set it to a nonsensical value like 0777 which prevents even the owner from having any permissions. But this is not a case we need to deal with.

That said, irrespective of whether it's needed or not, it may not be a bad idea to override the umask anyway and use 0077 for all files the cli creates, since arguably all the files the cli makes should be readable only to the user creating them.

@disassembler any opinion?

(left $ GenericPermissionsExist vrfPrivKey)
where
genericPermissions = gENERIC_ALL .|. gENERIC_READ .|. gENERIC_WRITE .|. gENERIC_EXECUTE
hasPermission fModeA fModeB = fModeA .&. fModeB /= gENERIC_NONE
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice.

@Jimbo4350 Jimbo4350 force-pushed the jordan/create-vrf-with-correct-permissions branch 4 times, most recently from ca0536c to 441fafa Compare October 26, 2020 10:08
@Jimbo4350 Jimbo4350 force-pushed the jordan/create-vrf-with-correct-permissions branch from 441fafa to f70dade Compare October 26, 2020 16:39
@Jimbo4350
Copy link
Contributor Author

Yes please! I found that with version 1.21.2, cardano-node checks permissions, but cardano-cli doesn't set those required permissions.

It might be easier in general if cardano-cli just set a restricted umask on startup. See setFileCreationMask.

Also to be able to use functions from the unix package, without infecting everything with CPP, there are some tricks. Either:

1. Use a facade module like this: [`cardano-wallet-launcher.cabal`](https://github.com/input-output-hk/cardano-wallet/blob/rvl/cardano-node-1.21.2/lib/launcher/cardano-wallet-launcher.cabal#L50-L57). The implementation module for `Cardano.Startup.POSIX` uses functions from the unix library. The implementation module for `Cardano.Startup.Windows` has no-op functions. The module `Cardano.Startup` re-exports one or the other using a single instance of `#ifdef WINDOWS` CPP.

2. Put the same module in multiple cabal source dirs like this:
   ```
   if os(windows)
     build-depends: Win32
     hs-source-dirs:
       src-windows
     cpp-options:   -DWINDOWS
   else
     build-depends: unix
     hs-source-dirs:
       src-posix
   ```

Thanks for the suggestion! Let's get this PR in and then I can look into what you've described.

Comment on lines +25 to +27
#ifdef UNIX
_ <- setFileCreationMask (otherModes `unionFileModes` groupModes)
#endif
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I cannot reproduce this.

Irrespective of the initial umask, the file is created without "other" or "group" getting any permissions. The only time I can get the umask to affect things is if I set it to a nonsensical value like 0777 which prevents even the owner from having any permissions. But this is not a case we need to deal with.

That said, irrespective of whether it's needed or not, it may not be a bad idea to override the umask anyway and use 0077 for all files the cli creates, since arguably all the files the cli makes should be readable only to the user creating them.

@disassembler any opinion?

@Jimbo4350 Jimbo4350 force-pushed the jordan/create-vrf-with-correct-permissions branch from f70dade to b6b88cc Compare October 28, 2020 10:37
@dcoutts
Copy link
Contributor

dcoutts commented Oct 28, 2020

bors merge

iohk-bors bot added a commit that referenced this pull request Oct 28, 2020
1948: Create VRF signing key file with correct permissions r=dcoutts a=Jimbo4350

Since we are now checking that the VRF signing key file has the correct owner read/write permissions, it makes sense to create the VRF signing key file with the correct permissions in `cardano-cli`.
- Related to #1936 


Co-authored-by: Jordan Millar <jordan.millar@iohk.io>
@iohk-bors
Copy link
Contributor

iohk-bors bot commented Oct 28, 2020

Build failed:

@dcoutts
Copy link
Contributor

dcoutts commented Oct 30, 2020

bors merge

@Jimbo4350 Jimbo4350 force-pushed the jordan/create-vrf-with-correct-permissions branch from b6b88cc to 0cc2dc4 Compare November 2, 2020 10:51
@Jimbo4350
Copy link
Contributor Author

bors r+

@iohk-bors
Copy link
Contributor

iohk-bors bot commented Nov 2, 2020

@iohk-bors iohk-bors bot merged commit 964811d into master Nov 2, 2020
@iohk-bors iohk-bors bot deleted the jordan/create-vrf-with-correct-permissions branch November 2, 2020 14:44
iohk-bors bot added a commit that referenced this pull request Jun 7, 2022
3997: Remove code marked as for deletion after merging of #1948 r=newhoggy a=newhoggy



Co-authored-by: John Ky <john.ky@iohk.io>
iohk-bors bot added a commit that referenced this pull request Jun 7, 2022
3997: Remove code marked as for deletion after merging of #1948 r=newhoggy a=newhoggy



Co-authored-by: John Ky <john.ky@iohk.io>
iohk-bors bot added a commit that referenced this pull request Jun 7, 2022
3997: Remove code marked as for deletion after merging of #1948 r=newhoggy a=newhoggy



Co-authored-by: John Ky <john.ky@iohk.io>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants