chown 600 id_rsa が権限の問題を修正するのはなぜですか?

chown 600 id_rsa が権限の問題を修正するのはなぜですか?

最近、新しいマシン (Mac) にキーをインポートしたのですが、権限を 600 に変更するのを忘れていました。その際に、chownの代わりに と誤って書き込んでしまいましたchmod。不思議なことに、次のようにすると権限の問題が修正されました。

% git pull origin develop
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@         WARNING: UNPROTECTED PRIVATE KEY FILE!          @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Permissions 0644 for '/Users/m/.ssh/id_rsa' are too open.
It is required that your private key files are NOT accessible by others.
This private key will be ignored.
Load key "/Users/m/.ssh/id_rsa": bad permissions
git@<redacted>: Permission denied (publickey,gssapi-keyex,gssapi-with-mic).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
%
% sudo chown 600 ~/.ssh/id_rsa
Password:
%
% ls -l ~/.ssh/id_rsa
-rw-r--r--@ 1 600  staff  1679 Mar 25 15:14 /Users/m/.ssh/id_rsa
%
% git pull origin develop
From <redacted>
 * branch            develop    -> FETCH_HEAD
Already up to date.
%
% ssh <redacted>
The authenticity of host '<redacted> (<redacted>)' can't be established.
ECDSA key fingerprint is <redacted>.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '<redacted>' (ECDSA) to the list of known hosts.
Warning: your password will expire in 5 days
Last login: Fri Jul 27 19:51:32 2018 from <redacted>
-bash-4.2$

なぜそうなるのでしょうか?

答え1

によるオープンSSHソース コードでは、sshファイルの所有者が を実行しているユーザーである場合にのみ、秘密鍵ファイルの権限をチェックしますssh。ファイルが別のユーザーに属している場合、ssh はファイルを読み取ることができるすべてのユーザーにそのファイルの使用を許可します。

if ((st.st_uid == getuid()) && (st.st_mode & 077) != 0) {
     ^^^^^^^^^^^^^^^^^^^--owner ^^^^^^^^^^^^^^^^--permissions
    error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
    error("@         WARNING: UNPROTECTED PRIVATE KEY FILE!          @");
    error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
    ...

これは、マニュアルページ

ソース コードには、この動作が意図的であることを示す次のコメントが含まれています。

/*
 * if a key owned by the user is accessed, then we check the
 * permissions of the file. if the key owned by a different user,
 * then we don't care.
 */

現実的には、別のユーザーに属するキー ファイルの権限をチェックしても、セキュリティは向上しません。ファイルを読み取ることができる場合は、ファイルのコピーを作成し、コピーの権限を制限することで、権限チェックを回避できます。

答え2

chmod コマンドは「モードの​​変更」の略で、UNIX では「モード」とも呼ばれるファイルとフォルダの権限を変更できます。chown コマンドは「所有者の変更」の略で、ユーザーまたはグループの特定のファイルまたはフォルダの所有者を変更できます。

関連情報