Skip to content

Serialize the LogEvent message without MarshalledObject - #4271

Merged
ppkarwasz merged 6 commits into
2.xfrom
feat/2.x/log-event-proxy-wrapped-message
Sep 30, 2026
Merged

ppkarwasz merged 6 commits into
2.xfrom
feat/2.x/log-event-proxy-wrapped-message

Conversation

@ppkarwasz

@ppkarwasz ppkarwasz commented Aug 31, 2026 •

Copy link
Copy Markdown
Member

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.LogEventProxy transported its Message delegate inside a java.rmi.MarshalledObject. MarshalledObject.get() deserializes its embedded bytes on its own ObjectInputStream, so the nested message bypassed FilteredObjectInputStream and any ObjectInputFilter set on the outer stream. Only a process-wide jdk.serialFilter applied to it.

The message is now written with SerializationUtil.writeWrappedObject/readWrappedObject, like the contents of ObjectMessage, so the nested stream is filtered in the same way as the outer stream (see #4365). java.rmi.MarshalledObject is removed from the allowlist of FilteredObjectInputStream, from its Javadoc and from the jdk.serialFilter used in tests.

Fallback for unserializable values: a Message is always Serializable, but it may contain fields that are not. writeWrappedObject now takes a fallback value, which is written if the object is not Serializable or its serialization fails; failures are logged as warnings:

  • LogEventProxy writes a SimpleMessage with the formatted message, as it did with MarshalledObject. A message that cannot be read on the receiving side also still degrades to a SimpleMessage.
  • 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, which is closer to how context data is usually rendered.

UnserializableMessage in log4j-api-test is a reusable example of a Serializable object that cannot be serialized, and is used to test these fallbacks.

Errors while reading nested objects: readWrappedObject used to replace any nested object that could not be read with null, 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 with null, so that, for example, a custom Message unknown to the reader still degrades to a SimpleMessage.

New tests cover the fallbacks, using UnserializableMessage, and a nested java.net.URI, which is not on the allowlist, in ObjectMessage and in Log4jLogEvent. The latter is tagged serialization, so it also runs with the process-wide jdk.serialFilter.

Compatibility: this changes the serialized form of LogEventProxy. Checked against Log4j 2.26.1 with a plain ObjectInputStream:

  • Log events serialized by earlier versions can no longer be deserialized (OptionalDataException).
  • Log events serialized with this change remain readable by older versions, with the message downgraded to a SimpleMessage. This no longer holds with Simplify the serialization of SortedArrayStringMap #4272, which changes the serialized form of the context data.

The serialized-event fixture in Log4jLogEventTest predated nanoOfMillisecond and the trace-context fields and has been regenerated; the stale serializedEvent.dat test resource was unused (SerializedLayoutTest always rewrites it before reading) and is removed.

Stacked on #4270.

@ramanathan1504

ramanathan1504 commented Sep 7, 2026 •

Copy link
Copy Markdown
Contributor

The MarshalledObject removal looks complete. Dropping serializedEvent.dat is fine too, testDeserialization calls testSerialization() first.

One problem. A Message with a non-serializable field used to be caught by marshall and replaced by messageString. Now it throws NotSerializableException out of the
whole event. SerializedLayout.toByteArray catches that and still returns the partly written buffer. Fix inline.

@ramanathan1504

Copy link
Copy Markdown
Contributor

Old streams also stop being readable. defaultReadObject still resolves java.rmi.MarshalledObject from the stream's own field descriptor before dropping the value, and
that class is off the allowlist now. So events written by 2.8.2 to 2.25.x are rejected. That is a wire break on 2.x, worth a PMC note rather than riding in with the
hardening.

And nothing tests the new behaviour. Neither the filtered nested read nor the fallback to SimpleMessage is covered. The regenerated base64 fixture passes either way, even
with an unfiltered nested stream.

@ppkarwasz
ppkarwasz removed this pull request from stack #4273 September 29, 2026 16:38
@ppkarwasz
ppkarwasz added this pull request to stack #4367 September 29, 2026 16:39
@ppkarwasz
ppkarwasz force-pushed the feat/2.x/log-event-proxy-wrapped-message branch from 3df4199 to 084309a Compare September 29, 2026 17:38
@ppkarwasz ppkarwasz changed the title Serialize the LogEvent message delegate through the filtered wrapper Serialize the LogEvent message without MarshalledObject Sep 29, 2026
@ppkarwasz

Copy link
Copy Markdown
Member Author

That is a wire break on 2.x, worth a PMC note rather than riding in with the hardening.

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.

@ppkarwasz

Copy link
Copy Markdown
Member Author

And nothing tests the new behaviour. Neither the filtered nested read nor the fallback to SimpleMessage is covered. The regenerated base64 fixture passes either way, even with an unfiltered nested stream.

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:

  1. If deserialization failed due to a missing class (e.g. custom Message implementation), we degrade to SimpleMessage first and null as a fallback.
  2. If deserialization failed due to an IOException in the stream, we rethrow.

Fixed in 9270577

@ppkarwasz

Copy link
Copy Markdown
Member Author

SerializedLayout.toByteArray catches that and still returns the partly written buffer. Fix inline.

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.

@ramanathan1504

Copy link
Copy Markdown
Contributor

@ppkarwasz

The write side now degrades to keep the event, but the read side throws the whole event away. testJavaIoSerializableWithClassNotOnAllowlist locks that in: one
ObjectMessage holding a java.net.URI and the record is gone, timestamp, level and formatted message with it. Any application object behaves the same, the allowlist is
only the four prefixes.

LogEventProxy still writes messageString and message() still falls back to new SimpleMessage(messageString), so the degrade path is there and the rethrow makes it
unreachable. Was losing the whole event the intent, or only for corrupted data?

@ramanathan1504 ramanathan1504 added the port-done No further port needed: all required ports are merged, or none was needed label Sep 30, 2026
Base automatically changed from feat/2.x/serial-util-java9-filter to 2.x September 30, 2026 10:45
`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>
@ramanathan1504
ramanathan1504 force-pushed the feat/2.x/log-event-proxy-wrapped-message branch from 07e6fb3 to 546e551 Compare September 30, 2026 10:45
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>
@ppkarwasz

ppkarwasz commented Sep 30, 2026 •

Copy link
Copy Markdown
Member Author

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:

  • If the stream is corrupted, truncated or contains classes that should be there, the method throws as if was read from the outer stream.
  • The only case, when we degrade to SimpleMessage is when that particular Message class is not available on the receiving system.

I added the test for the degraded read case in 410bfde.

@ppkarwasz
ppkarwasz merged commit 9cb17cf into 2.x Sep 30, 2026
13 checks passed
@ppkarwasz
ppkarwasz deleted the feat/2.x/log-event-proxy-wrapped-message branch September 30, 2026 21:07
@ramanathan1504 ramanathan1504 added port-to-2.25.x port-to-2.26.x and removed port-done No further port needed: all required ports are merged, or none was needed labels Oct 1, 2026
@ramanathan1504 ramanathan1504 added this to the 2.26.2 milestone Oct 1, 2026
ppkarwasz added a commit that referenced this pull request Nov 22, 2026
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Development

Successfully merging this pull request may close these issues.

2 participants