Let $M$ be an $4m$-dimensional Riemannian manifold. We can then form the Pontryagin classes $p_k(TM)$ of the tangent bundle using Chern-Weil theory. For any sequence of numbers $k_1, \dots, k_l$ such that $$ k_1 + \dots + k_l = m,$$ we have that $$p_{k_1}(TM) \wedge \cdots \wedge p_{k_l}(TM) = P(R) \mathrm{vol},$$ where $P(R)$ is some homogeneous invariant polynomial of degree $2m$ in the coefficients of the curvature tensor and $\mathrm{vol}$ is the volume form of the metric. Also the Euler class $\chi(TM)$ has this form.
My question is the converse: Given an invariant polynomial $P(R)$, homogeneous of degree $2m$, in the coefficients of the curvature tensor, what are the conditions on $P$ needed to ensure that $P(R)\mathrm{vol}$ is given by a (linear combination of) characteristic classes as above?
It seems to me that this question should have a purely algebraic answer.
Edit: So I guess, more explicitly, my question is the following. There are two types of "invariant polynomials" in the game:
(1) One is determined by a $2m$-homogeneous, $O(n)$- or $SO(n)$-invariant polynomial $Q$ in the variables $R_{ij}$ (i.e. on the space of skew-symmetric matrices), and you get a $4m$-form by inserting into it the matrix of two-forms with entries $$R_{ij} := R_{ijkl} dx^k dx^l$$ with respect to some local trivialisation. By invariance of $Q$, this is independent of the choice of coordinates, and by the virtue of Chern-Weil-theory, the de-Rham-class defined by $Q(R_{ij})$ is independent of the metric used to define it.
(2) The second one is determined by a homogeneous polynomial $P$ of degree $2m$ in the variables $R_{ijkl}$, which have the symmetries of the curvature tensor, hence we have a polynomial on the space of algebraic curvature tensors, which we assume to be invariant under the action of $SO(n)$ or $O(n)$. Now we get a $4m$-form $P(R_{ijkl})\mathrm{vol}$, which is well-defined by invariance, but might depend on the metric.
By the theorem of Peter Gilkey, mentioned by Robert Bryant in the comments below, all polynomials obtained by the first method can be written in terms of Pontrjagin classes, as done above. On the other hands, polynomials obtained as in (1) can be obtained by the method of (2) as well.
Now: What are the conditions on $P$ in order that $P(R_{ijkl})\mathrm{vol}$ is actually of the type (1)? Of course, one can expand both sides in order to get some conditions, but this seems to be very messy. So I guess, my question is: Is there a slick way to formulate a condition on $P$ to ensure that the form it defines is of the type (1)?