You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Christopher Schultz <ch...@christopherschultz.net> on 2018/03/21 05:32:52 UTC
Error when using SecurityManager
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256
All,
I'm exploring running my application under a SecurityManager (on
Tomcat 8.5.29), and I'm getting an unexpected complaint during Tomcat
shutdown:
Mar 21, 2018 1:26:23 AM org.apache.catalina.core.ApplicationContext log
SEVERE: my.JNDIDataSourceShutdownListener: Cannot close DataSource
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.jav
a:62)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessor
Impl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at
my.JNDIDataSourceShutdownListener.close(JNDIDataSourceShutdownListener.j
ava:84)
at
my.JNDIDataSourceShutdownListener.contextDestroyed(JNDIDataSourceShutdow
nListener.java:63)
at
org.apache.catalina.core.StandardContext.listenerStop(StandardContext.ja
va:4800)
at
org.apache.catalina.core.StandardContext.stopInternal(StandardContext.ja
va:5437)
at
org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:226)
at
org.apache.catalina.core.ContainerBase$StopChild.call(ContainerBase.java
:1437)
at
org.apache.catalina.core.ContainerBase$StopChild.call(ContainerBase.java
:1426)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.jav
a:1142)
at
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.ja
va:617)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.security.AccessControlException: access denied
("javax.management.MBeanServerPermission" "createMBeanServer")
at
java.security.AccessControlContext.checkPermission(AccessControlContext.
java:472)
at
java.security.AccessController.checkPermission(AccessController.java:884
)
at
java.lang.SecurityManager.checkPermission(SecurityManager.java:549)
at
java.lang.management.ManagementFactory.getPlatformMBeanServer(Management
Factory.java:465)
at
org.apache.tomcat.dbcp.dbcp2.BasicDataSource.close(BasicDataSource.java:
1950)
... 15 more
My JNDIDataSourceShutdownListener fetches the JNDI DataSource from the
JNDI tree and reflectively calls "close" on it to free it up.
This seems unexpected because:
1. I would expect that the MBeanServer would still be available even
during application shut-down (before Tomcat itself shuts down).
2. I would expect that BasicDataSource would use a PrivilegedAction if
it needed to create an MBeanServer in this way.
I can obviously just give this permission to the application, but I
think it probably makes more sense to solve this in a different way --
through Tomcat itself.
Any ideas? I'm certainly no expert in SecurityManagers and what Tomcat
is doing under the covers with its DataSources.
- -chris
-----BEGIN PGP SIGNATURE-----
Comment: GPGTools - http://gpgtools.org
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/
iQIzBAEBCAAdFiEEMmKgYcQvxMe7tcJcHPApP6U8pFgFAlqx7oQACgkQHPApP6U8
pFgtnhAAgZG3nmLVDNwBIZk4IB3eaI+O58jDCcsgaHAMgri6rHvodUISo9fBI6iD
fwgvB3KjZ4kNqEzQP5img5/wAZ+WJB+yg93E0s8of39g88RlI5CxLjghpjMd2m+U
WbdHsW1h+qBZaqfAHbtAZmPsz6XnG5Kmo7HAMI5qpTW9jxvoS4Ds+uqSsj7/XgeW
GBVPZU9+6XezbxGZXw9yIjsI2Zv1zvok+Z5bmdMzc1u8kMaAQFCvBmAHrN1V+m3l
mReP/Hf5DGYJKn03mpaUO4d6q3RqLA+EkzE38RtHDASqBwkxrWbzArxo0+YpjfwQ
2gcP69efTzQrcmqkoJBrFfWmNG2NH56NhZMD0xAM2ikGhmAtjE1VA61+Sn1A7YJL
FTjWUxutbRmNUQbmkd6ee0c8rnGGLSa5Sv78c9GMWbvz+x9epRxp7fy7NfIqiaQe
cFgxVh85RoaLair2cc5v6zdBZfOnQutFHggcko18DW6NN3fYhRrk8WcAfm1B6lcX
p/Pb+zY9ni7MUswdXgeZByo8Sbc6UmE72RRRWxqu8XFhtqjBBCuuAOJQtPv3f+lE
A3PzN52J8MWLwsPPmLQoONcu7oPTMFn9SwckrNwJgHvqHf5lCjaMhxaX6bZ5cW00
BCs9JYWDH2Z63XMtwAR2xx/CVU3aY7ZR1UpFQvV5MN8LgsNaiRo=
=qoda
-----END PGP SIGNATURE-----
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org
Re: [OT] Error when using SecurityManager
Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256
Mark,
On 3/21/18 10:39 AM, Mark Thomas wrote:
> On 21/03/18 13:45, Christopher Schultz wrote:
>> Mark,
>>
>> On 3/21/18 6:24 AM, Mark Thomas wrote:
>>> On 21/03/18 05:32, Christopher Schultz wrote:
>>>> All,
>>>>
>>>> I'm exploring running my application under a SecurityManager
>>
>>> That's brave.
>>
>> Honestly, it really shouldn't be.
>>
>> I'm coming to the conclusion that a SecurityManager can really
>> only limit the damage an application can do to the extent that
>> the application could already do under normal circumstances.
>>
>> That sounds idiotic. Let me explain.
>>
>> In order to prevent the application from making arbitrary
>> network connections, I can whitelist those connections that are
>> okay. Fine. I get to specify the hostname (e.g. localhost) and
>> port (e.g. 11211) that the application is allowed to access.
>>
>> But since I'm using an exploded WAR file and I don't bother
>> packaging-up my application into a single JAR file (i.e. I use
>> .class files in WEB-INF/classes like everyone else), I can't
>> limit those connections to just my own code.
>
> You can limit permissions to just WEB-INF/classes. There is an
> exmaple in catalina.policy in Tomcat 9 at least.
Duh. At first, I did:
grant {
// stuff my application needs
}
I did that for two reasons:
1. I just wanted to get the application to work. I can narrow-down the
grants after I have an exhaustive list.
2. I wasn't sure all of the code was using PrivilegedActions when
running under a SecurityManager.
So the only danger, then, is having some unforeseen code loaded into
my running web application, writing a .class file into WEB-INF/classes
(theoretically impossible because I'm not allowing writes to that
directory) and then triggering classloading which would then place it
into the security context of WEB-INF/classes.
Then it only has access to some very specific things, but it could
e.g. make a connection to my database (if it could guess a valid set
of credentials) or my e.g. memcached instance (which has no
authentication).
Still, being able to limit the amount of damage that can be done is
attractive.
>> So, let's say I have an old version of commons-fileupload and my
>> application unsafely deserializes an untrusted blob. That means
>> that newly-instantiated code running within my application can
>> reac-hout to my memcached server and do whatever it wants. Not
>> great.
>
> Only if you grant the necessary permissions to the
> commons-fileupload WAR. Every piece of code in the stack from the
> call in question until you reach either the top of the stack of a
> privileged action has to have the necessary permission.
Duh. Because of my blanket-grant, this would be possible. But I should
be doing this in a more fine-grained way, which I will be doing.
>> So in order to get the most benefit from the SecurityManager, I
>> have to change my application's code (to use PrivilegedActions in
>> places where it matters)
>
> Yes. And don't forget design the public API that can call those
> PrivilegedActions in such a way that they can't be abused.
> Generally, the usual rules about not trusting provided data.
Yes, this is the tough part.
For example, it doesn't make sense to grant "SockerPermission"
"localhost:11211" "connect" to my memcached library. Instead, I'd want
to allow that only for the code in my web application that actually
should be allowed to contact memcached. But of course if I have a
utility class that "gets stuff from memcached", whitelisting that code
also doesn't make much sense, because rogue code could just call that AP
I.
It's a tricky business trying to get the *exact* piece of code the
correct privileges to make the application as close to 100% safe as
possible.
Too fine-grained and it's very difficult to use. Too coarse and you
aren't actually getting any protection.
My goal for the time being is to basically say "if rogue code executes
and can connect to my db/memcached instance, I'm willing to take that
risk" because I have at least made sure it can't connect to some OTHER
service on the internet and become part of a DDOS botnet. And you
can't read my log files and send them to a remote server. And you
can't delete random files on the filesystem.
That's the minimum bar I'm trying to set initially. In the future, I
might get more conservative as I get more familiar with the
capabilities and limits of the SecurityManager and, honestly, the
impacts upon my own code.
>> and also its packaging, either to a JAR within the exploded-WAR,
>> or switch to using a WAR file (which for me wouldn't actually be
>> that hard).
>
> I don't believe that is necessary.
Well, limiting to a single JAR file might narrow the attack surface.
If the attacker needs to modify my JAR file in order to gain
privileges, it's harder than just dropping a .class file onto the
disk. Or I can even sign the JAR file and require the signature to be
trusted and cut-off that vector.
I can't sign a directory, or even a single .class file. The JAR is the
smallest signable item that could be trusted.
>> If I wanted to run more than one web application with different
>> permissions, this would be an absolute requirement. As it stands,
>> I have one webapp per Tomcat instance, so this is less of an
>> issue.
>>
>> What would be really great is if Tomcat could manage the
>> protection domains such that each web application has its own
>> effective policy, and wouldn't require the use of
>> PrivilegedActions. I'm not sure if any of that is even
>> possible... just thinking like a lazy developer who wants to "add
>> security later" -- the impossible dream.
>
> It could never be as fine-grained as PrivilegedActions which are
> probably the best solution we have at the moment but it makes
> adding security later very hard work.
Of course. The good news is that I have very few places in my code
where privileged actions are required. Reading system properties,
reading/writing files, and network connections are fairly rare events
or at least they are handled in some specific places.
Some of the privileges need to be given at a coarse-grain such as
"it's okay to read everything in "{webapp}/-" so that my application's
templating library (Velocity) can read the damned templates. I don't
believe Velocity uses PrivilegedActions anywhere so I have to do it
myself (or grant thew whole JVM that privilege, which is what I've
done so far).
I'll keep experimenting to see what I can get done as safely as
possible with as few changes to my application as possible.
Being able to remove that ServletContextListener will be good, because
then I can drop the "allow package access" permission that allows me
to introspect the [Basic]DataSource's close() method (which requires
special permission) and then invoke it (which would already be allowed).
Thanks,
- -chris
-----BEGIN PGP SIGNATURE-----
Comment: GPGTools - http://gpgtools.org
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/
iQJRBAEBCAA7FiEEMmKgYcQvxMe7tcJcHPApP6U8pFgFAlqyjNAdHGNocmlzQGNo
cmlzdG9waGVyc2NodWx0ei5uZXQACgkQHPApP6U8pFi8pg//UvxPx8VmzQ6YHZET
hJE5SymXStyITnfxAjioxAhFmcw8Odcq/+px6fz4KdAV7gGEyHR51EIclbYIXLJg
1f2ssdth2dzuNVCeYJzpQVs5XAkhWmXiZaTYczZ3+KTQpI9x6IcP8JByLuqydNrJ
+YadLK8mLq+hiD/8zNESLyY/s9vaVV3m+xAlUkgDz8s2dw0kIioWYGEzhHY/ZXH0
+Xcfr+/tHSfRtbzj9hzKRP6xlo56IcRhbMOmUr41GUuZM2P+EaOPmTTmXWSwwxw7
Ebl1RwTnCwQ4rvTDfsbaKgMMo2VzYghCkVXfiR7YVzlXn7ZC2Sz1YQT9WJhB+Rsa
tkIwxrkvM9QqfCrlctuxf+z+GtNaCKMHmhS9OXAGgEUPTAS/kUzWBJPgtLKv4JZS
kTfF2O+kJYiiws7D3xIO/qOZR+IEQi3D0omroy6c87yQ6y5HQAIIyZqfL93pkQBU
BBcioUtmh5MJuY20jIf+wsNQiglJ83RpwZxEJA4Lann8pGQ6bYyBQBi0EYFbQ42g
TGC5n8R1cYWHRGjAdp2Sx9+cibFzEagA0mASxqs+YnFrApErwVjESd8LX3csUcQl
M6MyrBB5LUuhHGZKlqDP4VPKRw5ZC6skgyElSGU4ePdWD0vaZOKFaO2575iAtMUf
k42bB423ZkPYK2qhQhh2t5SsND4=
=0NCi
-----END PGP SIGNATURE-----
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org
Re: [OT] Error when using SecurityManager
Posted by Mark Thomas <ma...@apache.org>.
On 21/03/18 13:45, Christopher Schultz wrote:
> Mark,
>
> On 3/21/18 6:24 AM, Mark Thomas wrote:
>> On 21/03/18 05:32, Christopher Schultz wrote:
>>> All,
>>>
>>> I'm exploring running my application under a SecurityManager
>
>> That's brave.
>
> Honestly, it really shouldn't be.
>
> I'm coming to the conclusion that a SecurityManager can really only
> limit the damage an application can do to the extent that the
> application could already do under normal circumstances.
>
> That sounds idiotic. Let me explain.
>
> In order to prevent the application from making arbitrary network
> connections, I can whitelist those connections that are okay. Fine. I
> get to specify the hostname (e.g. localhost) and port (e.g. 11211)
> that the application is allowed to access.
>
> But since I'm using an exploded WAR file and I don't bother
> packaging-up my application into a single JAR file (i.e. I use .class
> files in WEB-INF/classes like everyone else), I can't limit those
> connections to just my own code.
You can limit permissions to just WEB-INF/classes. There is an exmaple
in catalina.policy in Tomcat 9 at least.
> So, let's say I have an old version of commons-fileupload and my
> application unsafely deserializes an untrusted blob. That means that
> newly-instantiated code running within my application can reac-hout to
> my memcached server and do whatever it wants. Not great.
Only if you grant the necessary permissions to the commons-fileupload
WAR. Every piece of code in the stack from the call in question until
you reach either the top of the stack of a privileged action has to have
the necessary permission.
> So in order to get the most benefit from the SecurityManager, I have
> to change my application's code (to use PrivilegedActions in places
> where it matters)
Yes. And don't forget design the public API that can call those
PrivilegedActions in such a way that they can't be abused. Generally,
the usual rules about not trusting provided data.
> and also its packaging, either to a JAR within the
> exploded-WAR, or switch to using a WAR file (which for me wouldn't
> actually be that hard).
I don't believe that is necessary.
> If I wanted to run more than one web application with different
> permissions, this would be an absolute requirement. As it stands, I
> have one webapp per Tomcat instance, so this is less of an issue.
>
> What would be really great is if Tomcat could manage the protection
> domains such that each web application has its own effective policy,
> and wouldn't require the use of PrivilegedActions. I'm not sure if any
> of that is even possible... just thinking like a lazy developer who
> wants to "add security later" -- the impossible dream.
It could never be as fine-grained as PrivilegedActions which are
probably the best solution we have at the moment but it makes adding
security later very hard work.
Mark
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org
Re: [OT] Error when using SecurityManager
Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256
Mark,
On 3/21/18 6:24 AM, Mark Thomas wrote:
> On 21/03/18 05:32, Christopher Schultz wrote:
>> All,
>>
>> I'm exploring running my application under a SecurityManager
>
> That's brave.
Honestly, it really shouldn't be.
I'm coming to the conclusion that a SecurityManager can really only
limit the damage an application can do to the extent that the
application could already do under normal circumstances.
That sounds idiotic. Let me explain.
In order to prevent the application from making arbitrary network
connections, I can whitelist those connections that are okay. Fine. I
get to specify the hostname (e.g. localhost) and port (e.g. 11211)
that the application is allowed to access.
But since I'm using an exploded WAR file and I don't bother
packaging-up my application into a single JAR file (i.e. I use .class
files in WEB-INF/classes like everyone else), I can't limit those
connections to just my own code.
So, let's say I have an old version of commons-fileupload and my
application unsafely deserializes an untrusted blob. That means that
newly-instantiated code running within my application can reac-hout to
my memcached server and do whatever it wants. Not great.
So in order to get the most benefit from the SecurityManager, I have
to change my application's code (to use PrivilegedActions in places
where it matters) and also its packaging, either to a JAR within the
exploded-WAR, or switch to using a WAR file (which for me wouldn't
actually be that hard).
If I wanted to run more than one web application with different
permissions, this would be an absolute requirement. As it stands, I
have one webapp per Tomcat instance, so this is less of an issue.
What would be really great is if Tomcat could manage the protection
domains such that each web application has its own effective policy,
and wouldn't require the use of PrivilegedActions. I'm not sure if any
of that is even possible... just thinking like a lazy developer who
wants to "add security later" -- the impossible dream.
Thanks,
- -chris
-----BEGIN PGP SIGNATURE-----
Comment: GPGTools - http://gpgtools.org
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/
iQIzBAEBCAAdFiEEMmKgYcQvxMe7tcJcHPApP6U8pFgFAlqyYhMACgkQHPApP6U8
pFjGsQ/9FCL3gZW94JySqtMbk+BDCTEfrJuLdlUk9txAO7CiahZ9HdvfAGOGKTzO
hD7sgTpihZ9G6B9/eQXYxr1iGQ1dclQ57See9eDaRazEvNSAhhVSLt7upL/fVstG
YmSmhRz1G+kbJ5K1Urf+345to2g89BdrTH3mR3TUaF8uDyXca2Yd1aWuvbXnlTB5
xLxIAz+K4EBG2UZArOxK1tajkmyhEuA4JA9pl7Zjs0S1FkTMlLwjf6g6Jfli6C5P
NhlTlotXiX47yj7mzTtaJee6t3I+ryUcYuz379SxJmZrJ5I63ey8OscIcIxD/e8k
tz70HuExbZ/cdCvHSZgu3Kld2Ezjq1m9H6a3yCmmGyBOBNn7ggPUxCItR2Id6uEq
gcJ1uaLeZIv8DfK0EouxhHkUP3b4H0x1Fl2Cf6TPFhMzCDcQwdI049eoENwmnOHY
2i0y0APnKcBwSEffbIJoIgYJuAgjNtY6I/ZrKLRTPYnCuN1tgJxbHwKo33JfzCxM
cJ9oIA/l296GCMnUtilecDngWHD5L34Wtp6fYzfxh/zGecU6BZfDMvAN5JT0bwX0
+Y/izmdtHmR5tK9HZLQWCAiFw4hTMzPD7iM9gBfslPKmZxYfCSrsluXZYQQ3iZHW
TtU/iU/breyGRAATICRZiD3EIdP0/px2snihZ8x6yB/iVRWXgTU=
=czHs
-----END PGP SIGNATURE-----
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org
Re: Error when using SecurityManager
Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256
All,
On 3/21/18 4:49 PM, Christopher Schultz wrote:
>> 3. Check via JMX to see if the DataSource has shut down
>
> There is no DataSource tree in JMX after calling stop() on the
> context, so I think that's satisfactory in terms of behavior. :)
>
>> If that works, I can just remove my listener.
>
> Looks like I can abandon my listener as we move to Tomcat 8.5. (We
> may be able to do it in Tomcat 8.0 as well; I'll confirm that,
> too.)
Confirmed: under Tomcat 8.5, the DataSource does in fact shut-down.
This must have been something historic that I had been dealing with.
Hooray for removing code!
- -chris
-----BEGIN PGP SIGNATURE-----
Comment: GPGTools - http://gpgtools.org
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/
iQJRBAEBCAA7FiEEMmKgYcQvxMe7tcJcHPApP6U8pFgFAlqyx3YdHGNocmlzQGNo
cmlzdG9waGVyc2NodWx0ei5uZXQACgkQHPApP6U8pFh7XRAAhusGiSKGcXFMo1m9
Yiiy1BzdjmWGg8ahVlFoz0LNQX+apO/rl3TFeCLJepANG4T+eeK9QChY/Qqz7e6w
cMo6o9hIwQLqSOeBwNpyBkXSR4uxecWezynLJ1FpoibnBpzM9i+3/YdXCjlG/Lsd
BdcKhsBixt9WcfHrOVp7cYROVChcv1FVRxVeuDW/z6fk8GNEqv7qKh/BR0whCFrY
MJhI5IQ96TcaAMNbhS+W63QKWkxwvLbMT84RkeqfIBzwKOn0EZCQ7tYgOePWKpS/
RzPx/T/20P/7S+RTAGjv9Y2zLWbxmUBk3s+zI/Ibu9powsciDsh982CW6/bWWGon
Sf+Eh2U1A/V/hJ3yrMBcAhpB0a5tHmV7YZOENMUUVVG8IVMwL5xAL41gvJtqhtG3
15vktj3kZDRGalZBnVnignVPUTMt/Obp0BkEnU2kiQbn0FC9Su+THg2y2h4Iqy8T
UTC9awto3KP89hNEVTPnNFr5ZDy/cwZsfbT5B510NHfbDTXyzcpkzOm8tvDgtYNF
CqUK0fb6Z5Ma7PxV4TJCcXTpNXwNoXqTwOcFGxqaVe5TcDpDJzL5a94TQel+9qel
i1cXgOh9wn24ybP0SC9TplOVz9xdG5wjQphcJ8Tf5bPSpP+v6nqiyDRCwJjb7E36
4xBMkBQHKJMfJZt7pg345AUolSQ=
=AzYg
-----END PGP SIGNATURE-----
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org
Re: Error when using SecurityManager
Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256
All,
On 3/21/18 12:27 PM, Christopher Schultz wrote:
> Okay, I already had closeMethod="close" set. I just need to set up
> a test-case where I can check to see if it's actually doing it.
> Perhaps I can just throw-out my ServletContextListener.
>
> I should be able to:
>
> 0. Disable my Listener 1. Launch Tomcat + my webapp 2.
> Stop/undeploy my webapp
Got this on stdout:
Mar 21, 2018 3:04:00 PM
org.apache.catalina.loader.WebappClassLoaderBase clearReferencesThreads
WARNING: The web application [webapp] appears to have started a thread
named [Abandoned connection cleanup thread] but has failed to stop it.
This is very likely to create a memory leak. Stack trace of thread:
java.lang.Object.wait(Native Method)
java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:143)
com.mysql.jdbc.AbandonedConnectionCleanupThread.run(AbandonedConnection
CleanupThread.java:43)
Thanks again, Connector/J. :( This is library version 5.1.38. I'll try
a later version, but I'm not hopeful.
I have no SQL-related timeouts configured and the Connector/J library
is documented "not to start any Threads unless you use
query-timeouts". Evidently not :(
[time passes]
UPDATE: MySQL Connector/J 5.1.44 appears to use the proper TCCL so
although the "Abandoned connection cleanup thread" continues to run,
it doesn't pin my ClassLoader in memory anymore.
The TCCL appears to be Tomcat's "Common" ClassLoader[1] -- the one
that contains everything under CATALINA_HOME/lib/*.jar. I'll have to
check the code, but I suspect Connector/J is finally using their own
.class's ClassLoader as the TCCL of that thread instead of whatever
ClassLoader triggers the creation of the thread (e.g. The
WebappClassLoader).
The real problem is that they don't yet provide a mechanism to
shut-down that thread from client code.
We'll see if that becomes a problem when I move the JAR file into
WEB-INF/lib. I might be back to pinning the WebappClassLoader.
> 3. Check via JMX to see if the DataSource has shut down
There is no DataSource tree in JMX after calling stop() on the
context, so I think that's satisfactory in terms of behavior. :)
> If that works, I can just remove my listener.
Looks like I can abandon my listener as we move to Tomcat 8.5. (We may
be able to do it in Tomcat 8.0 as well; I'll confirm that, too.)
Thanks,
- -chris
[1] https://tomcat.apache.org/tomcat-8.5-doc/class-loader-howto.html
-----BEGIN PGP SIGNATURE-----
Comment: GPGTools - http://gpgtools.org
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/
iQJRBAEBCAA7FiEEMmKgYcQvxMe7tcJcHPApP6U8pFgFAlqyxVMdHGNocmlzQGNo
cmlzdG9waGVyc2NodWx0ei5uZXQACgkQHPApP6U8pFgVbQ/8CB9FWD0aYk/iaYOW
slofQiUeb7ElxQ6mhHUv3Y1+xWCX0D3rAAhlKsgCeii1r06qjZ7hZvlBQGCYRAae
q7l/ex4Ysc9S19BVkjBzuwALRiZGcEuKzrgb2x2Z+AsCLuIRM8DcfcYe26mpbWea
BF7vD6l5A9xgGv8UBxdCKOpo+r2E4LOrxCuSvcOtPb1AQz9msT3gp41qZUhrRupd
Qu8ucNWqmWwwmkjlQ7Ss1cXdivPfTXSiuCXE7Li+RvnCbbG8WMU7kuOam+OdIk2y
RW3ru05E5mkKJPEjQ82MjZrIABwXalovMgjyRtUXw6eBaVDK9YfOwwAW6egfIc2a
Bnqv9QIE0qu6ROkSCUF0J0zOy4a7c+U2uIqZRZldkjvC+7Ho58/uSHpOuq58/E7z
MpGHW8+NTyBSw/tyQH4NtPYURoq9P7/0ofvt0cLvSvB1GcSazaLEegR7P+GtXA+h
cAf+E05NB4Y044BJO0CQQRdEzUQ2jLo1BihUCvJYXbdyf4sl7eY2EDAN/QYSDxqx
+E3XpSt0ZxFs9oy5tX82mzUb0Bi9rnJ6cndSge8FpLf/T+dZZfY0u8u8a4Om3YpX
RPeghOQVXsUiR0X0GddYLKdBIY8xWR6YBhZxSdAQE7QxDY/fcA44eetI2Ou4Zs4N
t3GynQLasj3q8EHJTI8FILIHV5w=
=0h0y
-----END PGP SIGNATURE-----
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org
Re: Error when using SecurityManager
Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256
Mark,
On 3/21/18 10:33 AM, Mark Thomas wrote:
> On 21/03/18 13:50, Christopher Schultz wrote:
>
> <snip/>
>
>> Hmm, I'll take a look at that.
>>
>> This Listener predates a lot of Tomcat's more recent
>> capabilities. I know that there is a tension between the
>> officially-documented behavior of JNDI DataSources (you always
>> get a new DataSource) and what webapps usually do (always go to
>> JNDI to get the DataSource) and so Tomcat has options by default
>> that re-issue the same DataSource to code.
>>
>> It used to be that failing to dispose of the DataSource left it
>> hanging around. That seemed unclean to me, so I wrote the
>> ServletContextListener to shut it down cleanly.
>
> I'm sure that was the case a while ago. Probably far enough back
> that it was before all the work on memory leak detection and
> prevention.
Okay, I already had closeMethod="close" set. I just need to set up a
test-case where I can check to see if it's actually doing it. Perhaps
I can just throw-out my ServletContextListener.
I should be able to:
0. Disable my Listener
1. Launch Tomcat + my webapp
2. Stop/undeploy my webapp
3. Check via JMX to see if the DataSource has shut down
If that works, I can just remove my listener.
>> My JDBC library is in Tomcat's lib/ directory for a handful of
>> reasons (not the least of which is that Connector/J team can't
>> seem to figure out how to let client code shut down the Threads
>> it creates -- stupidly I might add). Does that impact whether or
>> not Tomcat will auto-close the DataSource?
>
> That should make no difference.
Okay.
Once I figure out whether the DataSource is being shut down, I think
I'd like to bundle the JDBC driver in the web application instead of
always having to drop it into CATALINA_HOME (or CATALINA_BASE). That
then allows me to whitelist a specific JDBC connection under the
SecurityManager instead of giving the JDBC driver AllPermission :)
>> The <Resource> itself is defined in the application's
>> META-INF/context.xml file and nowhere else. How does that impact
>> things?
>
> That is a requirement. It has to be defined in the app to be
> stopped when the app stops.
Good. No problem, here.
Thanks,
- -chris
-----BEGIN PGP SIGNATURE-----
Comment: GPGTools - http://gpgtools.org
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/
iQJRBAEBCAA7FiEEMmKgYcQvxMe7tcJcHPApP6U8pFgFAlqyiAAdHGNocmlzQGNo
cmlzdG9waGVyc2NodWx0ei5uZXQACgkQHPApP6U8pFjthg/9HO/TvVkgrcAPbC0w
b/al/d1Vycew6tG9luBI4lEdtU9jfNcGKBYGBB6XmcY9cs70YHQgS82P4HC6DJ3K
n9gHgqxRErfF17seFY6VWRnZpmfeUCOmtbsYvDy3YHHdN1bBNmlxsgbP8mIsLiMM
3k3U42fbd658pxVrcV37pozoqJUeNArDKuu3pGNg4a1MXbeSnE+uQupJoTNAANre
KSceymw4KGxhlzEpdGiME9eWjctpY9gzGJeZ12qKqAX8uaiesLTRX+d/kE6PnOlG
EGHWOHI0cpYMbWMAKSHiPzbS8Bvk9f33uZ6fwj9uTrNOfH9IaCchTAAQENkwyyhY
a+b0sZfeD1dr4GiN0S/NvHLat51YncRBjUmcxrGdP8rUjEAON1GGkgaBtlFtX8WM
3Av4xB984BzsmMOtzKmY+omeo5ZXQfpVbwSjMoEVSzYlnhv9qzY8ezK3csLzC9s2
o3T3tNjzZsdTXoLjIPtsNQmSrXUUkosYYGORBoIVRztYxIimiCTUtWl+PV6f9fIA
H56qietpt/xooUHdQ99Tc9KJOqmMOM0lo0fl20zu6hWaSu/FCRUJPaAgkti68WxX
prQPye92TcjfOM1OEnOAMkpWUjPQ+l8y3Qk3IUKkmSqmOH3kGUBeCLILZHasNi9Q
auFSh9aJ12+xMZZzrWzCkzpksWY=
=ijRt
-----END PGP SIGNATURE-----
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org
Re: Error when using SecurityManager
Posted by Mark Thomas <ma...@apache.org>.
On 21/03/18 13:50, Christopher Schultz wrote:
<snip/>
> Hmm, I'll take a look at that.
>
> This Listener predates a lot of Tomcat's more recent capabilities. I
> know that there is a tension between the officially-documented
> behavior of JNDI DataSources (you always get a new DataSource) and
> what webapps usually do (always go to JNDI to get the DataSource) and
> so Tomcat has options by default that re-issue the same DataSource to
> code.
>
> It used to be that failing to dispose of the DataSource left it
> hanging around. That seemed unclean to me, so I wrote the
> ServletContextListener to shut it down cleanly.
I'm sure that was the case a while ago. Probably far enough back that it
was before all the work on memory leak detection and prevention.
> My JDBC library is in Tomcat's lib/ directory for a handful of reasons
> (not the least of which is that Connector/J team can't seem to figure
> out how to let client code shut down the Threads it creates --
> stupidly I might add). Does that impact whether or not Tomcat will
> auto-close the DataSource?
That should make no difference.
> The <Resource> itself is defined in the application's
> META-INF/context.xml file and nowhere else. How does that impact things?
That is a requirement. It has to be defined in the app to be stopped
when the app stops.
Mark
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org
Re: Error when using SecurityManager
Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256
Mark,
On 3/21/18 6:24 AM, Mark Thomas wrote:
> On 21/03/18 05:32, Christopher Schultz wrote:
>> All,
>>
>> I'm exploring running my application under a SecurityManager
>
> That's brave.
>
>> (on Tomcat 8.5.29), and I'm getting an unexpected complaint
>> during Tomcat shutdown:
>>
>> Mar 21, 2018 1:26:23 AM
>> org.apache.catalina.core.ApplicationContext log SEVERE:
>> my.JNDIDataSourceShutdownListener: Cannot close DataSource
>> java.lang.reflect.InvocationTargetException at
>> sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at
>> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
jav
>>
>>
a:62)
>> at
>> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sor
>>
>>
Impl.java:43)
>> at java.lang.reflect.Method.invoke(Method.java:498) at
>> my.JNDIDataSourceShutdownListener.close(JNDIDataSourceShutdownListene
r.j
>>
>>
ava:84)
>> at
>> my.JNDIDataSourceShutdownListener.contextDestroyed(JNDIDataSourceShut
dow
>>
>>
nListener.java:63)
>> at
>> org.apache.catalina.core.StandardContext.listenerStop(StandardContext
.ja
>>
>>
va:4800)
>> at
>> org.apache.catalina.core.StandardContext.stopInternal(StandardContext
.ja
>>
>>
va:5437)
>> at
>> org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:226)
>>
>>
at
>> org.apache.catalina.core.ContainerBase$StopChild.call(ContainerBase.j
ava
>>
>>
:1437)
>> at
>> org.apache.catalina.core.ContainerBase$StopChild.call(ContainerBase.j
ava
>>
>>
:1426)
>> at java.util.concurrent.FutureTask.run(FutureTask.java:266) at
>> java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.
jav
>>
>>
a:1142)
>> at
>> java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor
.ja
>>
>>
va:617)
>> at java.lang.Thread.run(Thread.java:748) Caused by:
>> java.security.AccessControlException: access denied
>> ("javax.management.MBeanServerPermission" "createMBeanServer")
>> at
>> java.security.AccessControlContext.checkPermission(AccessControlConte
xt.
>>
>>
java:472)
>> at
>> java.security.AccessController.checkPermission(AccessController.java:
884
>>
>>
)
>> at
>> java.lang.SecurityManager.checkPermission(SecurityManager.java:549)
>>
>>
at
>> java.lang.management.ManagementFactory.getPlatformMBeanServer(Managem
ent
>>
>>
Factory.java:465)
>> at
>> org.apache.tomcat.dbcp.dbcp2.BasicDataSource.close(BasicDataSource.ja
va:
>>
>>
1950)
>> ... 15 more
>>
>>
>> My JNDIDataSourceShutdownListener fetches the JNDI DataSource
>> from the JNDI tree and reflectively calls "close" on it to free
>> it up.
>>
>> This seems unexpected because:
>>
>> 1. I would expect that the MBeanServer would still be available
>> even during application shut-down (before Tomcat itself shuts
>> down).
>>
>> 2. I would expect that BasicDataSource would use a
>> PrivilegedAction if it needed to create an MBeanServer in this
>> way.
>>
>> I can obviously just give this permission to the application, but
>> I think it probably makes more sense to solve this in a different
>> way -- through Tomcat itself.
>
> Looks like the fix would need to be in DBCP2 since that is the
> source of that code.
>
>> Any ideas? I'm certainly no expert in SecurityManagers and what
>> Tomcat is doing under the covers with its DataSources.
>
> Doesn't Tomcat shutdown the JNDI datasource without
> JNDIDataSourceShutdownListener doing it explicitly? See closeMethod
> in the docs/code. With the shut down initiated from Tomcat code,
> the right permissions should be in place.
Hmm, I'll take a look at that.
This Listener predates a lot of Tomcat's more recent capabilities. I
know that there is a tension between the officially-documented
behavior of JNDI DataSources (you always get a new DataSource) and
what webapps usually do (always go to JNDI to get the DataSource) and
so Tomcat has options by default that re-issue the same DataSource to
code.
It used to be that failing to dispose of the DataSource left it
hanging around. That seemed unclean to me, so I wrote the
ServletContextListener to shut it down cleanly.
My JDBC library is in Tomcat's lib/ directory for a handful of reasons
(not the least of which is that Connector/J team can't seem to figure
out how to let client code shut down the Threads it creates --
stupidly I might add). Does that impact whether or not Tomcat will
auto-close the DataSource?
The <Resource> itself is defined in the application's
META-INF/context.xml file and nowhere else. How does that impact things?
- -chris
-----BEGIN PGP SIGNATURE-----
Comment: GPGTools - http://gpgtools.org
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/
iQIzBAEBCAAdFiEEMmKgYcQvxMe7tcJcHPApP6U8pFgFAlqyYxMACgkQHPApP6U8
pFh/yhAAp5gKGvM6WoznC7SK9lhD0OMhe4BY9/MkehYfh6ZvEBWKobCa6C1kjE+2
n9ePARG7YSLQT+viIn+bFhmYY301mko7ry//eIOxvTDlt+ptzN4jskcOwKP2t0S8
P69J6JISw3j8maVHKoYpwhtVIPJQFv/80o5brfmpfESiXK/eZgU9EVdsVa1JZnvh
SttXOw0XsfjNF5CnVZh7cTock5QjW1bBh4lyP7Dksxd2mohXtZVmrk/CMmiJ09QC
l7LEdVELV6BjFncwwkmizCLkzo5teLJivdDXmHie2ixxLwmjVQl2Jv8azLfRRZUI
8TzVf1L0gqEh4jNip69jJ2+fZlBkGZ67tML3Ex8VpgCKmgrYRPRpy6BBe+25MCU/
SAZXKH2wA/PuGcPjry/odBi2oqIJOECRLbYfT+1gQ5jlYnfILjLtvtkIj5DEU4DU
N+KEfwNWZVO8napI+wfue/dy/xPxMmJISGJ0kOSrEYey0XhQ42l3NhYOeR9wQ0uS
wJOhlEbS/WrbyiAaNP64BgPnzkpMRIACBrKx+/q9kVE6nC+wFr03fblOloi9288v
wr2BmydktWPuuuKwpiZzvYOallwzCKQYXGOLjQcqdMq/cpVTyvgCXb3+z3HrHG0e
HSJdgHIffzh2JzNaXY9BdDKzw6HMKu2IhYiH9B6v0Lgau+CAZ2c=
=PUEQ
-----END PGP SIGNATURE-----
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org
Re: Error when using SecurityManager
Posted by Mark Thomas <ma...@apache.org>.
On 21/03/18 05:32, Christopher Schultz wrote:
> All,
>
> I'm exploring running my application under a SecurityManager
That's brave.
> (on
> Tomcat 8.5.29), and I'm getting an unexpected complaint during Tomcat
> shutdown:
>
> Mar 21, 2018 1:26:23 AM org.apache.catalina.core.ApplicationContext log
> SEVERE: my.JNDIDataSourceShutdownListener: Cannot close DataSource
> java.lang.reflect.InvocationTargetException
> at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> at
> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.jav
> a:62)
> at
> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessor
> Impl.java:43)
> at java.lang.reflect.Method.invoke(Method.java:498)
> at
> my.JNDIDataSourceShutdownListener.close(JNDIDataSourceShutdownListener.j
> ava:84)
> at
> my.JNDIDataSourceShutdownListener.contextDestroyed(JNDIDataSourceShutdow
> nListener.java:63)
> at
> org.apache.catalina.core.StandardContext.listenerStop(StandardContext.ja
> va:4800)
> at
> org.apache.catalina.core.StandardContext.stopInternal(StandardContext.ja
> va:5437)
> at
> org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:226)
> at
> org.apache.catalina.core.ContainerBase$StopChild.call(ContainerBase.java
> :1437)
> at
> org.apache.catalina.core.ContainerBase$StopChild.call(ContainerBase.java
> :1426)
> at java.util.concurrent.FutureTask.run(FutureTask.java:266)
> at
> java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.jav
> a:1142)
> at
> java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.ja
> va:617)
> at java.lang.Thread.run(Thread.java:748)
> Caused by: java.security.AccessControlException: access denied
> ("javax.management.MBeanServerPermission" "createMBeanServer")
> at
> java.security.AccessControlContext.checkPermission(AccessControlContext.
> java:472)
> at
> java.security.AccessController.checkPermission(AccessController.java:884
> )
> at
> java.lang.SecurityManager.checkPermission(SecurityManager.java:549)
> at
> java.lang.management.ManagementFactory.getPlatformMBeanServer(Management
> Factory.java:465)
> at
> org.apache.tomcat.dbcp.dbcp2.BasicDataSource.close(BasicDataSource.java:
> 1950)
> ... 15 more
>
>
> My JNDIDataSourceShutdownListener fetches the JNDI DataSource from the
> JNDI tree and reflectively calls "close" on it to free it up.
>
> This seems unexpected because:
>
> 1. I would expect that the MBeanServer would still be available even
> during application shut-down (before Tomcat itself shuts down).
>
> 2. I would expect that BasicDataSource would use a PrivilegedAction if
> it needed to create an MBeanServer in this way.
>
> I can obviously just give this permission to the application, but I
> think it probably makes more sense to solve this in a different way --
> through Tomcat itself.
Looks like the fix would need to be in DBCP2 since that is the source of
that code.
> Any ideas? I'm certainly no expert in SecurityManagers and what Tomcat
> is doing under the covers with its DataSources.
Doesn't Tomcat shutdown the JNDI datasource without
JNDIDataSourceShutdownListener doing it explicitly? See closeMethod in
the docs/code. With the shut down initiated from Tomcat code, the right
permissions should be in place.
Mark
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org