2

私はアクセスルールを次のRegistryKeyように追加しようとしています:

using ( RegistryKey registry = OpenOrCreateMyKey() )
{
    RegistrySecurity security = registry.GetAccessControl();
    security.AddAccessRule( new RegistryAccessRule(
        new SecurityIdentifier( WellKnownSidType.BuiltinUsersSid, null ),
        RegistryRights.WriteKey,
        InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit,
        PropagationFlags.None,
        AccessControlType.Allow ) );
    registry.SetAccessControl( security );
}

ただし、レジストリがマングルされた場合AddAccessRule、例外がスローされます(リフレクターは、呼び出しがそれらが正規ではないと判断し、インスタンスがその状態にあるGetAccessControlときに書き込みを行おうとするとフェイルセーフを作動させることを示します):RegistrySecurity

System.InvalidOperationException: This access control list is not in canonical form and therefore cannot be modified.

regedt32(およびおそらくregedit)を実行すると、次のようなポップアップが表示されますthe permissions on <key> are incorrectly ordered, which may cause some entries to be ineffective <paraphrasing>Do you want to unmangle them now? Y/N</paraprasing>

この問題で私が見た中で最も権威のある作品はhttp://www.codeproject.com/KB/system/accessctrl3.aspxで、次のように書かれています。

ただし、制御フラグはプロパティとして実装されます(不整合について話します!)。AreAccessRulesProtected/から自動継承設定を取得できますAreAuditRulesProtected(ACLが保護されている場合は、自動継承されないことを思い出してください)。パート2を読むと、一部のWindows APIが継承をサポートしていないため、マシンの継承設定が破損する可能性があることがわかります。良いニュースは、.NETが継承メカニズムを完全にサポートし、継承設定を適切に保持することです。何らかの理由でACLが乱れた(おそらく不正なWin32アプリから)セキュリティ記述子を開いた場合、それを編集しようとすると、InvalidOperationExceptionがスローされます。

一般に、このような非正規のACLは、[廃止されてから] NewSIDツールを使用した結果であり、人々はKB記事に「それならやめなさい」と書いています。

しかし、批判的には、これが常に理由であるとは限らず、コードが機能する必要がある場合もあります。これを処理するための良いクリーンで安全な方法は何ですか?

私は2つの答えを提供し、人々は投票して穴を選び、投票し、コメントし、そしてニッチピックすることができます。

4

2 に答える 2

2

アプローチ1は、継承されたアクセス許可を無視し、とにかくやりたいことを盲目的に書き込むことです。

using ( RegistryKey registry = OpenOrCreateMyKey() )
{
    RegistrySecurity security = new RegistrySecurity();
    security.AddAccessRule( new RegistryAccessRule( ... ));
    registry.SetAccessControl( security );
}

問題は、潜在的な悪影響が何であるかがわからないことです。アクセスが必要なすべての人が私のルールからアクセスできることを嬉しく思います。ただし、他のOSインフラストラクチャとうまく連携しますか?

于 2010-02-10T12:36:15.063 に答える
1

アプローチ2は、最初の方法を試し、必要な場合にのみダーティな方法にフォールバックすることです。

using ( RegistryKey registry = OpenOrCreateMyKey() )
{
    RegistrySecurity security = new RegistrySecurity();
    if ( !security.AreAccessRulesCanonical )
    {
        Log.WriteWarning( "Registry permissions (likely ones inherited from parent) are inconsistent (RegistrySecurity.AreAccessRulesCanonical is false), so using alternate permissioning algorithm, Has NewSID or another tool mangled permissions? regedt32 can be used to analyze the issue." );
         // Ignore parent ACLs and just blindly stuff in this ACL (which will continue to be inherited)
         security = new RegistrySecurity();
     }

    security.AddAccessRule( new RegistryAccessRule( ... ));
    registry.SetAccessControl( security );
}

アプローチ1の弱点に加えて、これには予測できない動作がありますが、少なくとも、元のアプローチとある程度のバグ互換性があります。

于 2010-02-10T12:40:40.187 に答える