Repository navigation
Serialize the LogEvent message without MarshalledObject - #4271
Conversation
3b05d04 to
3df4199
Compare
|
The One problem. A |
|
Old streams also stop being readable. And nothing tests the new behaviour. Neither the filtered nested read nor the fallback to |
3df4199 to
084309a
Compare
LogEvent message delegate through the filtered wrapperLogEvent message without MarshalledObject
I added that to the changelog. These days we don't expect anybody to send log events in serialized form through the network, which only leaves the case of serializing an event when an application goes down and deserializing it upon startup. This will work, unless the application was upgraded of course. |
You are completely right on this one too. Since we want to be neutral regarding serialization, it makes sense to rethrow exceptions that come from corrupted data to the reader, more precisely:
Fixed in 9270577 |
Now it should be much less probable for serialization to fail, since I added back the fallbacks, but I'll address this with a new PR at the top of the stack. |
9270577 to
117b4a7
Compare
117b4a7 to
15b526f
Compare
15b526f to
07e6fb3
Compare
|
The write side now degrades to keep the event, but the read side throws the whole event away.
|
`Log4jLogEvent.LogEventProxy` used `java.rmi.MarshalledObject` to transport its `Message` delegate. `MarshalledObject.get()` deserializes its embedded bytes on a private, unfiltered `ObjectInputStream`, so the delegate escaped the deserialization allow-list entirely. The message is now written with the same `SerializationUtil.writeWrappedObject`/`readWrappedObject` mechanism used by `ObjectMessage`, which re-applies the allow-list to the nested stream, and `java.rmi.MarshalledObject` is removed from the allow-list. Compatibility: log events serialized by Log4j 2.8.2-2.25.x are rejected by newer readers, since `MarshalledObject` no longer passes the filter. Events serialized by this version remain readable by older versions, with the message downgraded to a `SimpleMessage`. The serialized-event fixture in `Log4jLogEventTest` is regenerated accordingly, and the stale, unused `serializedEvent.dat` resource is removed (`SerializedLayoutTest` always rewrites it before reading). Part of the hardening series from #4168. Assisted-By: Claude Fable 5 <noreply@anthropic.com>
Assisted-By: Claude Opus 5.5 <noreply@anthropic.com>
`SerializationUtil.writeWrappedObject` now takes a fallback, which is written instead of the object if it is not `Serializable` or its serialization fails. Failed serializations are logged as warnings. - `LogEventProxy` writes a `SimpleMessage` with the formatted message, as it did when the message was wrapped in a `MarshalledObject`. - `ObjectMessage` writes its formatted message. - `ParameterizedMessage` and `SortedArrayStringMap` write the string representation of the value. For `SortedArrayStringMap`, this replaces the `null` previously written for non-serializable values. `UnserializableMessage` in `log4j-api-test` provides a reusable example of a `Serializable` object that cannot be serialized. Assisted-By: Claude Opus 5.5 <noreply@anthropic.com>
Assisted-By: Claude Opus 5.5 <noreply@anthropic.com>
`readWrappedObject` returned `null` for any nested object that could not be read, including classes rejected by the filter of the stream. The nested stream now behaves like the outer one: `IOException`s, including filter rejections, are thrown. Only classes that are not available on the reader, `ClassNotFoundException` and `LinkageError`, are still replaced with `null`. Adds tests with a nested `java.net.URI`, which is not on the allowlist, in `ObjectMessage` and in a `Log4jLogEvent`. Assisted-By: Claude Opus 5.5 <noreply@anthropic.com>
07e6fb3 to
546e551
Compare
A message whose class is allowed by the deserialization filter, but not available to the reader, degrades to a `SimpleMessage` with the formatted message. The Base64 fixtures in `Log4jLogEventTest` are folded into multiple lines. Assisted-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Hi @ramanathan1504, Yes, the read path intentionally throws if the filter rejects a class. I want our deserialization code to be as transparent as possible:
I added the test for the degraded read case in 410bfde. |
Reverts the following commits, which will be cherry-picked from #4466 once it is merged into `2.25.x`: - 5250e59 Keep `util.internal` unexported: copy the wrapped-object helpers into `log4j-core` - 7e6eece Simplify the serialization of `ObjectArrayMessage` and `ParameterizedMessage` (#4274) - ad80723 Simplify the serialization of `SortedArrayStringMap` (#4272) - ea610fe Serialize the `LogEvent` message without `MarshalledObject` (#4271) - ba40f9c Apply the deserialization filter in test helpers on Java 9+ (#4270) - e8700bb Reject dynamic proxies in `FilteredObjectInputStream` (#4269) - 84a9266 Apply the outer `ObjectInputFilter` to nested streams (#4365) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Important
This PR is part of the deserialization hardening work tracked in #4168. The Logging Services PMC does not use nor recommend Java serialization/deserialization, and our security FAQ has long documented this position. This work is submitted solely to reduce the false-positive "vulnerability" reports that keep being filed regardless of that FAQ. Its utility for end users is close to zero.
Since 2.8.2,
Log4jLogEvent.LogEventProxytransported itsMessagedelegate inside ajava.rmi.MarshalledObject.MarshalledObject.get()deserializes its embedded bytes on its ownObjectInputStream, so the nested message bypassedFilteredObjectInputStreamand anyObjectInputFilterset on the outer stream. Only a process-widejdk.serialFilterapplied to it.The message is now written with
SerializationUtil.writeWrappedObject/readWrappedObject, like the contents ofObjectMessage, so the nested stream is filtered in the same way as the outer stream (see #4365).java.rmi.MarshalledObjectis removed from the allowlist ofFilteredObjectInputStream, from its Javadoc and from thejdk.serialFilterused in tests.Fallback for unserializable values: a
Messageis alwaysSerializable, but it may contain fields that are not.writeWrappedObjectnow takes a fallback value, which is written if the object is notSerializableor its serialization fails; failures are logged as warnings:LogEventProxywrites aSimpleMessagewith the formatted message, as it did withMarshalledObject. A message that cannot be read on the receiving side also still degrades to aSimpleMessage.ObjectMessagewrites its formatted message.ParameterizedMessageandSortedArrayStringMapwrite the string representation of the value. ForSortedArrayStringMapthis replaces thenullpreviously written for non-serializable values, which is closer to how context data is usually rendered.UnserializableMessageinlog4j-api-testis a reusable example of aSerializableobject that cannot be serialized, and is used to test these fallbacks.Errors while reading nested objects:
readWrappedObjectused to replace any nested object that could not be read withnull, including classes rejected by the filter of the stream. The nested stream now behaves like the outer one:IOExceptions, including filter rejections and corrupted data, are thrown. Only classes that are not available on the reader (ClassNotFoundException,LinkageError) are still replaced withnull, so that, for example, a customMessageunknown to the reader still degrades to aSimpleMessage.New tests cover the fallbacks, using
UnserializableMessage, and a nestedjava.net.URI, which is not on the allowlist, inObjectMessageand inLog4jLogEvent. The latter is taggedserialization, so it also runs with the process-widejdk.serialFilter.Compatibility: this changes the serialized form of
LogEventProxy. Checked against Log4j 2.26.1 with a plainObjectInputStream:OptionalDataException).SimpleMessage. This no longer holds with Simplify the serialization ofSortedArrayStringMap#4272, which changes the serialized form of the context data.The serialized-event fixture in
Log4jLogEventTestpredatednanoOfMillisecondand the trace-context fields and has been regenerated; the staleserializedEvent.dattest resource was unused (SerializedLayoutTestalways rewrites it before reading) and is removed.Stacked on #4270.