Debian Codegraph @

external/rx/Rx/NET/Source/System.Reactive.Runtime.Remoting/Reactive/Linq/QueryLanguage.Remoting.cs

raw
1 // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
2
3 #if !NO_REMOTING
4 using System.Reactive.Disposables;
5 using System.Runtime.Remoting;
6 using System.Runtime.Remoting.Lifetime;
7 using System.Security;
8 using System.Threading;
9
10 //
11 // DESIGN: The MarshalByRefObject (MBRO) implementations for RemotableObserver and RemotableSubscription act as
12 // self-sponsoring objects controlling their lease times in order to tie those to the lifetime of the
13 // underlying observable sequence (ended by OnError or OnCompleted) or the user-controlled subscription
14 // lifetime. If we were to implement InitializeLifetimeService to return null, we'd end up with leases
15 // that are infinite, so we need a more fine-grained lease scheme. The default configuration would time
16 // out after 5 minutes, causing clients to fail while they're still observing the sequence. To solve
17 // this, those MBROs also implement ISponsor with a Renewal method that continues to renew the lease
18 // upon every call. When the sequence comes to an end or the subscription is disposed, the sponsor gets
19 // unregistered, allowing the objects to be reclaimed eventually by the Remoting infrastructure.
20 //
21 // SECURITY: Registration and unregistration of sponsors is protected by SecurityCritical annotations. The
22 // implementation of ISponsor is known (i.e. no foreign implementation can be passed in) at the call
23 // sites of the Register and Unregister methods. The call to Register happens in the SecurityCritical
24 // InitializeLifetimeService method and is called by trusted Remoting infrastructure. The Renewal
25 // method is also marked as SecurityCritical and called by Remoting. The Unregister method is wrapped
26 // in a ***SecurityTreatAsSafe*** private method which only gets called by the observer's OnError and
27 // OnCompleted notifications, or the subscription's Dispose method. In the former case, the sequence
28 // indicates it has reached the end, and hence resources can be reclaimed. Clients will no longer be
29 // connected to the source due to auto-detach behavior enforced in the SerializableObservable client-
30 // side implementation. In the latter case of disposing the subscription, the client is in control
31 // and will cause the underlying remote subscription to be disposed as well, allowing resources to be
32 // reclaimed. Rogue messages on either the data or the subscription channel can cause a DoS of the
33 // client-server communication but this is subject to the security of the Remoting channels used. In
34 // no case an untrusted party can cause _extension_ of the lease time.
35 //
36 //
37 // Notice this assembly is marked as APTCA in official builds, causing methods to be treated as transparent,
38 // thus requiring the ***SecurityTreatAsSafe*** annotation on the security boundaries described above. When not
39 // applied, the following exception would occur at runtime:
40 //
41 // System.MethodAccessException:
42 //
43 // Attempt by security transparent method 'System.Reactive.Linq.QueryLanguage+RemotableObservable`1+
44 // RemotableSubscription<T>.Unregister()' to access security critical method 'System.Runtime.Remoting.Lifetime.
45 // ILease.Unregister(System.Runtime.Remoting.Lifetime.ISponsor)' failed.
46 //
47 // Assembly 'System.Reactive.Linq, Version=2.0.ymmdd.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'
48 // is marked with the AllowPartiallyTrustedCallersAttribute, and uses the level 2 security transparency model.
49 // Level 2 transparency causes all methods in AllowPartiallyTrustedCallers assemblies to become security
50 // transparent by default, which may be the cause of this exception.
51 //
52 //
53 // The two CodeAnalysis suppressions below are explained by the Justification property (scroll to the right):
54 //
55 [assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2136:TransparencyAnnotationsShouldNotConflictFxCopRule", Scope = "member", Target = "System.Reactive.Linq.QueryLanguage+RemotableObserver`1.#Unregister()", Justification = "This error only occurs while running FxCop on local builds that don't have NO_CODECOVERAGE set, causing the assembly not to be marked with APTCA (see AssemblyInfo.cs). When APTCA is enabled in official builds, this SecurityTreatAsSafe annotation is required.")]
56 [assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2136:TransparencyAnnotationsShouldNotConflictFxCopRule", Scope = "member", Target = "System.Reactive.Linq.QueryLanguage+RemotableObservable`1+RemotableSubscription.#Unregister()", Justification = "This error only occurs while running FxCop on local builds that don't have NO_CODECOVERAGE set, causing the assembly not to be marked with APTCA (see AssemblyInfo.cs). When APTCA is enabled in official builds, this SecurityTreatAsSafe annotation is required.")]
57
58 namespace System.Reactive.Linq
59 {
60 public static partial class RemotingObservable
61 {
62 #region Remotable
63
64 private static IObservable<TSource> Remotable_<TSource>(IObservable<TSource> source)
65 {
66 return new SerializableObservable<TSource>(new RemotableObservable<TSource>(source, null));
67 }
68
69 private static IObservable<TSource> Remotable_<TSource>(IObservable<TSource> source, ILease lease)
70 {
71 return new SerializableObservable<TSource>(new RemotableObservable<TSource>(source, lease));
72 }
73
74 [Serializable]
75 class SerializableObservable<T> : IObservable<T>
76 {
77 readonly RemotableObservable<T> remotableObservable;
78
79 public SerializableObservable(RemotableObservable<T> remotableObservable)
80 {
81 this.remotableObservable = remotableObservable;
82 }
83
84 public IDisposable Subscribe(IObserver<T> observer)
85 {
86 var d = new SingleAssignmentDisposable();
87
88 var o = Observer.Create<T>(
89 observer.OnNext,
90 ex =>
91 {
92 //
93 // Make call to the remote subscription, causing lease renewal to be stopped.
94 //
95 using (d)
96 {
97 observer.OnError(ex);
98 }
99 },
100 () =>
101 {
102 //
103 // Make call to the remote subscription, causing lease renewal to be stopped.
104 //
105 using (d)
106 {
107 observer.OnCompleted();
108 }
109 }
110 );
111
112 //
113 // [OK] Use of unsafe Subscribe: non-pretentious transparent wrapping through remoting; exception coming from the remote object is not re-routed.
114 //
115 d.Disposable = remotableObservable.Subscribe/*Unsafe*/(new RemotableObserver<T>(o));
116
117 return d;
118 }
119 }
120
121 class RemotableObserver<T> : MarshalByRefObject, IObserver<T>, ISponsor
122 {
123 readonly IObserver<T> underlyingObserver;
124
125 public RemotableObserver(IObserver<T> underlyingObserver)
126 {
127 this.underlyingObserver = underlyingObserver;
128 }
129
130 public void OnNext(T value)
131 {
132 underlyingObserver.OnNext(value);
133 }
134
135 public void OnError(Exception exception)
136 {
137 try
138 {
139 underlyingObserver.OnError(exception);
140 }
141 finally
142 {
143 Unregister();
144 }
145 }
146
147 public void OnCompleted()
148 {
149 try
150 {
151 underlyingObserver.OnCompleted();
152 }
153 finally
154 {
155 Unregister();
156 }
157 }
158
159 [SecuritySafeCritical] // See remarks at the top of the file.
160 private void Unregister()
161 {
162 var lease = (ILease)RemotingServices.GetLifetimeService(this);
163 if (lease != null)
164 lease.Unregister(this);
165 }
166
167 [SecurityCritical]
168 public override object InitializeLifetimeService()
169 {
170 var lease = (ILease)base.InitializeLifetimeService();
171 lease.Register(this);
172 return lease;
173 }
174
175 [SecurityCritical]
176 TimeSpan ISponsor.Renewal(ILease lease)
177 {
178 return lease.InitialLeaseTime;
179 }
180 }
181
182 [Serializable]
183 sealed class RemotableObservable<T> : MarshalByRefObject, IObservable<T>
184 {
185 readonly IObservable<T> underlyingObservable;
186 readonly ILease lease;
187
188 public RemotableObservable(IObservable<T> underlyingObservable, ILease lease)
189 {
190 this.underlyingObservable = underlyingObservable;
191 this.lease = lease;
192 }
193
194 public IDisposable Subscribe(IObserver<T> observer)
195 {
196 //
197 // [OK] Use of unsafe Subscribe: non-pretentious transparent wrapping through remoting; throwing across remoting boundaries is fine.
198 //
199 return new RemotableSubscription(underlyingObservable.Subscribe/*Unsafe*/(observer));
200 }
201
202 [SecurityCritical]
203 public override object InitializeLifetimeService()
204 {
205 return lease;
206 }
207
208 sealed class RemotableSubscription : MarshalByRefObject, IDisposable, ISponsor
209 {
210 private IDisposable underlyingSubscription;
211
212 public RemotableSubscription(IDisposable underlyingSubscription)
213 {
214 this.underlyingSubscription = underlyingSubscription;
215 }
216
217 public void Dispose()
218 {
219 //
220 // Avoiding double-dispose and dropping the reference upon disposal.
221 //
222 using (Interlocked.Exchange(ref underlyingSubscription, Disposable.Empty))
223 {
224 Unregister();
225 }
226 }
227
228 [SecuritySafeCritical] // See remarks at the top of the file.
229 private void Unregister()
230 {
231 var lease = (ILease)RemotingServices.GetLifetimeService(this);
232 if (lease != null)
233 lease.Unregister(this);
234 }
235
236 [SecurityCritical]
237 public override object InitializeLifetimeService()
238 {
239 var lease = (ILease)base.InitializeLifetimeService();
240 lease.Register(this);
241 return lease;
242 }
243
244 [SecurityCritical]
245 TimeSpan ISponsor.Renewal(ILease lease)
246 {
247 return lease.InitialLeaseTime;
248 }
249 }
250 }
251
252 #endregion
253 }
254 }
255 #endif