Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

新版本 ognl 有一个 UseStricterInvocation 模块,默认开启,执行 ognl 表达式容易出现: cannot be called from within OGNL invokeMethod() under stricter invocation mode #2955

Open
hengyunabc opened this issue Nov 21, 2024 · 5 comments
Milestone

Comments

@hengyunabc
Copy link
Collaborator

$ ognl '@java.lang.ClassLoader@getSystemClassLoader().loadClass("aaa")'
Failed to execute ognl, exception message: ognl.MethodFailedException: Method "getSystemClassLoader" failed for object class java.lang.ClassLoader [java.lang.IllegalAccessException: Method [public static java.lang.ClassLoader java.lang.ClassLoader.getSystemClassLoader()] cannot be called from within OGNL invokeMethod() under stricter invocation mode.], please check $HOME/logs/arthas/arthas.log for more details.
    /**
     * Control usage of "stricter" invocation processing by invokeMethod() using the JVM options:
     * -Dognl.UseStricterInvocation=true
     * -Dognl.UseStricterInvocation=false
     * <p>
     * Note: Using the "true" value has the same effect as omitting the option completely.
     * The default behaviour is to use the "stricter" invocation processing.
     * Using the "false" value reverts to the older "less strict" invocation processing
     * (in the event the "stricter" processing causes issues for existing applications).
     */
    static final String USE_STRICTER_INVOCATION = "ognl.UseStricterInvocation";

需要和 arthas 本身的 options strict 对应起来。

@hengyunabc hengyunabc added this to the 4.0.5 milestone Nov 21, 2024
hengyunabc added a commit that referenced this issue Nov 21, 2024
@tasier
Copy link

tasier commented Dec 4, 2024

I recently encountered this issue as well. Has it been resolved in the new version?

@p1n93r
Copy link

p1n93r commented Dec 4, 2024

@tasier

The current version of Arthas (4.0.4) has not resolved this issue with OGNL. You can solve it using the following steps:

  1. First, check Arthas' own classloader to get its classloader hash:
classloader -l | grep taobao
  1. Specify the Arthas classloader hash and disable the OGNL strict option:
ognl -c 1eb98135 '#field = @com.taobao.arthas.core.util.reflect.FieldUtils@getDeclaredField(Class.forName("ognl.OgnlRuntime"),"_useStricterInvocation",true), #modifiers = @com.taobao.arthas.core.util.reflect.FieldUtils@getDeclaredField(#field.getClass(),"modifiers",true),#modifiers.setInt(#field, #field.getModifiers() & [email protected]@FINAL),#field.set(null,false)'

@tasier
Copy link

tasier commented Dec 5, 2024

@tasier

The current version of Arthas (4.0.4) has not resolved this issue with OGNL. You can solve it using the following steps:

  1. First, check Arthas' own classloader to get its classloader hash:
classloader -l | grep taobao
  1. Specify the Arthas classloader hash and disable the OGNL strict option:
ognl -c 1eb98135 '#field = @com.taobao.arthas.core.util.reflect.FieldUtils@getDeclaredField(Class.forName("ognl.OgnlRuntime"),"_useStricterInvocation",true), #modifiers = @com.taobao.arthas.core.util.reflect.FieldUtils@getDeclaredField(#field.getClass(),"modifiers",true),#modifiers.setInt(#field, #field.getModifiers() & [email protected]@FINAL),#field.set(null,false)'

I seems not work for me. I also use Arthas 4.0.4. Following is the exception stack.

[arthas-command-execute] WARN c.t.a.c.command.klass100.OgnlCommand -ognl: failed execute express: #field = @com.taobao.arthas.core.util.reflect.FieldUtils@getDeclaredField(Class.forName("ognl.OgnlRuntime"),"_useStricterInvocation",true), #modifiers = @com.taobao.arthas.core.util.reflect.FieldUtils@getDeclaredField(#field.getClass(),"modifiers",true),#modifiers.setInt(#field, #field.getModifiers() & [email protected]@FINAL),#field.set(null,false)
com.taobao.arthas.core.command.express.ExpressException: java.lang.NullPointerException: target is null for method setInt
at com.taobao.arthas.core.command.express.OgnlExpress.get(OgnlExpress.java:39)
at com.taobao.arthas.core.command.klass100.OgnlCommand.process(OgnlCommand.java:106)
at com.taobao.arthas.core.shell.command.impl.AnnotatedCommandImpl.process(AnnotatedCommandImpl.java:82)
at com.taobao.arthas.core.shell.command.impl.AnnotatedCommandImpl.access$100(AnnotatedCommandImpl.java:18)
at com.taobao.arthas.core.shell.command.impl.AnnotatedCommandImpl$ProcessHandler.handle(AnnotatedCommandImpl.java:111)
at com.taobao.arthas.core.shell.command.impl.AnnotatedCommandImpl$ProcessHandler.handle(AnnotatedCommandImpl.java:108)
at com.taobao.arthas.core.shell.system.impl.ProcessImpl$CommandProcessTask.run(ProcessImpl.java:385)
at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:539)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
at java.base/java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:304)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
at java.base/java.lang.Thread.run(Thread.java:840)
Caused by: java.lang.NullPointerException: target is null for method setInt
at ognl.OgnlRuntime.callMethod(OgnlRuntime.java:2032)
at ognl.ASTMethod.getValueBody(ASTMethod.java:97)
at ognl.SimpleNode.evaluateGetValueBody(SimpleNode.java:212)
at ognl.SimpleNode.getValue(SimpleNode.java:258)
at ognl.ASTChain.getValueBody(ASTChain.java:141)
at ognl.SimpleNode.evaluateGetValueBody(SimpleNode.java:212)
at ognl.SimpleNode.getValue(SimpleNode.java:258)
at ognl.ASTSequence.getValueBody(ASTSequence.java:63)
at ognl.SimpleNode.evaluateGetValueBody(SimpleNode.java:212)
at ognl.SimpleNode.getValue(SimpleNode.java:258)
at ognl.Ognl.getValue(Ognl.java:586)
at ognl.Ognl.getValue(Ognl.java:688)
at ognl.Ognl.getValue(Ognl.java:658)
at com.taobao.arthas.core.command.express.OgnlExpress.get(OgnlExpress.java:36)

@p1n93r
Copy link

p1n93r commented Dec 5, 2024

@tasier

If you're using JDK 17 or higher, you can try the following steps:

  1. First, check Arthas' own classloader to get its classloader hash:
classloader -l | grep taobao
  1. Specify the Arthas classloader hash and disable the OGNL strict option:
ognl -c 51e2f7d9 '#field = @com.taobao.arthas.core.util.reflect.FieldUtils@getDeclaredField(Class.forName("ognl.OgnlRuntime"),"_useStricterInvocation",true),#unsafe = @com.taobao.arthas.common.UnsafeUtils@UNSAFE, #staticFieldBaseMethod = new com.taobao.arthas.core.advisor.ArthasMethod(#unsafe.getClass(),"staticFieldBase","(Ljava/lang/reflect/Field;)Ljava/lang/Object"),#staticFieldOffsetMethod = new com.taobao.arthas.core.advisor.ArthasMethod(#unsafe.getClass(),"staticFieldOffset","(Ljava/lang/reflect/Field;)Ljava/lang/Object"),#putBooleanMethod = new com.taobao.arthas.core.advisor.ArthasMethod(#unsafe.getClass(),"putBoolean","(Ljava/lang/Object;JZ)V"),#staticFieldBase = #staticFieldBaseMethod.invoke(#unsafe,#field),#staticFieldOffset = #staticFieldOffsetMethod.invoke(#unsafe,#field),#putBooleanMethod.invoke(#unsafe, #staticFieldBase, #staticFieldOffset, false)'

Its effect is equivalent to the following code:

Field field = OgnlRuntime.class.getDeclaredField("_useStricterInvocation");
field.setAccessible(true);
// 获取字段的内存偏移量和基址
Object staticFieldBase = UnsafeUtils.UNSAFE.staticFieldBase(field);
long staticFieldOffset = UnsafeUtils.UNSAFE.staticFieldOffset(field);

// 修改字段的值
UnsafeUtils.UNSAFE.putBoolean(staticFieldBase, staticFieldOffset, strict);

@tasier
Copy link

tasier commented Dec 6, 2024

@tasier

If you're using JDK 17 or higher, you can try the following steps:

  1. First, check Arthas' own classloader to get its classloader hash:
classloader -l | grep taobao
  1. Specify the Arthas classloader hash and disable the OGNL strict option:
ognl -c 51e2f7d9 '#field = @com.taobao.arthas.core.util.reflect.FieldUtils@getDeclaredField(Class.forName("ognl.OgnlRuntime"),"_useStricterInvocation",true),#unsafe = @com.taobao.arthas.common.UnsafeUtils@UNSAFE, #staticFieldBaseMethod = new com.taobao.arthas.core.advisor.ArthasMethod(#unsafe.getClass(),"staticFieldBase","(Ljava/lang/reflect/Field;)Ljava/lang/Object"),#staticFieldOffsetMethod = new com.taobao.arthas.core.advisor.ArthasMethod(#unsafe.getClass(),"staticFieldOffset","(Ljava/lang/reflect/Field;)Ljava/lang/Object"),#putBooleanMethod = new com.taobao.arthas.core.advisor.ArthasMethod(#unsafe.getClass(),"putBoolean","(Ljava/lang/Object;JZ)V"),#staticFieldBase = #staticFieldBaseMethod.invoke(#unsafe,#field),#staticFieldOffset = #staticFieldOffsetMethod.invoke(#unsafe,#field),#putBooleanMethod.invoke(#unsafe, #staticFieldBase, #staticFieldOffset, false)'

Its effect is equivalent to the following code:

Field field = OgnlRuntime.class.getDeclaredField("_useStricterInvocation");
field.setAccessible(true);
// 获取字段的内存偏移量和基址
Object staticFieldBase = UnsafeUtils.UNSAFE.staticFieldBase(field);
long staticFieldOffset = UnsafeUtils.UNSAFE.staticFieldOffset(field);

// 修改字段的值
UnsafeUtils.UNSAFE.putBoolean(staticFieldBase, staticFieldOffset, strict);

Thank you very much. It works now.
Does Arthas consider to support merge these two commands into one? such as

ognl -c `classloader -l | grep taobao`   '#field = @com.taobao.arthas.core.util.reflect.FieldUtils@getDeclaredField(Class.forName("ognl.OgnlRuntime"),"_useStricterInvocation",true),#unsafe = @com.taobao.arthas.common.UnsafeUtils@UNSAFE, #staticFieldBaseMethod = new com.taobao.arthas.core.advisor.ArthasMethod(#unsafe.getClass(),"staticFieldBase","(Ljava/lang/reflect/Field;)Ljava/lang/Object"),#staticFieldOffsetMethod = new com.taobao.arthas.core.advisor.ArthasMethod(#unsafe.getClass(),"staticFieldOffset","(Ljava/lang/reflect/Field;)Ljava/lang/Object"),#putBooleanMethod = new com.taobao.arthas.core.advisor.ArthasMethod(#unsafe.getClass(),"putBoolean","(Ljava/lang/Object;JZ)V"),#staticFieldBase = #staticFieldBaseMethod.invoke(#unsafe,#field),#staticFieldOffset = #staticFieldOffsetMethod.invoke(#unsafe,#field),#putBooleanMethod.invoke(#unsafe, #staticFieldBase, #staticFieldOffset, false)'

A bash shell command style like.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants