Microsoft MVP성태의 닷넷 이야기
글쓴 사람
정성태 (techsharer at outlook.com)
홈페이지
첨부 파일

(시리즈 글이 8개 있습니다.)
.NET Framework: 439. .NET CLR4 보안 모델 - 1. "Security Level 2"란?
; https://www.sysnet.pe.kr/2/0/1680

.NET Framework: 440. .NET CLR4 보안 모델 - 2. 샌드박스(Sandbox)을 이용한 보안
; https://www.sysnet.pe.kr/2/0/1681

.NET Framework: 441. .NET CLR4 보안 모델 - 3. CLR4 보안 모델에서의 APTCA 역할
; https://www.sysnet.pe.kr/2/0/1682

오류 유형: 228. CLR4 보안 - yield 구문 내에서 SecurityCritical 메서드 사용 불가
; https://www.sysnet.pe.kr/2/0/1683

.NET Framework: 514. .NET CLR2 보안 모델에서의 APTCA 역할 (2)
; https://www.sysnet.pe.kr/2/0/10804

.NET Framework: 573. .NET CLR4 보안 모델 - 4. CLR4 보안 모델에서의 조건부 APTCA 역할
; https://www.sysnet.pe.kr/2/0/10947

.NET Framework: 605. CLR4 보안 - yield 구문 내에서 SecurityCritical 메서드 사용 불가 - 2번째 이야기
; https://www.sysnet.pe.kr/2/0/11041

닷넷: 2219. .NET CLR2 보안 모델에서의 개별 System.Security.Permissions 제어
; https://www.sysnet.pe.kr/2/0/13565




.NET CLR4 보안 모델 - 4. CLR4 보안 모델에서의 조건부 APTCA 역할

지난 글까지 이해한 것을 전제로,

.NET CLR4 보안 모델 - 1. "Security Level 2"란?
; https://www.sysnet.pe.kr/2/0/1680

.NET CLR4 보안 모델 - 2. 샌드박스(Sandbox)을 이용한 보안
; https://www.sysnet.pe.kr/2/0/1681

.NET CLR4 보안 모델 - 3. CLR4 보안 모델에서의 APTCA 역할
; https://www.sysnet.pe.kr/2/0/1682

.NET CLR4 보안 모델 - 4. CLR4 보안 모델에서의 조건부 APTCA 역할 
; https://www.sysnet.pe.kr/2/0/10947

이번에는 "조건부 APTCA"라는 것을 설명해 보겠습니다.

The Conditional APCTA
; https://www.simple-talk.com/dotnet/.net-framework/code-access-security-in-asp.net-4.0/

이게 뭐냐면??? 우리가 만든 APTCA 적용 DLL을 기본적으로는 로드되지 않도록 한다는 것입니다. 로드하려면 해당 DLL을 '알고 로드하겠다.'는 명시적인 설정을 해줘야 합니다.

테스트를 한번 해볼까요? ^^

".NET CLR4 보안 모델 - 3. CLR4 보안 모델에서의 APTCA 역할" 글에 포함된 TestApp2.zip 예제를 이용해 볼텐데요.

예제 프로젝트는 3개의 프로젝트를 포함합니다.

TestApp - 샌드 박싱 환경을 만들고 TestLib를 호출

TestLib - 일반적인 DLL 라이브러리
          2개의 메서드(DoMethod, DoCriticalMethod)가 구현되어 있는데,
          DoCriticalMethod 메서드에서 HostService 라이브러리에 구현된 
          SecuritySafeCritical 특성의 GetFileHandle 메서드를 호출

HostService - APTCA가 적용된 DLL 라이브러리, SecuritySafeCritical 특성이 적용된 GetFileHandle 메서드를 제공

일단 빌드하고 실행해 보면 이전 글에서 설명한 대로 DoCriticalMethod 메서드 호출 시 다음과 같은 예외가 발생합니다.

System.MethodAccessException: Attempt by security transparent method 'SecurityService.GetFileHandle(System.IO.FileStream
)' to access security critical method 'System.IO.FileStream.get_Handle()' failed.

Assembly 'HostService, Version=1.0.0.0, Culture=neutral, PublicKeyToken=2a9ed378705e40fd' is partially trusted, which ca
uses the CLR to make it entirely security transparent regardless of any transparency annotations in the assembly itself.
  In order to access security critical code, this assembly must be fully trusted.
   at SecurityService.GetFileHandle(FileStream fs)
   at Class1.DoCriticalMethod()

이유도 지난 글에서 설명했지만, 샌드 박싱 환경에서 GAC에 등록되지 않은 APTCA의 SecuritySafeCritical 류의 메서드를 호출했기 때문입니다. 따라서, 오류를 없애기 위해 GAC에 HostService.dll을 등록해 줍니다.

여기까지가, 지난 글에서 설명한 보안 모델인데 이제 "조건부 APTCA"가 뭔지 테스트를 해보겠습니다.

기존의 (GAC에 등록된) "APTCA" DLL을 "조건부 APTCA" DLL로 기존의 APTC 특성을 다음과 같이 바꿔주기만 하면 됩니다.

// [assembly: AllowPartiallyTrustedCallers]

[assembly: AllowPartiallyTrustedCallers(PartialTrustVisibilityLevel = PartialTrustVisibilityLevel.NotVisibleByDefault)]

이렇게 변경하고 HostService 프로젝트를 빌드한 후 다시 GAC에 등록하고 실행하면 이번엔 다음과 같은 예외가 발생합니다.

System.MethodAccessException: Attempt by security transparent method 'Class1.DoCriticalMethod()' to access security critical method 'SecurityService.GetFileHandle(System.IO.FileStream)' failed.

Assembly 'TestLib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=feef41772217d3e6' is partially trusted, which causes the CLR to make it entirely security transparent regardless of any transparency annotations in the assembly itself. In order to access security critical code, this assembly must be fully trusted.

Assembly 'HostService, Version=1.0.0.0, Culture=neutral, PublicKeyToken=2a9ed378705e40fd' is a conditionally APTCA assembly which is not enabled in the current AppDomain. To enable this assembly to be used by partial trust or security transparent code, please add assembly name 'HostService, PublicKey=002400000...[생략]...10C273F71D42DC0651F96BA' to the the PartialTrustVisibleAssemblies list when creating the AppDomain. at Class1.DoCriticalMethod()


저렇게 상세한 오류가 남는 경우는 운이 좋은 편에 속하고, 어떨 때는 내부적으로 예외가 '씹히는 경우'라면 단순히 디버그 출력 창에서 다음과 같은 식의 메시지로만 확인이 가능합니다.

CLR:(C:\Windows\Microsoft.Net\assembly\GAC_MSIL\HostService\v4.0_4.0.0.0__2a9ed378705e40fd\HostService.dll) Rejecting code sharing because a dependent assembly did not match the conditional APTCA share mode


오류 메시지의 의미는 간단합니다. HostService 어셈블리가 "conditionally APTCA"로 지정되었기 때문에 이 어셈블리를 로드하려면 명시적으로 이를 허락하겠다는 표시를 해야 한다는 것입니다.

이 글에서 다루는 예제 프로젝트는 샌드 박싱 환경을 직접 구성하고 있기 때문에 AppDomain.CreateDomain 메서드에 전달되는 AppDomainSetup.PartialTrustVisibleAssemblies 속성 값을 다음과 같이 채워놓아야 합니다.

AppDomainSetup adSetup = new AppDomainSetup();
// ...[생략]...
adSetup.PartialTrustVisibleAssemblies = new string[]
{
    "HostService, PublicKey=002400000...[생략]...DC0651F96BA"
};

// ...[생략]...

AppDomain newDomain = AppDomain.CreateDomain("Sandbox", null, adSetup, permSet, fullTrustAssembly);

주의할 것은, 어셈블리 이름 지정 시 PublicKeyToken 값이 아닌 PublicKey가 쓰인다는 점입니다. 이 값을 구하려면 예외 메시지에 나온 PublicKey 값을 써도 되지만 만약 표시되지 않은 경우에는 해당 어셈블리로부터 sn.exe를 이용해 다음과 같이 구할 수 있습니다.

C:\Program Files (x86)\Microsoft Visual Studio 14.0>sn -Tp C:\...[생략]...\TestApp2\HostService\bin\Debug\HostService.dll

Microsoft (R) .NET Framework Strong Name Utility  Version 4.0.30319.0
Copyright (c) Microsoft Corporation.  All rights reserved.

Public key (hash algorithm: sha1):
002400000480000094000000060200000024000052534131000400000100010015508e3d344333
7e6c58ae5eca494a8fb5ff8c92538de1756779a1e199f00d1931690e02ccd7db671100051cca6e
877a258b919e466c3f7723bd3dd499a5e9bf4bd8861894f8bbbff8dba79c49d0006d6b46fda009
38c026d73360979454b08aa19507ebc03b858e11cc1d438904fe46db8758ae010c273f71d42dc0
651f96ba

Public key token is 2a9ed378705e40fd

이제, 다시 한번 예제 프로젝트를 실행하면 이번에는 정상적으로 실행되는 것을 확인할 수 있습니다.

물론, 이 제약은 샌드박싱 환경에서만 유효합니다. 즉, 보안을 제약하지 않은 일반 응용 프로그램이거나 "<trust level="Full" />"이 기본값인 웹 응용 프로그램에서는 '조건부 APTCA'가 설정된 어셈블리를 partialTrustVisibleAssemblies로 굳이 등록해줄 필요가 없습니다.

(첨부한 파일은 이 글의 예제 코드를 포함합니다.)




참고로, 이미 호스팅 환경이 구성된 ASP.NET 웹 응용 프로그램에서라면 '조건부 APTCA'가 지정된 어셈블리를 사용하기 위해 web.config에 다음과 같은 식으로 등록해 줄 수 있습니다.

<configuration> 

    ...[생략]...

    <system.web>

        ...[생략]...

        <partialTrustVisibleAssemblies> 
            <add assemblyName="HostService" publicKey="002400000...[생략]...DC0651F96BA" />
        </partialTrustVisibleAssemblies>
    </system.web>

    ...[생략]...

</configuration>




[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]







[최초 등록일: ]
[최종 수정일: 4/13/2017]

Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
by SeongTae Jeong, mailto:techsharer at outlook.com

비밀번호

댓글 작성자
 




... 46  47  48  49  50  [51]  52  53  54  55  56  57  58  59  60  ...
NoWriterDateCnt.TitleFile(s)
12350정성태9/25/202013950Windows: 175. 윈도우 환경에서 클라이언트 소켓의 최대 접속 수 [2]파일 다운로드1
12349정성태9/25/20208908Linux: 32. Ubuntu 20.04 - docker를 위한 tcp 바인딩 추가
12348정성태9/25/20209622오류 유형: 658. 리눅스 docker - Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock
12347정성태9/25/202023768Windows: 174. WSL 2의 네트워크 통신 방법 [4]
12346정성태9/25/20208867오류 유형: 657. IIS - http://localhost 방문 시 Service Unavailable 503 오류 발생
12345정성태9/25/20208594오류 유형: 656. iisreset 실행 시 "Restart attempt failed." 오류가 발생하지만 웹 서비스는 정상적인 경우파일 다운로드1
12344정성태9/25/20209763Windows: 173. 서비스 관리자에 "IIS Admin Service"가 등록되어 있지 않다면?
12343정성태9/24/202019290.NET Framework: 945. C# - 닷넷 응용 프로그램에서 메모리 누수가 발생할 수 있는 패턴 [5]
12342정성태9/24/202010622디버깅 기술: 171. windbg - 인스턴스가 살아 있어 메모리 누수가 발생하고 있는지 확인하는 방법
12341정성태9/23/20209789.NET Framework: 944. C# - 인스턴스가 살아 있어 메모리 누수가 발생하고 있는지 확인하는 방법파일 다운로드1
12340정성태9/23/20209528.NET Framework: 943. WPF - WindowsFormsHost를 담은 윈도우 생성 시 메모리 누수
12339정성태9/21/20209488오류 유형: 655. 코어 모드의 윈도우는 GUI 모드의 윈도우로 교체가 안 됩니다.
12338정성태9/21/20209022오류 유형: 654. 우분투 설치 시 "CHS: Error 2001 reading sector ..." 오류 발생
12337정성태9/21/202010320오류 유형: 653. Windows - Time zone 설정을 바꿔도 반영이 안 되는 경우
12336정성태9/21/202012840.NET Framework: 942. C# - WOL(Wake On Lan) 구현
12335정성태9/21/202022244Linux: 31. 우분투 20.04 초기 설정 - 고정 IP 및 SSH 설치
12334정성태9/21/20207700오류 유형: 652. windbg - !py 확장 명령어 실행 시 "failed to find python interpreter"
12333정성태9/20/20208139.NET Framework: 941. C# - 전위/후위 증감 연산자에 대한 오버로딩 구현 (2)
12332정성태9/18/202010161.NET Framework: 940. C# - Windows Forms ListView와 DataGridView의 예제 코드파일 다운로드1
12331정성태9/18/20209329오류 유형: 651. repadmin /syncall - 0x80090322 The target principal name is incorrect.
12330정성태9/18/202010346.NET Framework: 939. C# - 전위/후위 증감 연산자에 대한 오버로딩 구현 [2]파일 다운로드1
12329정성태9/16/202012283오류 유형: 650. ASUS 메인보드 관련 소프트웨어 설치 후 ArmouryCrate.UserSessionHelper.exe 프로세스 무한 종료 현상
12328정성태9/16/202012460VS.NET IDE: 150. TFS의 이력에서 "Get This Version"과 같은 기능을 Git으로 처리한다면?
12327정성태9/12/202010076.NET Framework: 938. C# - ICS(Internet Connection Sharing) 제어파일 다운로드1
12326정성태9/12/20209587개발 환경 구성: 516. Azure VM의 Network Adapter를 실수로 비활성화한 경우
12325정성태9/12/20209141개발 환경 구성: 515. OpenVPN - 재부팅 후 ICS(Internet Connection Sharing) 기능이 동작 안하는 문제
... 46  47  48  49  50  [51]  52  53  54  55  56  57  58  59  60  ...