You are right that there is a problem. At the extreme, suppose no-one ever went off treatment. Survival time would be identical to time on treatment, giving an apparently infinite benefit even if the true benefit was zero.
Doing better than this still requires some information about how treatment length ends up being the way it is. This is partly a solved problem, but partly a definitively unsolved problem. It helps that you seem to have a single treatment period, so that everyone being treated at time $t$ has been treated for the same amount of time.
Let's start simple. Suppose that some doctors had a policy of mostly treating people for six months and others had a policy of mostly treating people for a year. Even if you didn't know who'd had which policy, you could count time forward from six months (ignoring anyone who died earlier), and see if the binary variable 'treatment after six months' was associated with survival after six months. With no more than the usual caveats, that would tell you whether longer treatment was better.
In the other direction, suppose some types of patient were mostly treated for six months and other types for twelve months. You could do the same analysis, but now treatment is completely confounded with the type of patient, so you would need separate information on the survival difference by type of patient, which you may well not be able to get.
Those two extremes cover basically everything, it's just that we have more than two possible time periods for length of treatment.
At each point in time, some patients stop treatment (but are still followed up) and others continue (and are still followed up). A Cox model with time-dependent treatment variable(s) is the limiting case of the split-by-time analysis as you allow more and more splits. It will say whether lower hazard for treated people compared to untreated people at the same point in time. You would also have to decide how to model time-since-stopping, but that's just an ordinary modelling question; you know treatment history at time $t$ and you just need to decide how to code it as a set of predictor variables. This model will not tell you directly whether that difference is caused by treatment.
If there is practice variation, so that otherwise similar patients can have different treatment durations, you could model the time to treatment discontinuation and do matching or an inverse-probability weighting thing to try to estimate whether, for the same patient, there would be a difference in outcome with different treatment duration.
If there is little or no practice variation, so people mostly stay on treatment until, say, they can't take any more or until they feel better or until their insurance stops paying, you will have complete confounding between treatment and the reason for treatment. In that case you would need external information on the effect of the reason for treatment on survival to either estimate or bound the effect of receiving treatment.
And as a final point, you might well want a time-varying coefficient for your treatment variables as well. That is, it might be true that extra treatment matters for the first month but not beyond that. Again, that's an ordinary modelling question -- you can look at diagnostics for proportional hazards and see if the hazard ratios seem to vary with time.